foreach ($inputs as $input) {
    // mark added messages for deletion
    for ($i = 1; $i < 4; $i++) {
        imap_delete($stream_id, $i);
    }
    echo "\n-- Iteration {$iterator} --\n";
    var_dump($check = imap_close($stream_id, $input));
    // check that imap_close was successful, if not call imap_close and explicitly set CL_EXPUNGE
    if (false === $check) {
        imap_close($stream_id, CL_EXPUNGE);
    } else {
        // if imap_close was successful test whether CL_EXPUNGE was set by doing a message count
        $imap_stream = imap_open($mailbox, $username, $password);
        $num_msg = imap_num_msg($imap_stream);
        if ($num_msg != 0) {
            echo "CL_EXPUNGE was not set, {$num_msg} msgs in mailbox\n";
        } else {
            echo "CL_EXPUNGE was set\n";
        }
        // call imap_close with CL_EXPUNGE explicitly set in case mailbox not empty
        imap_close($imap_stream, CL_EXPUNGE);
    }
    $iterator++;
    // get $stream_id for next iteration
    $stream_id = imap_open($mailbox, $username, $password);
    populate_mailbox($stream_id, $mailbox, 3);
}
?>
===DONE===
<?php 
require_once dirname(__FILE__) . '/clean.inc';
<?php

/* Prototype  : bool imap_createmailbox  ( resource $imap_stream  , string $mailbox  )
 * Description: Creates a new mailbox specified by mailbox .
 * Source code: ext/imap/php_imap.c
 */
echo "*** Testing imap_createmailbox() : basic functionality ***\n";
require_once dirname(__FILE__) . '/imap_include.inc';
$imap_stream = imap_open($default_mailbox, $username, $password) or die("Cannot connect to mailbox {$default_mailbox}: " . imap_last_error());
$newname = "phpnewbox";
echo "Newname will be '{$newname}'\n";
$newbox = imap_utf7_encode($server . $newname);
if (imap_createmailbox($imap_stream, $newbox)) {
    echo "Add a couple of msgs to '{$newname}' mailbox\n";
    populate_mailbox($imap_stream, $newbox, 2);
    $status = imap_status($imap_stream, $newbox, SA_ALL);
    if ($status) {
        echo "Your new mailbox '{$newname}' has the following status:\n";
        echo "Messages:    " . $status->messages . "\n";
        echo "Recent:      " . $status->recent . "\n";
        echo "Unseen:      " . $status->unseen . "\n";
        echo "UIDnext:     " . $status->uidnext . "\n";
        echo "UIDvalidity: " . $status->uidvalidity . "\n";
    } else {
        echo "imap_status on new mailbox failed: " . imap_last_error() . "\n";
    }
    if (imap_deletemailbox($imap_stream, $newbox)) {
        echo "Mailbox '{$newname}' removed to restore initial state\n";
    } else {
        echo "imap_deletemailbox on new mailbox failed: " . implode("\n", imap_errors()) . "\n";
    }