0

I am new to PHP and am currently constructing a do/while loop from a tutorial. I would understand if the whole condition was ($variable == true) or ($variable == false), however in the tutorial the while condition is simply while($variable). Could anyone explain this to me?

Here is the tutorial code.

<?php
    $loopCond = false;
    do {
        echo "<p>The loop ran even though the loop condition is false.</p>";
    } while ($loopCond);


    echo "<p>Now the loop is done running.</p>";
?>
2
  • When LoopCond is false it will echo Now the loop is done running. Commented Aug 6, 2014 at 9:45
  • Also note that a do loop will ALWAYS run at least once, as the while condition is evaluated at the end of the iteration. If this behavior is undesirable, you should use a while loop instead, which evaluates the condition before the iteration Commented Aug 6, 2014 at 9:49

7 Answers 7

6

All such conditional statements, including while and if, are evaluating the given expression against true. If the expression results in true, the statement executes the action. If it results in false, it won't.

$var == true is an expression which compares $var to true. The result of this expression is either true or false. The important point to understand here is expressions. Expressions are things which return values. Try var_dump($var == true) or var_dump(4 > 6). It shows you that the expressions return a boolean value. Here:

if ($var == true)

first $var is compared to true, which yields either the value true or false, which is then evaluated by if whether it's true or false, which then prompts if to execute the following statement or not.

In other words: it's redundant.

if ($var)

This simply causes if to evaluate whether $var is true or false and then execute the following statement. The == true is essentially already "built in".

The following statements are all essentially equivalent:

if ($var)
if ($var == true)
if (($var == true) == true)
if ((($var == true)) == true) == true)
...
Sign up to request clarification or add additional context in comments.

2 Comments

You should also mention that if $var has set a value like "random string" if ($var) will also return true.
I'm not going to go into explaining the truth tables of PHP in this answer. The rules are the same between if ($var) and if ($var == true).
0

A boolean value true or false should not be used with a redundant $c == true as the result is the same as $c: true or false

$driving = true;

while ($driving) {
while ($driving == true) { // ugly

while (! $driving) { // while not driving.
while ($driving == false) { // ugly

$drinking = ! $driving;
if ($driving && $drinking) {

Hence also use adjectives for boolean variables.

Comments

0

A condition is met, if the value or statement in it is considered as true.

The code $variable == true is a statement that looks whether the value of the variable is true and if it is, yields true - Or false if it is not.

However, as this means, that $variable itself can only ultimately be true or false, you don't even need the statement, as its return value will also be one of those two.

Therefore $variable is exactly the same as $variable == true.

I hope this made it clear.

Comments

0

The semantic of while/do-while is

   while(<boolean expression>) {
       // do your stuff
   }

A boolean expression is anything that evaluates to true or false. So, if $loopCount is true, then $loopCount == true is checked on every loop and evalutes to true. But you could also write $LoopCount as condition, since it also evaluates to true.

This is very handy for using other data types, e.g. integers.

$count = 0;
while ($count < 10) { 
    $count = $count +1;
}

Here $count < 10 is a boolean expression that evaluates to true as long as $count is not higher then 9.

A while loop runs as long as the condition is met, in other words, as long as the boolean expression you provide evaluates to true.

You can also just use a variable, e.g. $loopCount when that variable evaluates to a boolean or a constant (even the constant value true).

Comments

0

Like Padarom said: Therefore $variable is exactly the same as $variable == true.

In your case: The while-do loop determines if redo the loop-body after the first run. Means the loop-body is executed exactly one time regardless what value $variable has. After the first run, the while($variable) checks if the expression is true. If so, the loop-body is executed second time and so forth.

Check PHP reference for do-while loops here. PHP.net do-while reference

Comments

-1

while ($loopCond) and while ($loopCond == true) is the same thing. It checks the "trueness" of whatever you put in the brackets.

Comments

-3

If I ask a question "does sun set in the west ? " what would be your answer, definitely YES OR TRUE. Same as compiler always look for statement value. Take a look

        $condition = true;
        if($condition == true )
         // above will return TRUE; in short $condition == true will replaced by true at runtime.  But if we place true directly which is $condition value or can say we place $condition instead true  thus statement become shorten and look like...
        if($condition) {
         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.