2

this is my first post. I am sorry for the totally amateur question but I want to learn PHP so I started doing some online courses-tutorials for PHP and there was an example without explanation and I can't find the answer on google.

In the code line 4 $flip = rand(0,1); that means that $flip is getting a random number 0 or 1 right? Then at line 6 there is if ($flip) { ... But they don't explain what "if ($flip)" means or equals to. For example $flip = 1 or $flip = 0. Thank you in advance.

$headCount = 0;
$flipCount = 0;
while ($headCount < 3) {
    $flip = rand(0,1);
    $flipCount ++;
    if ($flip){
        $headCount ++;
        echo "<div class=\"coin\">H</div>";
    }
    else {
        $headCount = 0;
        echo "<div class=\"coin\">T</div>";
    }
}
echo "<p>It took {$flipCount} flips!</p>";
Kiri
  • 23
  • 3

3 Answers3

1

Basically In PHP expression is evaluated to its Boolean value. for e.g

if($flip) // means if(1) or if(0)

if (expr)
  statement

0 : is evaluated as Boolean FALSE

if($flip){

} 
else { 
  // goes here
} 

1 : is evaluated as Boolean TRUE (non-zero)

if($flip){
  // goes here
} 
else { 

} 

If the value of the first expression is TRUE (non-zero) it enters into if block

kamlesh.bar
  • 1,774
  • 19
  • 38
0

A variable equal to 0 means false, other numbers mean true :

if(0) => false // you go in the else statement
if(1) => true
if(2) => true

etc...

In fact, when you do if($flip), the test executed by PHP is if($flip != 0)

Random
  • 3,158
  • 1
  • 15
  • 25
  • there aren't a 2 value :) – Zakaria Wahabi May 21 '15 at 09:37
  • 1
    @ZakariaWahabi It is for comprehension purpose, since OP wants to learn ;) – Random May 21 '15 at 09:37
  • yes i see , but for the comprehension and good comprehension there aren't a 2 value for the booleans , 0 - 1 => false - true thats it :) – Zakaria Wahabi May 21 '15 at 09:41
  • 1
    The answer that was totally clear for a starter like me was the one someone post but can't find anymore that if ($flip) actually means $flip==1 or $flip!=0 like Honza Haering said. Thank you all for the answers I can't upvote since my reputation is low. – Kiri May 21 '15 at 09:46
  • @Kiri although your reputation is low you can accept answer you like – kamlesh.bar May 21 '15 at 09:56
  • @Kiri Yes i deleted my comment since Honza Haering edited his comment. I edited my answer to contain what you wish – Random May 21 '15 at 10:02
  • Yes I did that already. Your answer is also correct just bit harder to get when you are totally new in PHP. – Kiri May 21 '15 at 10:05
  • You might need to edit your answer @Random. Even though the OP understood what he is looking for, the explanation looks confusing.When would we use such `if` in realworld? and `2` ? – Kishor May 21 '15 at 10:09
  • 1
    @Kishor Please exlicit what is confusing ? Then, about your question, first, it would be `if`s with variables (not constant like here), and I had a live exemple yesterday. reading some multi-thread code (with PCNTL). The result of `pcntl_fork()` is `0` if you are in father, and thread's `PID` if you are in the child. So a `if($pid)` is used to check it out... However, it is not recommanded to use it this way, you should explicit `if($pid != 0)` to be clear. – Random May 21 '15 at 12:08
  • For a starter, it would confuse him than help him. that was my point. Cheers! – Kishor May 21 '15 at 12:19
0

You are correct in you first assumption:

$flip = rand(0,1);

$flip will be equal to either 0 or 1. The below for example:

$flip = rand(0,5);

Will return a number between 0 and 5 randomly.

Generally if you do an if statement like so

if($example)

You are testing if the value is true or false. True can also be expressed as 1 and false can also be expressed as 0.

Therefore as $flip is equal to a 0 or 1, it is equal to either true or false. So if flip is true or 1, then it will execute the code in the if statement. As a note you could reverse how the if works by testing for false using:

if(!$flip)

Effectively if $flip is greater than 0 it will be considered true, see the example below:

$test = 0;
if($test) //This will be false and therefore echo It is false
{
  echo "It is true";
}
else
{
  echo "It is false";
}

$test2 = 5;
if($test2) //This will be true and therefore echo It is true
{
  echo "It is true";
}
else
{
  echo "It is false";
}

Update

You could consider the following to effectively be the same:

  if($flip)
  if($flip == 1 || $flip == true || $flip > 0)
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
  • So to make it clear for me and the other people reading. Let's say we make $flip = rand(0,9); Flip is getting one of the following 0,1,2,3,4,5,6,7,8,9 So if ($flip) which actually means $flip == 1 and the statement that follows will occur only when $flip == 1 otherwise will go to else – Kiri May 21 '15 at 09:57
  • @Kiri - You are mistaken. It means `if($flip)` checks if `$flip` is `0` or `FALSE`. The `if` doesnt compare or check if its `1` or `TRUE`. – Kishor May 21 '15 at 10:16
  • @Kiri as kishor mentions, the `if` doesn't care if the value is eith `1` or `true`, just simply that it is not false and not 0. I have put an update above to help clarify. – The Humble Rat May 21 '15 at 10:23
  • This page explains which values are cast to which booleans in detail: http://php.net/manual/en/language.types.boolean.php – Erik May 21 '15 at 10:24
  • Technically, the if does care whether it's 1 or true; it will cast whatever result it gets down to a boolean. The reason that both 1 and true will go in the branch is because 1 when cast to a boolean becomes true. – Erik May 21 '15 at 10:25
  • Yes my mistake I meant 0 but anyway with your comments things are totally clear now. The if($flip) more or less is like the binary 01 true or false, electricity passing or not. – Kiri May 21 '15 at 10:51
  • Dont use two = to compare, always use === – Vladimir May 21 '15 at 10:58
  • @Ara can you explain why? – Kiri May 21 '15 at 11:22
  • @Kiri Check this question: http://stackoverflow.com/questions/3641819/php-not-equal-to-and?lq=1 – Vladimir May 21 '15 at 12:01
  • @Ara very interesting! Thank you for the info. I also found another post plus I will post 2 examples for people to get it. http://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp http://www.w3schools.com/php/showphp.asp?filename=demo_oper_equal http://www.w3schools.com/php/showphp.asp?filename=demo_oper_identical – Kiri May 21 '15 at 14:23