Determine Difference Between NULL, ZERO (double) and value (double) - php

As both NULL and 0 return as empty in php, I'm struggling to determine the difference based on double type results from a mysqli recordset.
I've tried converting each scenario in order to return a more manageable returned string:
if($val == 0){
echo "No Cost Option";
} elseif (empty($val)){
echo "UNCOSTED";
} else {
echo "£ ".number_format($val ,2);
}
Theoretically, if a zero is present in the DB, it should return "No Cost Option".
Equally then, if the DB comes back with NULL, it should return "UNCOSTED".
Finally, if there is a value, it will simply format that value.
Currently (and incorrectly), both NULLs and 0s are being treated the same way so both get processed as "No Cost Option".

You can strictly compare to null. If you strictly compare to integer it might not work, because the result would be a numeric string.
Reverse your if statement order and check for null first.
if (null === $val) {
echo "UNCOSTED";
} elseif ($val == 0) {
echo "No Cost Option";
} else {
echo "£ ".number_format($val, 2);
}

If you strictly want to compare to the number 0 your should use ===
if($val === 0){
echo "No Cost Option";
} elseif (empty($val)){
echo "UNCOSTED";
} else {
echo "£ ".number_format($val ,2);
}
You use === to get a strict identical comparison (Type & value).
If you want more precision,
0, null, false, "0", [] (an empty array) and more (see type comparaison) are equal in value.
So
0 == null == false == "0" == array()
But they are not if you compare they type with ===

Related

How many = signs are required to equal a value in php?

I have following code which is not working with three equal signs (===). Hence it is working with single equal sign (=). I always use === in such cases and that works fine. I am confused why it is not working in this case?
$vendor_name = "Stock Returned";
if($vendor_name = "Stock Returned")
{
$stock_return === "Yes";
}
else
{
$stock_return = "No";
}
echo $stock_return;
Result of above code is showing "No". But in my understanding it should be "Yes"
The 1 equal sign (=) is used for assigning values, it is not used for comparisons.
Comparisons are made with == or ===, that makes your $stock_return === "Yes"; line incorrect as well because it looks like you want to assign a value to $stock_return
The difference between == and === is that
== is for testing if both values are equal, with php, a string '20' and an integer 20 are equal
=== is more strict and will test the type as well so a string '20' is no longer equal to an integer 20
Your code should look something like this to be correct
$vendor_name = "Stock Returned";
if($vendor_name == "Stock Returned")
{
$stock_return = "Yes";
}
else
{
$stock_return = "No";
}
echo $stock_return;

Boolean from MySQL to PHP - Conversion to tinyint and comparing

My database has multiple boolean values. After I saved the database, the booleans got converted into tinyint(1). I think this is because it just needs to save 1 or 0.
However, I now have a problem comparing the value in PHP. I saved the tinyint into an array without any code-wise conversion. The array has multiple entries that are text and date and multiple entries with booleans, for example:
array[0] is '09:45:00'
array[1] is '10:45:00'
array[2] is 1
array[3] is 0
array[4] is 0
array[5] is 1
array[6] is 'active'
Now if I loop through the array I want to check if the value is a time, a text, or true/false.
Checking if the entry is true will always return true, because no entry is empty. Checking if the entry is 1 or 0 works for the boolean, but when I check if 'active' == 0 it is returning true. Why is this the case and how can I get a false if I compare a string with a tinyint?
Comparing with === does not work in any case.
I think u can do this with some nested if-else statements. But I'm pretty sure there is a better-looking solution too. :)
$a=array('09:45:00','10:45:00',1,0,0,1,'active',3.12);
foreach ($a as $value) {
$type= gettype($value);
if ($type == "string") {
if(strtotime ($value)){
echo "$value is 'Time' \n";
}
else{
echo "$value is 'String' \n";
}
} elseif ($type == "integer") {
if($value == 0 || $value == 1){
echo "$value is 'Boolean' \n";
}
else{
echo "$value is 'Integer' \n";
}
} else{
echo "$value is ($type)!";
}
}

How can I distinguish an empty variable from a variable that has the value "0"?

I am a little confused with empty and 0. I want to find out if $number is empty. This means for example $number = But this should not include 0 because if $number = 0 it is not empty for me.
$number = 0;
if(empty($number)){
echo "$number is empty.";
} else {
echo "$number is 0";
}
My result is $number is empty.
http://php.net/empty
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Note that this is exactly the same list as for a coercion to Boolean false.
empty is simply !isset($var) || !$var. Try isset instead.
Here your workaround issues you can check this way.
$number = 0;
if (empty($number) && $number!== '0') {
echo "$number is empty.";
} else {
echo "$number is 0";
}
here i have checked if it is empty and non zero value then it is empty. hope it will help you.
Firsly, use
echo "\$number is empty";
or
echo '$number is empty'l
because when you write "$number", you'll get a value of $number instead of string '$number'.
About your question.
In your example your $number has integer value 0.
It's equal to boolean false.
Write description section here:
php.net empty
Correct example:
$number = 0
if($number === 0)
echo '$number is 0';
elseif(empty($number))
echo '$number is empty';
else
echo '$number isn't empty and not equal to 0';

What is my error in the very simple php?

i make a very simple php to check a data, but it not works.
<?php
$ngl="G";
if ($parsed[0][4]="0") {
$ngl="NG";
}
if ($parsed[0][5]="0") {
$ngl="NG";
}
?>
and the output of the
<?php echo $ngl; ?>
is always
G
But I know that $parsed[0][4] and $parsed[0][5] is 0. The problem is that the output is G and not NG! I also tried to remove $ngl="G"; but then the output is nothing.
What do I have to repair?
i just use = but == its the correct. thx all.
You're assigning (=) a value instead of comparing (== or ===)
<?php
$ngl = "G";
if($parsed[0][4] == "0")
{
$ngl = "NG";
}
if($parsed[0][5] == "0")
{
$ngl = "NG";
}
?>
Explanation:
When you put an assignment like $parsed[0][4]="0" in an if statement, the if will evaluate the "0" to false. The reason it evaluates just the "0" is because the line $parsed[0][4]="0" (any assignment) returns the right hand side of the operation (the "0")
== vs === : php.net - Comparison Operators
== is a loose comparison which doesn't compare the type. ie "2" == 2 is true (even though one is a string and the other is an integer)
=== is a strict comparison, comparing types as well as values. ie "2" === 2 is false
The if statement uses the former (loose) comparison on your "0", and of course 0 is the false value in binary (0 and 1), so 0 == false and "0" == false both evaluate to true - however, 0 === false would return false as 0 is an integer, whereas false is a boolean.
Use == not = when checking
<?php
$ngl="G";
if ($parsed[0][4]=="0") {
$ngl="NG";
}
if ($parsed[0][5]=="0") {
$ngl="NG";
}
?>
You use = to set variables. You use == to compare.
<?php
$ngl="G";
if ($parsed[0][4]=="0") {
$ngl="NG";
}
if ($parsed[0][5]=="0") {
$ngl="NG";
}
?>
You should use == rather than =:
<?php
$ngl="G";
if ($parsed[0][4]=="0") {
$ngl="NG";
}
if ($parsed[0][5]=="0") {
$ngl="NG";
}
?>
The reason is that the = is the assignment operator, while the == is the comparison operator (which is the one you want).
$parsed[0][4]=="0" will evaluate to 0, which is false, so $ngl will not be changed. The same thing happens with $parsed[0][5]=="0"
This line is not checking if $parsed[0][4] is zero. It makes it zero.
if ($parsed[0][4]="0") {
Change it to
if ($parsed[0][4]=="0") {

PHP If Statement not working correctly, not null

I'm struggling to understand why my if statement below always results in false. I am creating a function which will test incoming connections to a script which will reject connections made by certain bots.
In my test below, on applying the if logic, I'm expecting a TRUE as both the array $value and $test value should match... resulting in a NOT NULL?
$bots = array(0 => "PaperLiBot", 1 => "TweetmemeBot", 2 => "Appsfirebot", 3 => "PycURL", 4 => "JS-Kit", 5 => "Python-urllib");
$test = strtolower("PaperLiBot");
foreach($bots as $value)
{
$i = strtolower(strpos($value, $test));
if ($i != NULL)
{
echo "Bot is found";
exit;
}else
{
echo "not found";
}
}
I think you are trying to accomplish this
foreach($bots as $value)
{
$i = strpos(strtolower($value), $test);
if ($i !== false){
echo "Bot is found";
exit;
}else{
echo "not found";
}
}
you want to write:
if(stristr($value, $test)){
// found
}else{
// not found
}
null in PHP is mutually type-castable to '0', '', ' ', etc... You need to use the strict comparisons to check for a 0-based array index:
if ($i !== NULL) {
...
}
note the extra = in the inequality operator. It forces PHP to compare type AND value, not just value. When comparing value, PHP will typecast however it wants to in order to make the test work, which means null == 0 is true, but null === 0 is false.
the correct way to check if a variable is set to NULL is:
if(!is_null($var)){
[my code]
}
the strpos returns a boolean, not a NULL value.
if you are unsure of the content of a variable, you can always debug it with a simple
var_dump($var);

Categories