In my simple code 'continue' skips the condition evaluation and jumps straight to beginning of loop body (the if line) causing infinite loop. I can see that's exactly what happens when debugging and resuming execution with breakpoint on loop condition (it doesn't break) or when using step over. Probably just me being tired but I'm clueless.
while ( ($line = _e(fgets($dump_file))) !== FALSE )
{
if ($line === '')
{
echo 'continue';
continue;
}
You should use the break keyword to stop a loop.
Continue will go to the beginning of the next loop iteration while skipping the rest of your loop.
More info :
http://php.net/manual/en/control-structures.break.php
http://php.net/manual/en/control-structures.continue.php
continue will always continue to the next iteration of the loop.
The loop is infinite because the value of $line never changes within the loop, so either the loop infinitely executes or never does at all depending on the initial value of $line.
break will break out of the loop
You're assigning the result of _e to $line in your while loop. I'm assuming you're getting _e from WordPress, which is documented here: http://codex.wordpress.org/Function_Reference/_e .
Return Values
(void)
This function does not return a value.
So, your loop basically says:
while (void !== False) {
continue;
}
Which, of course, continues forever.
Note that several answers have stated that the break statement should have been used. The code above is designed to abort the loop when the end of file (EOF) is hit, to which fgets returns False. Each time the loop is run, fgets reads the next line until it reaches the end of the file, at which point it returns False.
From php.net:
Returns a string of up to length - 1 bytes read from the file pointed to by handle. If there is no more data to read in the file pointer, then FALSE is returned.
The problem here isn't that a break statement is needed, because the while condition will be false when fgets returns False. The issue is that _e returns void which !== False and therefore the loop continues indefinitely.
Quick solution is to remove the _e in the while statement and apply it to the string later in your code block if necessary.
OK so here's what happened. Problem with the infinite loop was indeed due to my _e function not accounting for it's argument being FALSE value and always returning a string so the while loop condition would always evaluate to TRUE as suggested by Ryan Vincent and geis.
However that does not explain why my debugger would not break on the condition. Turns out that this problem is not related. The debugger it self is acting out and now refusing to break on all kinds of random breakpoints for no good reason.
Marking geis's answer as solution for all the effort.
Related
everyone. I saw a PHP while loop example today that I don't quite understand.
Example (Complete code)
// Open XML file
$fp=fopen("note.xml","r");
// Read data
while ($data=fread($fp,4096)) { // Line about which I have questions
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
The expression $data=fread($fp,4096) inside the while loop parentheses doesn't seem to change at all. It is just ONE assignment statement. How does this loop end?
The function xml_parse($parser,$data,feof($fp)) may end when parsing is complete, but I don't see how it will affect the test condition for the while loop. I feel the parsing of the XML file would repeat indefinitely.
Also when there is assignment expression as a test condition for a loop, are we really just looking to see if the RIGHT side of the assignment yields TRUE or FALSE to determine whether to end the loop? I have doubt about this though because then it would produce an infinite loop for this example since $data, once assigned, will always return TRUE.
Thanks
It ends when the fread() function returns false, which does when it reaches the end of the file. You can confirm this in the PHP documentation for fread:
http://php.net/fread
As for your question about the evaluation of the assignment, in PHP when you evaluate an assignment it will return the final value of the variable (different to Javascript for instance, in which the assignment is indeed what becomes evaluated), so you're basically both assigning a value to a variable from a function and evaluating the result of that function (assigned to the variable) without any extra syntax.
Work
while(there is data to read) OR (!EOF)
break when reach (EOF==true) OR no data to read OR return false
* EOF (end of file)
As php manual says, the function fread returns the read string or FALSE on failure.
That means:
$data=fread($fp,4096)
In the above statement the value of $data is either the contents of your file note.xml or false.
As you may know, while loop will continue until the condition is false. This statement is false only when fread($fp,4096) function fails to read data from the file, when there is no data left to read.
I want to try and make my code as efficient as possible and from what ive read
if($process == true)
Will process faster than something that calls a function. I.e
if(count($total) == 100)
So if i had an OR inside my if and the first condition was a simple boolean check and it turned out to be true would the second part of the condition still be checked anyway?
For example
$process = true;
if($process == true || count($total) == 100)
Would the count function still be called even though process is true and that is enough the make the condition pass.
PHP has indeed a mechanic named short-circuit:
http://php.net/manual/en/language.operators.logical.php#example-140
If the first operand of a logical OR is true, then the second part isn't evaluated, and the function isn't called.
Since comparing a variable to a boolean is faster than calling a function in PHP, this mechanic can help you to optimize, but never forget that premature optimisation is the root of all evil.
It will work same as exactly logical operator works. For example foo() will never get called.
if (false && foo()) {
}
if (true || foo()) {
}
In case of OR once it found a true statement it wont check further conditions,
In case of AND once it found a false statement it wont check further conditions.
its known as lazy evaluation.
illustration:
<?php
$var=0;
if($var++ || $var++){//Since 0 means false in Php both conditions will be checked
//Do nothing
}
echo $var;//output :2
if we change condition to if(++$var || $var++) the first condition will return true hence next condition will not be checked thus it will print output as 1;
If any statement inside a or is true other statements will not be checked and the if will be evaluated to true.
If you are checking a and statement all statements needs to evaluate to true and if any of them is false the stamen check will stop because the if is already false.
Just another important detail is that a function inside a if does not slow down the if check, what will slow down is what the function is doing and if it needs a lot of processing of course will be slower than just check a boolean but do not get afraid of use functions because they are very important in any system.
(I remember this from C# but I'm quite sure it's the same in php)
This depends if you use the single | or double || I think if you used the single ones it would still go do the other one ( usefull for when theres a function like loggin behind it ) if you use double and the first one is true it'll skip the second condition.
(This could be the other way around, so best bet is to try it using Javascript eg. create 2 function each returning true after alerting something, and thest these with either 1 line or 2)
I am new to PHP and saw the code below online. It has continue 2 and break together in switch/case statement. What does it mean?
foreach ( $elements as &$element ) {
switch ($element['type']) {
case a :
if (condition1)
continue 2;
break;
case b :
if (condition2)
continue 2;
break;
}
// remaining code here, inside loop but outside switch statement
}
The continue 2 skips directly to the next iteration of the structure that is two levels back, which is the foreach. The break (equivalent to break 1) just ends the switch statement.
The behavior in the code you've shown is:
Loop through $elements. If an $element is type "a" and condition1 is met, or if it's type "b" and condition2 is met, skip to the next $element. Otherwise, perform some action before moving to the next $element.
From PHP.net:continue:
continue accepts an optional numeric argument which tells it how many
levels of enclosing loops it should skip to the end of. The default
value is 1, thus skipping to the end of the current loop.
From PHP.net:switch
PHP continues to execute the statements until the end of the switch
block, or the first time it sees a break statement.
If you have a switch inside a loop and wish to continue to the next
iteration of the outer loop, use continue 2.
continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.
Source: http://php.net/manual/en/control-structures.continue.php
continue and break are similar in that the will stop something from happening.
in case of continue, it will stop anything after the braces but won't stop the loop. The switch statement just gets out of this statement and goes on to the next statement.
In case of break it will stop the entire loop from continuing, end the loop there.
I was looking through some code at work and found something I've not encountered before:
for (; ;)
{
// Some code here
break;
}
We call the function that contains this all the time, I only recently got in there to see how it works. Why does this work and is it documented somewhere?
It seems as though a while loop would have been more appropriate in this instance...
It's essentially the same as while(true). It doesn't have any initialisation, doesn't change anything between iterations, and in the absence of anything to make it false the condition is assumed to be true.
It's an infinite loop.
Normally you would have something like:
for ($i=0; $i<10; $i=$i+1)
But you can omit any of the parts.
These are all valid:
for ($i=0; ; $i=$i+1)
for (; $i<10; $i=$i+1)
for (; $i<10;)
However, if you omit the second part, there will be no condition for exiting the loop. This can be used if you do not know how many times you want to run the loop. You can use a break instruction to exit the loop in that case
for (;;)
{
// some code
if (some condition)
break;
}
Note that if you do not put a break the page will just get stuck and run indefinitely
The first blank statement is executed at the beginning.
The second blank expression (which determines whether you exit the loop or not) evaluates to TRUE implicitly:
http://php.net/manual/en/control-structures.for.php
The third blank statement executes after each iteration.
So any condition that kicks out of the loop will need to be in the loop itself.
I've seen this code, and I've no idea what it means.
while(true){
echo "Hello world";
}
I know what a while loop is, but what does while(true) mean? How many times will it executed. Is this not an infinite loop?
Although is an infinite loop you can exit it using break. It is useful when waiting for something to happen but you don't exactly know the number of iteration that will get you there.
Yes, this is an infinite loop.
The explicit version would be
while (true == true)
This is indeed (as stated already) an infinite loop and usually contains code which ends itself by using a 'break' / 'exit' statement.
Lots of daemons use this way of having a PHP process continue working until some external situation has changed. (i.e. killing it by removing a .pid file / sending a HUP etc etc)
Please referes to the PHP documentation currently at: http://www.w3schools.com/php/php_looping.asp
The while loop executes a block of code as long as the specified condition is true.
while (expression) {
statement(s)
}
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the
while statement executes the statement(s) in the while block. The
while statement continues testing the expression and executing its
block until the expression evaluates to false.
As a consequence, the code:
while (true) {
statement(s)
}
will execute the statements indefinitely because "true" is a boolean expression that, as you can expect, is always true.
As already mentioned by #elzo-valugi, this loop can be interrupted using a break (or exit):
while (true) {
statement(s)
if (condition) {
break;
}
}
It is indeed an infinite loop.