How Does True/False Work in PHP

How does true/false work in PHP?

This is covered in the PHP documentation for booleans and type comparison tables.

When converting to boolean, the following values are considered FALSE:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string '0'
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

Every other value is considered TRUE.

What does true false actually do in php

What makes the function stop executing is the Control Structure return, true or false are Boolean variables, also represented as 1 and 0 respectively.


if(function that returns a boolean !=1) 

The if will only execute if function that returns a boolean is true (1)


Learn more about return and Boolean variables


Please note that mysql_* is now deprecated as of PHP7 because of security issues. It is suggested that you switch to mysqli_* or PDO extensions.

PHP boolean: why `true == 'false'` is true?

Because 'false' is not a false value, it is a string that contains something.

So when the comparison is made, 'false' is equal to true.

A value is false if :

  • it is false : $val = false;
  • it is an empty string : $val = "";
  • it is zero : $val = 0;
  • it is null : $val = null;

See comparisons documentation.

PHP boolean TRUE / FALSE?

I don't fully understand your question, but you can use any of the examples you provided, with the following caveats:

If you say if (a == TRUE) (or, since the comparison to true is redundant, simply if (a)), you must understand that PHP will evaluate several things as true: 1, 2, 987, "hello", etc.; They are all "truey" values. This is rarely an issue, but you should understand it.

However, if the function can return more than true or false, you may be interested in using ===. === does compare the type of the variables: "a" == true is true, but "a" === true is false.

PHP check if a variable is true or false

I think your $input['appointed_phases1'] contains a string value.
In PHP, any string value that is not empty evaluates to true.

See this question:
How to convert string to boolean php

You can cast the value of $input['appointed_phases1'] to a boolean, or you can compare this value with 'true' instead of true.

True/False value giving output 1 or blank using PHP

PHP doesn't support printing True/False, so you can use following as a work-around:

echo $available_image ? 'true' : 'false';

Or even simpler, you can use:

echo json_encode($available_image);

Returning True/False from PHP to Ajax

Two thing are to be returned from your login() function:

  • a message
  • a boolean

So I suggest you to output the data as a json string like this:

Before the login() function, declare an array:

$response = [];

Then, within all the conditions you have... just set msg and success accordingly, like, for example if the login is correct:

$response["msg"] = "You're logged in buddy!";
$response["success"] = true;

At the end of all conditions echo the array as a json string:

echo json_encode($response);

It will send the following string:

{"msg":"You're logged in buddy!","success":true}

** Make sure that echo is the only echo in that PHP file!

On client-side now, in the success callback, it would be:

success: function(output) {

// Parse the string.
var json = JSON.parse(output);

swal(json.msg); // Sweet Alert...
if(json.success){
// something to do with the boolean true
} else {
// Something else
}
}

Inconsistency between TRUE and FALSE constant definitions in PHP

It inherently doesn't make sense to ask for the ord of a bool. ord expects a string, so casts any input to a string. true casts to '1', and false casts to ''. The ord of '1' is 49, and the ord of an empty string is 0.

That doesn't mean that true and false are defined as such. true is defined as true and false is defined as false. It's merely the type casting rules that you're stumbling over (and yes, they're arguably arcane). Most databases support native boolean types, or their PHP database API will convert PHP booleans to the database's equivalent, as long as you use the API correctly.

As for why those casting rules exist:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

https://www.php.net/manual/en/language.types.string.php#language.types.string.casting

No, it doesn't make any more sense than this.

Returning true/false in functions with a message to further explain the reason why it returned false

Note that the way the idea is demonstrated here might not be the best, but once you get te hang of it, it gets easier. Also, read end note, please.

If you want to use like this (true is expected and false is problem):

if(pokeme()){ /*success*/ } else { /* not needed */}

You can do something like this:

function pokeme($number){
//let's say you want to return true if it's >10 and -9 to -1
if($number > 10){
// do something
return true;
}
if($number < 0 && $number > -10){
return true;
}
// handling multiple problems (just 2 ^^)
if($number < -9){
throw new Exception("Invalid input. Can't use negative smaller than -9.");
}

throw new Exception('Invalid input. Expected bigger than 10.');
}

Two tests:

try{
echo "RESULT1 :".pokeme(-42).":"; // not shown (error in this line)
echo "RESULT2 :".pokeme(11).":"; // not shown
}catch(Exception $e){
echo "Error: '".$e->getMessage()."'"; // Just the message
}

echo "<br><br>";

try{
echo "RESULT3 :".pokeme(11).":<br>"; // shown
echo "RESULT4 :".pokeme(10).":"; // not shown (error in this line)
}catch(Exception $e){
echo $e; // Full error
}

You can use it like this:

try{
if(pokeme(11)){
echo "VALID INPUT<br>";
}
if(pokeme(5)){
echo "I'm not seen :\\";
}
}catch(Exception $e){
echo "Error: '".$e->getMessage()."'";
}

End note: Think of this like you are using a built-in php function that might cause an error. This way you have to handle it with a try..catch.

More about Exceptions.



Related Topics



Leave a reply



Submit