Why did i get this result in php? - php

if(NULL ==0){
echo "test". NULL;//output is test
echo "<br>";
echo "test". 0;//output is test0
}
If condition say both null and 0 are equal.But why did i get this result?

You have used Loose comparison == . If you use Strict comparison === then you will find the differences.
Read more :
type comparison table
NULL in PHP

Because it depends if you are looking at 0 as the number zero (as in nothing) or a string (as in the character '0').

NULL in PHP has the following properties:
NULL == NULL is true,
NULL == FALSE is true.
And in line with the relational model, NULL == TRUE fails
Over here you are comparing NULL to false whose output is true in PHP

As Nick already said: in this case, you are adding the value 0 to a string that makes it also a string. That's why you get the value test0.
Also, in your if, you are checking for zero value, not a strict true or false statement:
<?php
if( NULL == 0 ) {
echo "test" . NULL;
echo "<br>";
echo "test" . 0;
}
?>
Output:
test
test0
Now try it like this:
<?php
if( NULL === 0 ) {
echo "test" . NULL;
echo "<br>";
echo "test" . 0;
}
?>
You will see, that you get no output, because now the if statement is false.

NULL has no value, in your comparison it is evaluating to False, and 0 is also evaluated as False (so False == False, which is True), which is why the body of the loop executes.
NULL explicitly means "no value". See the documentation for what exactly NULL is.

because if you concat a string with NOTHING (null) the string will remain at it is, if you concat with the integer "0", it will be casted to string (auto-boxing) and concated to the original string...
normal behaviour?
and "null == 0" -> true, but "null === 0" -> false...
you have to check not only the VALUE (which will be "zero" for both itc), you have to check TYPE equality too with "==="

Because, a NULL string is nothing to be printed. Hence the first echo statement, concatenates:
"test" . NULL => "test" Then Nothing
While the 0 is a logical NULL, but in case of String it prints as 0.

In PHP, if you compare any value to a number, the value will be typecasted to Int or Float, and then the comparison will be done. In your case, the NULL is first typecasted to Int, which produces 0, and then compared, givng TRUE. Check PHP type comparison and type juggling.

Related

String as boolean in PHP [duplicate]

For the record, I know the solution is to use === instead of == .
I'm just wondering what the logic behind it is. How is it logical that 'hello' can equal TRUE?
$var = TRUE;
if($var == 'hello'){
echo 'match';
}
else{
echo 'no match';
}
The solution has been discussed, but I haven't seen any real explanation.
String value equals true
== compares just the values of the variables whereas === compares variable values and type. so for an example:
1 == 1: true
1 === "1": false // "1" is a string and 1 is an integer
when asking if a string == true, you are essientially asking if it is set. Similar functionality is behind the isset() method.
If you were to compare "hello" === true. This would be false as they are of different type and "hello" would HAVE to equal "hello"
When using == operator think in falsy and truthy terms.
So:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integers 0 and -0 (zero)
the floats 0.0 and -0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
-1 is considered TRUE, like any other non-zero (whether negative or positive) number!
Every other value is considered TRUE (including any resource and NAN).
#see: https://www.php.net/manual/en/language.types.boolean.php

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

How is it logical that a string equals TRUE in PHP

For the record, I know the solution is to use === instead of == .
I'm just wondering what the logic behind it is. How is it logical that 'hello' can equal TRUE?
$var = TRUE;
if($var == 'hello'){
echo 'match';
}
else{
echo 'no match';
}
The solution has been discussed, but I haven't seen any real explanation.
String value equals true
== compares just the values of the variables whereas === compares variable values and type. so for an example:
1 == 1: true
1 === "1": false // "1" is a string and 1 is an integer
when asking if a string == true, you are essientially asking if it is set. Similar functionality is behind the isset() method.
If you were to compare "hello" === true. This would be false as they are of different type and "hello" would HAVE to equal "hello"
When using == operator think in falsy and truthy terms.
So:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integers 0 and -0 (zero)
the floats 0.0 and -0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
-1 is considered TRUE, like any other non-zero (whether negative or positive) number!
Every other value is considered TRUE (including any resource and NAN).
#see: https://www.php.net/manual/en/language.types.boolean.php

PHP Casting Error

<?php
isInt("4");
isInt("Test");
function isInt($id){
echo $id." ".(int)$id;
if($id == (int)$id)echo "True.<br />";
else echo "False.<br />";
}
?>
The output results in:
4 4 True.
Test 0 True.
You'll notice the 2nd results in true, which by the output, it should echo false.
I realize there is a built in function in php is_int().
I also realize that in the if statement if I put a 3rd equals sign: if($id === (int)$id) then it will return false for the 2nd one, but it will also return false for the 1st one too.
Can someone explain to me why PHP does this, and maybe a fix for this? (I am running PHP 5.4.22)
Basically what I want to accomplish is isInt("4") to echo true, isInt(4) to echo true, and isInt("Text") to echo false;
If you use the triple equals, it checks the type along with the result.
For the first one, "4" is a string, not an int (the literal 4 is an int, but since you're using quotes, it's a string).
For the second one, "Test" is a string as well.
For your second version:
$id = 'Test';
'Test' == (int)'Test';
'Test' == 0
0 == 0
TRUE
Strings which contain no digits at the beginning of the string will always get auto-converted to integer 0 when used in an integer context.
If you'd had 123Test instead:
$id = '123Test';
'Test' == int('Test');
'Test' == 123
0 == 123
FALSE
to get desired value , check this..
if($id === (int)$id)echo "True.<br />";

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