Why does PHP not throw an error when you have a typo in the code when error reporting is set to E_STRICT

StackOverflow https://stackoverflow.com/questions/19944309

Question

EDIT: In an effort to make this question not totally useless I'm changing the subject.

Why does PHP not throw an error when you have a typo like contninue; in the code when error reporting is set to E_STRICT.

See my below original question. I was trying to continue a foreach loop, but accidentally wrote contninue; instead of continue;, yet I received no warning or error from either NetBeans IDE or PHP when the script was run. I have E_STRICT and E_ALL error reporting on, HTML errors on, display errors on, etc. Is there any way to make programming in PHP more bullet-proof to these kinds of stupid errors? Note that there was no variable or function named contninue. In fact, I can type pretty much anything in a line including dsafdsfdafdsafdsafdsafdsa_FDA9812324398s; and it will not throw an error.


Original question: Why doesn't continue in PHP for each loop skip to the next loop?

It's a bit embarassing to post a question like this but here goes...

Here's a little snippet of code from a function. I'm simply trying to recreate an array filtering out one or more items in the array that meet certain criteria, in this case match a postId. (This is development in Wordpress in case you're wondering.)

In my simple foreach loop, the continue; command doesn't appear to be doing its job. At least its not behaving how I would expect from any other language I've used (C, C#, MEL, Javascript, etc.) The echoed output always includes "should have exited but it did not!" even though its after the continue; and should never be executed. Also, the resulting array always contains the filtered content even though "REMOVED " . $postId clearly ran.

$posts = get_posts( $args ); // posts is an array of objects
if($posts == null) return; // no posts
if($includeCurrentPost || $postId < 0) return $posts; // we are including all posts, so just return it

// Remove current post id from list since excluding
$newPosts = array();
$count = 0;
foreach($posts as $p) {
    if($p->ID == $postId) {
        echo("REMOVED " . $postId);
        contninue;
        echo("should have exited but it did not!");
    }
    $newPosts[$count++] = $p;
}
return $newPosts;

Okay, I normally program in C# and consider myself a decent mid-level programmer. This just doesn't make any sense. @_@ I'm guessing its going to turn out to be some stupid syntax error that PHP is just letting me get away with that I can't see...

PHP version it 5.3 something, Apache, running on Windows Vista.

Was it helpful?

Solution

I just tried your code here: http://writecodeonline.com/php/

$posts = array(1,2,3,4,5);
foreach($posts as $p) {
    if($p == 3) {
        echo("REMOVED " . $p . "\n");
        contninue;
        echo("should have exited but it did not!");
    }
    echo("Other stuff\n");
}

It doesn't work because of contninue instead of continue, but also doesn't have an error by default (this probably means you don't have strict errors on).

When you fix the typo:

$posts = array(1,2,3,4,5);
foreach($posts as $p) {
    if($p == 3) {
        echo("REMOVED " . $p . "\n");
        continue;
        echo("should have exited and it did!");
    }
    echo("Other stuff\n");
}

It works

OTHER TIPS

In PHP, continue ends the current iteration but does not break the loop. break will end the loop

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top