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;
Related
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 ===
If variable value is 0 (float) it will pass all these tests:
$test = round(0, 2); //$test=(float)0
if($test == null)
echo "var is null";
if($test == 0)
echo "var is 0";
if($test == false)
echo "var is false";
if($test==false && $test == 0 && $test==null)
echo "var is mixture";
I assumed that it will pass only if($test == 0)
Only solution I found is detect if $test is number using function is_number(), but can I detect if float variable equal zero?
Using === checks also for the datatype:
$test = round(0, 2); // float(0.00)
if($test === null) // false
if($test === 0) // false
if($test === 0.0) // true
if($test === false) // false
Use 3 equal signs rather than two to test the type as well:
if($test === 0)
If you use=== instead of == it will compare as well as get the data types errors manage...Can you post your answer while using === ? Please check the difference between this two here
When comparing values in PHP for equality you can use either the == operator or the === operator. What’s the difference between the 2? Well, it’s quite simple. The == operator just checks to see if the left and right values are equal. But, the === operator (note the extra “=”) actually checks to see if the left and right values are equal, and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).
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") {
I'm trying to check if a variable has more than one zero. It seems php treats multiple zeros as one zero. For example, the following code always returns true no matter how many zeros the variable has:
$input = 0000;//or "0000"
if($input==00) echo "true";
else echo "false";
My question is : How can I make the above code return true only if it has the exact number of zero in the if statement? Thanks
How can I make the above code return true only if it has the exact number of zero in the if statement?
Use strings.
Integers is the only data of the number, not its presentation. So 0000 equals to 0.
Currently, your $input is a decimal integer. Therefore, 0 does in fact equal 00000.
You need to define it as a string and then compare with other strings.
$input = "0000";
if( $input === "00" ) { echo "yes"; } else { echo "no"; }
Use strings instead of numbers
$input = "00000";
// This searches for 0 extra zeros on the left
if(strpos($input,"0")==0 && $input!=="0"){
echo "true";
}
else{
echo "false";
}
<?php
$input = "0000";
if(strcmp($input,"0000")==0) {echo "they match";}
else {echo "They dont";}
?>
echo "<h2 style='margin:0; padding:0;'>Recent Comments</h2>";
if ($sth7->rowCount()) {
while($row7 = $sth7->fetch(PDO::FETCH_ASSOC)) {
echo "<div class='comment'>{$row7['usr']} said";
}
}
else($sth7->rowCount() = 0)
echo "User";
Can't use method return value in write context
Why doesnt that rowcount() = 0 logic work?
= is the assignment operator in PHP.
You're basically trying to assign 0 to $sth7->rowCount().
Perhaps you mean $sth7->rowCount() == 0?
Also, you really don't need the if else if. It could be just an if else:
if($sth7->rowCount()) {
} else {
}
rowCount() returns an integer, and any integer except for 0 will cast to true.
Try rowcount() == 0 to compare with 0, your code (rowcount() = 0) tries to assign 0. Also, it's may be useful to put constant on the left side while comparing : (0 == rowcount()) to make such errors easier to detect.
else doesn't take any logic, it just runs if it's assosciated if (and any elseifs) didn't evaluate true.
Also, = is an assignment operator - read it as "becomes equal to"
$var = 1; // Var becomes equal to 1
== is an equality operator, it tests if two expressions are equal
$var == 1 // Var is equal to 1
You probably want
else if ($sth7->rowCount() == 0)
echo "User";