Asked  7 Months ago    Answers:  5   Viewed   31 times

I have a loop that is doing some error checking in my PHP code. Originally it looked something like this...

foreach($results as $result) {
    if (!$condition) {
        $halt = true;
        ErrorHandler::addErrorToStack('Unexpected result.');
    }

    doSomething();
 }

if (!$halt) {
    // do what I want cos I know there was no error
}

This works all well and good, but it is still looping through despite after one error it needn't. Is there a way to escape the loop?

 Answers

93

You are looking for the break statement.

$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(, $val) = each($arr)) {
    if ($val == 'stop') {
        break;    /* You could also write 'break 1;' here. */
    }
    echo "$val<br />n";
}
Wednesday, March 31, 2021
 
o_flyer
answered 7 Months ago
38

The recommended way to do this is with an anonymous function:

$functions = [
  'function1' => function ($echo) {
        echo $echo;
   }
];

If you want to store a function that has already been declared then you can simply refer to it by name as a string:

function do_echo($echo) {
    echo $echo;
}

$functions = [
  'function1' => 'do_echo'
];

In ancient versions of PHP (<5.3) anonymous functions are not supported and you may need to resort to using create_function (deprecated since PHP 7.2):

$functions = array(
  'function1' => create_function('$echo', 'echo $echo;')
);

All of these methods are listed in the documentation under the callable pseudo-type.

Whichever you choose, the function can either be called directly (PHP ?5.4) or with call_user_func/call_user_func_array:

$functions['function1']('Hello world!');

call_user_func($functions['function1'], 'Hello world!');
Wednesday, March 31, 2021
 
Norgul
answered 7 Months ago
69

Use RecursiveDirectoryIterator in conjunction with RecursiveIteratorIterator.

$di = new RecursiveDirectoryIterator('path/to/directory');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
    echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
}
Wednesday, March 31, 2021
 
uiroshan
answered 7 Months ago
72

$_FILES is solely for handling files uploaded to the script by the client. You should be able to insert the file contents using file_get_contents() or fopen()/fread() without further ado.

Wednesday, March 31, 2021
 
hohner
answered 7 Months ago
38

As other answers mention, the Reverse() extension method will let you enumerate a sequence in reverse order.

Here's a random enumeration extension method:

public static IEnumerable<T> OrderRandomly<T>(this IEnumerable<T> sequence)
{
    Random random = new Random();
    List<T> copy = sequence.ToList();

    while (copy.Count > 0)
    {
        int index = random.Next(copy.Count);
        yield return copy[index];
        copy.RemoveAt(index);
    }
}

Your usage would be:

foreach (int n in Enumerable.Range(1, 10).OrderRandomly())
    Console.WriteLine(n);
Wednesday, August 4, 2021
 
The_Perfect_Username
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :