How to chain php if statements - php

I am trying to chain a number of if statements using && and it doesnt seem to be working correctly. This is my code.
if ($a == "1" && $b >= "10")
{ echo "1-3"; }
if ($a == "1" && $b <= "9")
{ echo "0-1"; }
if ($a == "2" && $b >= "10")
{ echo "2-3"; }
if ($a == "2" && $b <= "9")
{ echo "0-1"; }
if ($a == "3" && $b >= "10")
{ echo "2-4"; }
if ($a == "3" && $b <= "9")
{ echo "1-2"; }

You can compare strings, but it won't work the way you think. Comparing strings that way will only compare them based on the characters ASCII codes. (Thanks Rocket)
I assume that you want to compare the values of the numbers, therefore you should remove the quotes surround the numbers.
String: $b = "1";
Integer: $b = 1; (no quotes)
You might also want to use else if in your if statements here.

You cannot compare a String with > or <. Leave out the "
(example:
if ($a == "1" && $b >= 10)
{ echo "1-3"; }

You should take intval($a) and intval($b) if you are looking for numerical comparison.
You should compare objects in the same domain: intval($a) === 1 is strict, while intval($a) == "1" is typeless. And besides, you shouldn't compare a number to a string. As I said, both sides of the comparison should reside within the same domain.

The issue here is you are comparing a string to an int. When this occurs, the int is converted to a string, and then the strings are compared based on their characters' ASCII codes.
You should make sure $a and $b are ints by using intval and then compare them to ints.
$a = intval($a);
$b = intval($b);
if ($a == 1 && $b >= 10)

Related

How do I use or operator in PHP?

if($a=="" and $b=="" or $c=="") {
echo "something";
}
I want to ask that if $a AND $b is empty then it will print: something. But if $b is not empty, then it will check the statement like this e.g. if $a is empty AND $c is empty then print: something (Means $a is compulsory to check with the variable which is empty $b OR $c)
See the PHP Operator Precedence table. and has higher precedence than or, so your condition is treated as if you'd written
if (($a == "" and $b == "") or ($c == ""))
Since you want the $a check to be independent, you need to use parentheses to force different grouping:
if ($a == "" and ($b == "" or $c == ""))
Note that in expressions, it's conventional to use && and || rather than and and or -- the latter are usually used in assignments as a control structure, because they have lower precedence than assignments. E.g.
$result = mysqli_query($conn, $sql) or die (mysqli_error($conn));
See 'AND' vs '&&' as operator
I suppose you need something like this:
if (
($a == '')
&&
(
($b == '')
||
($c == '')
)
)
so, it will print something only when $a is empty and either $b or $c empty
Just force the comparison order/precedence by using parentheses:
if( $a == "" and ( $b == "" or $c == "" ) )
But you would be better off using && and ||:
if( $a == "" && ( $b == "" || $c == "" ) )

PHP - Is operator order LTR or RTL?

If I have a conditional statement
if (A > B || B > C)
Which statement is going to be evaluated first: "A > B" or "B > C"?
Does same order is applied to math statements:
$var = $value1 + $value2 + $value3;
Thanks,
Alex.
In PHP the script is evaluated from left to right unless parenthesis are used, if they are used it evalutes then in logical order. In addition please remember that no code in the if condition block(including evaluators) are ran past the first failing statement. This example will only execute the second echo $a and it's value will be 0
$a = 0;
if(1 == 0 && $a = 5)
{
echo $a;
}
echo $a;
This statement will have $a value of 5 and will execute the statement. Interestingly, the reason that the code will execute is because the $a = 5 assignment in the if sets $a = 5 or " 5 = 5".
if(1 == 1 && $a = 5)
{
echo $a;
}
Also note there are else and else if statements if you have not looked into it
$a =2
if($a == 2)
{
}
else if($a > 2){
echo ">".$a;
}
else{
echo "its none of the conditions";
}
The reason that you use two equals signs is to compare the value type insensitive vs one equal which would be assigning the value. There is also three equals which would compare the type and value example This would evaluate to true :
$a = 2;
if($a == "2")
The following would not be true because you are comparing a String to integer.
$a = 2;
if($a === "2")
Regarding your second questions the same is true of String operators but your syntax is INVALID.
This Example Would say Hellow World:
echo "hellow"."world";
This Example IS NOT DOING CONCATENATION(Though it would do addition if they are integers)
echo "hellow" + "world";

"AND &" operator work fine but "OR |" and Either-OR "^" always true

I am using logical operators to test variables but AND & operator work fine but OR | and Either-OR ^ always true.
Why?
$a = 6;
$b = 6;
if ($a OR $b == 3) {
echo 'true <br />';
}
else {
echo 'false <br />';
}
The issue is with your syntax.
You need to look at the expression separately.
if($a) OR if($b == 3)
is what you're doing.
What you want is:
if($a == 3 || $b == 3)
If you look at $a by itself, any value except for 0 will return true making the entire equation true thanks to the OR
Because you have to OR to Boolean results - you're reading it too much like English.
if ($a == 3 || $b == 3)
rather than
if ($a OR $b == 3)
It's a matter of precedence - see http://php.net/manual/en/language.operators.precedence.php for more details here.
Both the other answers give you the code you need.
$a = true
$b = true
if($a and $b) TRUE if both $a and $b are TRUE.
Reference: http://php.net/manual/en/language.operators.logical.php

Conversion vs different comparison

If I have a variable which is given a string that could also be a number, as so:
$a = "1";
If I want to check if it is indeed equal to 1, is there any functional difference between
if((int)$a == 1) {
whatever();
}
and
if($a == "1") {
whatever();
}
Here I am thinking about PHP, but answers about other languages will be welcome.
$a = "1"; // $a is a string
$a = 1; // $a is an integer
1st one
if((int)$a == 1) {
//int == int
whatever();
}
2nd one
if($a == "1") {
//string == string
whatever();
}
But if you do
$a = "1"; // $a is a string
if($a == 1) {
//string & int
//here $a will be automatically cast into int by php
whatever();
}
In the first case you need to convert and then compare while in the second you only compare values. In that sense the second solutions seems better as it avoids unnecessary operations. Also second version is safer as it is possible that $a can not be casted to int.
since your question also asks about other languages you might be looking for more general info, in which case dont forget about triple equals. which is really really equal to something.
$a = "1";
if ($a===1) {
// never gonna happen
}
$b = 1;
if ($b === 1) {
// yep you're a number and you're equal to 1
}

can === and != be used interchangably?

Can the relational operator === (used for identical) be used interchangeably with the != operator" and get the same results? Or will I eventually run into issues later down the road when I do larger programs?
I know I will get the same results in the example below, will this always be true?
//example 1
<?php
$a = 1; //integer
$b = '1'; //string
if ($a === $b) {
echo 'Values and types are same';
}
else {
echo 'Values and types are not same';
}
?>
// example 2
<?php
$a = 1; //integer
$b = '1'; //string
if ($a != $b) {
echo 'Values and types are not same';
}
else {
echo 'Values and types are same';
}
?>
Short answer is, no, you can't interchange them because they check for different things. They are not equivalent operators.
You'll want to use !==
It basically means both values being compared must be of the same type.
When you use ==, the values being compared are typecast if needed.
As you know, === checks the types also.
When you use !=, the values are also typecast, whereas !== checks the values and type, strictly.
You're essentially asking whether !($a != $b) will always be identical to $a === $b. The simple answer is: no. !($a != $b) can be boiled down to $a == $b, which is obviously not the same as $a === $b:
php > var_dump(!('0' != 0));
bool(true)
php > var_dump('0' === 0);
bool(false)
!== is obviously the opposite of ===, so !($a !== $b) will always be identical to $a === $b.

Categories