difference between <>, =!, ==! in php - php

Hi I am doing a conditional
if ($row['ConsignadaCaja'] === 'si' && $row['Estado'] ==! 'I') {
$pago = 0;
}
It does not work as I want
So I try using
if ($row['ConsignadaCaja'] === 'si' && $row['Estado'] =! 'I') {
$pago = 0;
}
But it does not work either
Finally I try this:
if ($row['ConsignadaCaja'] === 'si' && $row['Estado'] <> 'I') {
$pago = 0;
}
It is works but do not why?

==! is not the operator you are thinking of - You are mixing two operators here.
== checks for equality, and ! is a logical not operation. So, you are actually performing one of these, thanks to operator precedence putting ! higher than the comparison or assignment operators:
if( $row['Estado'] = (!'I')) // Assigns the inverted value of 'I' to $row['Estado']
if( $row['Estado'] == (!'I')) // Compares the inverted value of 'I' to $row['Estado']
Instead, you should be using != or !==, depending on if you want type-coercion to occur.
Note that if you see that <> is working as expected, this is identical to the != operator.

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
}

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 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 == "" ) )

How can i detect if (float)0 == 0 or null in PHP

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.).

What is the difference between the OR and || operator in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Logical Operators, || or OR?
I've always thought that OR is another way of writing the || operator in PHP. The only way I prefer using OR over || is that it makes the code easier to read since || can be confused with II or 11 or whatever...
One day I stumbled upon this thing though:
<?php
$a = 'string_b';
$active = ($a == 'string_a') OR
($a == 'string_b') OR
($a == 'string_c');
var_dump($active); // Prints FALSE;
?>
Why is this happening?
The only difference is operator priority, see Operator precedence. || has a higher priority than OR.
By the way, var_dump($a) returns null but prints the right thing, string_b.
But, var_dump($active) will indeed produce an unexpected result, false.
In fact, = has higher priority than or, so your code is equivalent to:
($active = ($a == 'string_a')) OR ($a == 'string_b') OR ($a == 'string_c');
It first assigns false to active, then execute the right part of the first OR.
It's the same. But || has higher precedence than OR
http://php.net/manual/en/language.operators.precedence.php
= has a higher precedence than OR. So, $active = ($a == 'string_a') is evaluated first, which is false. Enclose the entire right hand side in it's own set of brackets and you'll get the result you were expecting.
<?php
$a = 'string_b';
$active = (
($a == 'string_a') OR
($a == 'string_b') OR
($a == 'string_c')
);
var_dump($active); // Prints TRUE;
?>

Categories