PHP printed boolean value is empty, why? - php

I am new to PHP. I am implementing a script and I am puzzled by the following:
$local_rate_filename = $_SERVER['DOCUMENT_ROOT']."/ghjr324l.txt";
$local_rates_file_exists = file_exists($local_rate_filename);
echo $local_rates_file_exists."<br>";
This piece of code displays an empty string, rather than 0 or 1 (or true or false). Why? Documentation seems to indicate that a boolean value is always 0 or 1. What is the logic behind this?

Be careful when you convert back and forth with boolean, the manual says:
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.
So you need to do a:
echo (int)$local_rates_file_exists."<br>";

About converting a boolean to a string, the manual actually says:
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.
A boolean can always be represented as a 1 or a 0, but that's not what you get when you convert it to a string.
If you want it to be represented as an integer, cast it to one:
$intVar = (int) $boolVar;

The results come from the fact that php implicitly converts bool values to strings if used like in your example. (string)false gives an empty string and (string)true gives '1'. That is consistent with the fact that '' == false and '1' == true.

If you wanna check if the file exists when your are not sure of the return type is true/false or 0/1 you could use ===.
if($local_rates_file_exists === true)
{
echo "the file exists";
}
else
{
echo "the doesnt file exists";
}

Related

Trouble working with the value of a checkbox server side [duplicate]

I am new to PHP. I am implementing a script and I am puzzled by the following:
$local_rate_filename = $_SERVER['DOCUMENT_ROOT']."/ghjr324l.txt";
$local_rates_file_exists = file_exists($local_rate_filename);
echo $local_rates_file_exists."<br>";
This piece of code displays an empty string, rather than 0 or 1 (or true or false). Why? Documentation seems to indicate that a boolean value is always 0 or 1. What is the logic behind this?
Be careful when you convert back and forth with boolean, the manual says:
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.
So you need to do a:
echo (int)$local_rates_file_exists."<br>";
About converting a boolean to a string, the manual actually says:
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.
A boolean can always be represented as a 1 or a 0, but that's not what you get when you convert it to a string.
If you want it to be represented as an integer, cast it to one:
$intVar = (int) $boolVar;
The results come from the fact that php implicitly converts bool values to strings if used like in your example. (string)false gives an empty string and (string)true gives '1'. That is consistent with the fact that '' == false and '1' == true.
If you wanna check if the file exists when your are not sure of the return type is true/false or 0/1 you could use ===.
if($local_rates_file_exists === true)
{
echo "the file exists";
}
else
{
echo "the doesnt file exists";
}

my List of comparison operators in php returns '1' when I use keyword echo why

I'm begenning at PHP , I was poking around my code and learning about List of comparison operators , however I'll try out to put echo before my comparasion operators and I've received this result : 1 then I though the reason is comparasion true equal to 1, else equal to 0 , in this moment seemed me somethig kinda python, yet I just got the 1 . why not 0 as result ?
Is attached my question
You're printing a boolean value. The value is converted to a string where 1 represents true and a blank string represents false.
From the manual:
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.
http://php.net/manual/en/language.types.string.php
You could use the ternary operator to output the string true or false where appropriate. Example:
echo (10 >= 12) ? 'true' : 'false';
when print boolean value in php it will print 1 if TRUE and "" if FALSE.
if you want to print 0 if FALSE then you can convert into int.
in your case you can use like this
$bool = 10 >= 12;
echo (int)$bool;
it will return 0.
<?php
echo 10>=9 // print 1 as it is true
echo 11>3 // print 1
echo 11 == 11 // print 1
echo 10>=12 // print nothing because it is false
This happening becuase a boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string).

Logic in PHP, why is this happening?

function x() {return -1;}
echo x();
echo "<br>";
if(x()) {echo "True";} else {echo "False";}
output:
-1
True
Why am I getting 'True' outputted, surely the if() test would fail as it's negative?
That's because you can only test on true or false.
False is defined as 0, while true is defined as not 0. So -1 is just as much true as 1, 2, 3 etc.
To make sure you're getting the right result, make a real comparison.
-1 is considered TRUE in boolean context. See Converting to boolean in the PHP manual.
Only numeric 0 values are false in PHP: http://php.net/manual/en/language.types.boolean.php
0 is false and everything else is true. That's why !
In PHP a -1 is true as it isn't 0. Use a real comparison like:
if(x() <= 0) { ...do stuff... }
Have a look here:
var_dump(x()); //output: int(-1)
and casted to boolean:
var_dump((bool)x()); //output: bool(true)
-1 is not false in PHP. You could check if it's > 0?
All what is 0 is false and everything else is true. Wikipedia article about it
Converting to boolean in PHP
To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.
See also Type Juggling.
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 (including any resource).
Warning
-1 is considered TRUE, like any other non-zero (whether negative or positive) number!
Source http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

In PHP why is true cast 1 and false cast an empty string?

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string).
Why isn't FALSE cast to "0"?
If you cast to an int, then to a string, it prints 0's..
$x=false;
print (string)(int)$x;
Prints 0. You can, of course, omit the string type cast as it's done by print anyway.

How is `if (!integer) {}` evaluated?

I came across this code in a php database class:
if( !$this->_Link_ID )
Link_ID is an integer.
So does this code just check if Link_ID is not 0?
I know from experience that if a variable is type Boolean, you can just test the var like
$myBoolean = true;
if ($myBoolean){
// code
}
I didn't realise this can be done for integers.
So how is if( !$this->_Link_ID ) evaluated?
It checks if the integer is zero if it's integer. It also evaluates to truth if it's set to null and if it's unset, but in the latter case it also spits out a warning. if there was no negation, that would be a test for non-zero.
For more details see: converting to boolean:
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 (including any resource).
this is simply a silly way of check for non-zero,if LINK_ID is 0 or null or false ,it will
give true(please notice the '!') ,else if the LINK_iD is any thing it will give false
LINK_iD = 1 ,if (!LINK_ID) //this will give false
LINK_ID = 0 if (!LINK_iD) //this will give true
if( !$this->_Link_ID )
will return true if the value of $this->_Link_ID is 0, empty string or null.
If you want to check explicitly for "0" then you should use the triple equal ("===" or "!==") to test the value. like so
if($this->_Link_ID === 0)
or
if($this->_Link_ID === false)
if you only want it to return true for false, but not "0".
if (!$this->_Link_ID) will be true if $this->_Link_ID is not 0, and false if it is 0.
In PHP, you can test anything as a Boolean. A Boolean can be represented as 0 or 1, with 0 being false and 1 being true. In PHP, anything that is not 0 will be true, and anything that is 0 will be false. For example:
$string = 'This is a test.'
if ($string) echo 'Evaluated to true!';
Will print 'Evaluated to true!'. If $string does not exist, it will print nothing.
you should use
if (!is_int($var))
because
if (!$var)
checks if $var is not 0 or false
and if you want to check if $var exists you need to use this:
if (isset($var))
not only integers though
Here goes an explanation http://php.net/manual/en/language.types.type-juggling.php
And here goes a cheat-sheet http://www.php.net/manual/en/types.comparisons.php

Categories