edgarmtze
edgarmtze

Reputation: 25038

(int)false; in php why would it be?

I am reviewing a partner php code and is like:

function query_db($form)
{
   $result = (int)false;    
   try{
    $sqlQuery = " SELECT id_user, id_access, name, email, status FROM users ";
    $sqlQuery .= " WHERE id_access> 0 ";
    if($form->get("limit")){
       $sqlQuery .= " LIMIT ".$form->get("start")." , ".$form->get("registry");
    }           
        $query = $this->db->query($sqlQuery);
    return $query;
   } catch (Exception $e) { 
    $result = (int)false;
   }
    return $result;
}

So at the very begining he uses $result = (int)false;

I was investigating this instruction and found:

A string is converted to false only if it is empty or if it contains the single character 0. If it contains any other data—even multiple zeros—it is converted to true.

When converted to a number or a string, a Boolean becomes 1 if it is true, and 0 otherwise.

So back to the question:

echo 0 + 1; // 1
echo false + 1; // 1
echo 0  > -1; // true
echo false  > -1; // false
echo (int)false > -1; // true

So why in the world he casts it instead of using $result = 0; I mean, I do not understand, maybe it has a deeper meaning and I am not seing this.

Thats why I ask your opinion.

Upvotes: 3

Views: 2719

Answers (2)

Samuel Cook
Samuel Cook

Reputation: 16828

The only reason I can see for casting false as an int would be for the identical comparison operator.

Take the below example for instance:

<?php
$x = false;
$y = (int)false;

if($x===false){ // validates
    echo 'Test x.1<br>';
}

if($x===FALSE){ // validates
    echo 'Test x.2<br>';
}

if($x===0){ // skips
    echo 'Test x.3<br>';
}

if($x==0){ // validates
    echo 'Test x.4<br>';
}

if(!$x){ // validates
    echo 'Test x.5<br>';
}

if(empty($x)){ // validates
    echo 'Test x.6<br>';
}

if($y===false){ // skips
    echo 'Test y.1<br>';
}

if($y===FALSE){ // skips
    echo 'Test y.2<br>';
}

if($y===0){ // validates
    echo 'Test y.3<br>';
}

if($y==0){ // validates
    echo 'Test y.4<br>';
}

if(!$y){ // validates
    echo 'Test y.5<br>';
}

if(empty($y)){ // validates
    echo 'Test y.6<br>';
}

Upvotes: 4

newfurniturey
newfurniturey

Reputation: 38416

The answer to "why would it be" is the simple answer: type coercion.

In your quote, you touched upon the answer:

When converted to a number or a string, a Boolean becomes 1 if it is true, and 0 otherwise.

Through type coercion, the (int)false converts the value to 0. If you were to have (int)!false, it would be 1.

The answer to "why would your friend use this", that's something you'll have to ask him =P. There is no benefit to doing this in this code-block and could be replaced with $result = 0 with zero effect on the outcome.

In response to a comment, 0 is typically false and 1 is true. When referring to success and error codes, however, it is highly dependent on the system you're interacting with. For instance, in bash, 0 is a "successful" exit code and 1 would mean an error occurred.

Not answer related:
In your exact code-block, you can actually remove the $result variable entirely since the code only-ever uses it to return 0 if the query failed; instead, you can just have return 0; at the end - or return false; if you'd prefer dealing directly with boolean values.

Upvotes: 4

Related Questions