Sol Sol - 2 months ago 6
PHP Question

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

Answer

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