This question already has an answer here:
symbol and decimal number true in php
(1 answer)
Closed 8 years ago.
I have script like this
if('#' == 0){
echo "true";
}else{
echo "false";
}
output :
true
the question is why it get true and how to get it false?
thanks.
As Mark Baker stated in the comments, PHP uses weak typing. According to the comparison matrix that you find here (http://www.php.net/manual/en/types.comparisons.php), the expression involving the string '#' compared to 0 will be evaluated as true.
Change your condition to:
if('#' === 0){
...
in this case you'll get false.
For this condition to get true of false properly, you need to compare the value and the type of the operators. Try this:
if('#' === 0){
echo "true";
}else{
echo "false";
}
You will get false for this case.
Related
This question already has answers here:
Case insensitive string comparison
(6 answers)
Closed 1 year ago.
Question:
if ("sometext" == "SOMEtext") {
echo "TRUE";
} else {
echo "FALSE";
}
It is return me FALSE, when I use double equal.
The only diference are the capital leters...
This if should return TRUE ????
You can use strtolower("somestring") in order to compare 2 strings without account for case sensitivity
This question already has an answer here:
Empty string comparison to zero gives different result in PHP 8 than in previous versions
(1 answer)
Closed 1 year ago.
in PHP 8 why comparing 0 with an empty string, surprised me.
I can't understand the logic.
$a = 0;
$b = "";
echo ($a == false) ? "yes a is False<br>" : "no<br>";
echo ($b == false) ? "yes b is False<br>" : "no<br>";
echo ($a == $b) ? "yes a equals False and b equals to False" : "no, False is not equal to False!!!, there is a problem";
the output is "no, False is not equal to False!!!, there is a problem"
in the above code, I expected to give yes a equals False and b equals False but PHP surprised me.
can you help me to understand why the output is no?
if(false == false) it should return true and it works fine.
but,
if("" == 0) it should return true too, but it will return false, even "" and 0 are false. I want to know why???
I want the logic behind this.
I use PHP 8.0.2
There is an incompatibility in PHP 8. You can find it here: https://www.php.net/manual/en/migration80.incompatible.php
I recommend you cast your values before comparing
if (boolval($a) == boolval($b)) {
// do stuff
}
I think that happen because PHP8.
I try this code
$a = 0;
$b = "";
$result = ($a == $b) ? "yes a equals False and b equals to False" : "no, False is not equal to False!!!, there is a problem";
echo($result);
In PHP5 and PHP7 its return "yes a equals False and b equals to False" but in PHP8 its return "no, False is not equal to False!!!, there is a problem"
you can try its in PHP online Sandbox
This question already has an answer here:
What does ( !'which npm' ) mean in a PHP script?
(1 answer)
Closed 4 years ago.
I have used if(!$url) but I don't know the exact meaning of the below code in PHP
Can we check string with logical operator
if (! 'redirect cart'){
// data
}
Is it allow to use in a logical operation?
It equals to comparing to false:
if (false == 'redirect cart') { ...
Which could be true if your string is empty:
if (! '') { // this condition will be met
But this makes no sense to write this condition with a direct string, this should be a variable:
$str = my_function() ? 'redirect cart' : '';
if (!$str) { ...
The ! Operator negates the value of the expression. Code like this $value = (!true); will return false i.e $value will be false. The same happens when you apply the operator to an expression that returns false
This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
Closed 7 years ago.
See the following issue:
$str = "video-23984"; // returns false
$str = " video-23984"; // returns true
$search= "video";
if(strpos($str,$search)) {
echo "True";
}else {
echo "False";
}
Why in the world does $str = "video-23984" return false? And what can I do to make it return true?
In the first string the word video is in the first position, which means 0.
In the second it's in the second position, which means 1.
Since you're returning it into an if, the 0 is a boolean for false. That's why you're getting false as a result.
This question already has answers here:
The 3 different equals
(5 answers)
Closed 8 years ago.
So I have this function and I'm trying to understand how this is true and how it comes out false if I use === instead of == .
function is_equal($value1, $value2) {
$output = "{$value1} == {$value2}: ";
if ($value1 == $value2) {
$output = $output . "true<br />";
} else {
$output = $output . "false<br />";
}
return $output;
}
echo is_equal("123", " 123");
echo is_equal("123", "+0123");
?>
this code above comes out true because I'm testing for == how is that? and also if I use === it's false
When you compare equality using ==, PHP will juggle the types. I suspect your types are being juggled resulting in a numeric comparison.
When you compare equality using === the type is compared first, followed by the values.
Yes, this is right. === will compare the value and the typeinstead of == that is comparing if values are identical.
You can also try this one :
echo is_equal("123", 123);
=== tests whether the two variables are identical (same value, same type). == tests for equality and does type juggling for you.
Read up over here.