0

I was looking up true/false bool conditions from the php manual and I thought this should echo a false, but it echos a true instead. So what does the !call() truly do? Is it saying that the function is just working?

http://php.net/manual/en/control-structures.if.php

<?php
    $true=0; //false

    function call($true)
    {
        if ($true =1){
            return TRUE;

        }
        if ($true =0){
            return FALSE;
        }
    }

    if(call()) { 
        echo "true";
    }

    if(!call()) // or if(!call()) {
        echo "false";
    }

    if($true) { 
        echo "<br>true";
    }

    if(!$true)  {
        echo "<br>false";
    }
?> 

Output:

true

false
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
  • 3
    See [The 3 different equals](http://stackoverflow.com/q/2063480) for your if statements in your function (And https://en.wikipedia.org/wiki/Yoda_conditions as a little trick). Note that you never pass your variable to the function. Also see [What does this symbol mean?](http://stackoverflow.com/q/3737139) for the `!` symbol. – Rizier123 Sep 09 '16 at 16:42
  • From your comment, I see my error, totally missed the $true ==. –  Sep 09 '16 at 16:43
  • 1
    You're also not passing your variable to the function. – Rizier123 Sep 09 '16 at 16:44

2 Answers2

2

! is the logic operator not. Let's say you defined a var:

$myVar = true ;
var_dump( !$myVar); // will echo false

Your call() function will return true or false. So !cal(); will return the opposite, false or true.

Have a look at the PHP Manual.

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
1

The function call() returns true or false. !call() would simply indicate the scenario where the return value of this function is false.

Personally I like to set functions with Boolean return values, when called, equal to a variable, so I would definitely write:

$val = call();

if(!$val) {}

In your case, if you wanted to cut code to a minimum, you could write

echo call() ? "true" : "false";

to replace these lines:

if(call())
{ 
    echo "true";
}

if(!call()) // or if(!call())
{
    echo "false";
}