How do I use or operator in PHP? - 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 == "" ) )

Related

PHP: Evaluate string as logical expression and return true or false

I have a txt file with hundreds of logical expressions.
I want to read each one (no problem so far) and to be able to evaluate it recursively, but I can't figure a way how. The expression has && and == and comparissons between strings and numbers. I don't want to use eval, as it's not recommended apparently and it didn't work in my case.
Example. Let's say I read these 2 strings:
s = "a == alpha && b == beta || b == omega", or
s = "g >= 2 && f != gamma"
I want to break them down to
($a == "alpha" && $b == "beta" || b == "omega")
($g >= 2 && f!= "gamma")
to use them in an if, so that it returns TRUE or FALSE. My problem is not with replacing the variables, it's with making them evaluate as a logical expression
Can anybody give me a hand?
Thanks in advance,
Cristina
Try this :
if( (($a == 'alpha' && $b == 'beta') || ($b == 'omega')) || ($g >= 2 && $f != 'gamma'))
{
// returns true
}
else
{
// returns false
}

Shorthand expression for an if ( $a == $b || $a == $c ) statement

I know this code will work:
echo ( $a == $b || $a == $c ) ? "Yes" : "No";
That can be read like:
if $a is equal to $b or $a is equal to $c
Is there a way to make it more shorter like:
if $a is equal to $b or $c
I have tried a lot including this but still no luck:
echo ( $a == ( $b xor $c ) ) ? "Yes" : "No";
You can use in_array:
var_dump(in_array($a, [$b, $c]));
with your example:
echo in_array($a, [$b, $c]) ? 'Yes' : 'No';
Note: this syntax is only useful if you have more than 2 values. For few values $a == $b || $a == $c does the job well and is probably faster.
These are two alternatives, but they will both take longer to execute than the code you posted because they rely on more complex functions.
preg_match('/^('.$b.'|'.$c.')$/',$a) === 0
in_array($a,array($b,$c)) === true
If you put the condition more likely to be true as the first expression, in most cases, PHP will evaluate the expression as true and not test the second expression.

Sql rating issue

Here's my code. My problem is that I want the insertion to happen just when the rating_airlines value is between 1 and 5 but it keeps adding all the values.
rating_airlines is varchar on my database.
<?php
require_once('connect.php');
mysql_select_db($database_localhost,$con);
$nom_airlines=$_GET['nom_airlines'];
$rating_airlines=$_GET['rating_airlines'];
$a=intval($rating_airlines);
if($a=1 || $a=2 || $a=3 || $a=4 || $a=5 ) {
mysql_query("INSERT INTO Airlines(nom_airlines,rating_airlines)
VALUES ('$nom_airlines','$a') ");
echo "OK";}
else {
die('RequĂȘte invalide : ' . mysql_error());
}
?>
You're not comparing the values you are setting. Single equals sets double compares.
if($a=1 || $a=2 || $a=3 || $a=4 || $a=5 )
Should be
if($a==1 || $a==2 || $a==3 || $a==4 || $a==5 )
But could even be
if($a>=1 && $a <=5 )
You also should switch from the mysql to mysqli or PDO. mysqli or PDO - what are the pros and cons?
Also note that by passing $nom_airlines directly to your query you are open to injections at a minimum use the mysql_real_escape_string.
$nom_airlines=mysql_real_escape_string($_GET['nom_airlines']);
You are using assigning operator . You need to use equality here.
if($a==1 || $a==2 || $a==3 || $a==4 || $a==5 ) {
Take a look into these 2:
Assignment Operators in PHP
Equality Operators in PHP
Moreover,
$a = $b Assign Sets $a to be equal to $b.
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)

How do i make shortcuts for IF statements?

is there a shorthand version of checking if numerous variables have the same value please ?
instead of :-
if ($a="valid")
{do stuff;}
if ($b="valid")
{do stuff;}
if ($c="valid")
{do stuff;}
if ($d="valid")
{do stuff;}
is there something like:-
if ($a or $b or $c or $d = "valid")
{do stuff;}
Just put all of the variables you want to check into an array and use in_array() to check them all at once:
if (in_array('valid', array($a, $b, $c, $d))) {
do stuff;
}
John Conde's solution of putting together an array and then using in_array is a good solution. In case you want to stick with basic string comparisons, somewhat like in your example code, then you could do:
if ($a == 'valid' or
$b == 'valid' or
$c == 'valid' or
$d == 'valid') {
// DO STUFF
}
Something to keep in mind, based on what I noticed from your example code: When doing comparisons for equality in PHP, use == and not =. The single = is for assignment, while the double == is for comparison.
You could extract a function doing the comparison and have :
if (check($a) || check($b) ...)
It's not as sexy as John proposition (whatever his girlfriend says) I have to admit.
if ($a == 'valid' || $b == 'valid' || $c == 'valid' || $d == 'valid') { // DO STUFF }
This would be best because if first condition is true then no need to check rest.
Yes you can, like this:
if (($a || $b || $c || $d) == "valid")

How to chain php if statements

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)

Categories