user6173198
user6173198

Reputation:

What does the !function() do?

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

Upvotes: 0

Views: 58

Answers (2)

Al Foиce    ѫ
Al Foиce ѫ

Reputation: 4315

! 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.

Upvotes: 2

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";
}

Upvotes: 1

Related Questions