Php and or keywords

PHP — and / or keywords

and and or have higher lower precedence than && and || . To be more exact && and || have higher precedence than assignment operator ( = ) while and and or have lower.

Usually it doesn’t make a difference, but there are cases when not knowing about this difference can cause some unexpected behaviour. See examples here:

Solution 2

Yes, they are logically the same. (I believe «&&» and «||» are the preferred choice in the Zend coding standards, but I can’t find any specific information on this, so it might all have been a dream. Or something.)

  1. «&&» and «||» are of a higher precedence than «AND» and «OR» (unlikely to ever be relevant, but you never know).
  2. A lot of other languages use «&&» and «||», rather than the textual equivalents so it might be an idea to go with this.
  3. As long as you use your choosen set of operators consistently it doesn’t really matter.

Solution 3

echo (false and false ? true : true); // (empty/false) 

You might guess there is only the possible output of «1» (true) as there is no case which could output a false. . but it will be «» (false).

Using && as a operator in this case satifies at least my expectations:

So, in some cases the usage matters significantly.

Solution 4

The difference is on the precedence. But not only compared with each other!

Читайте также:  How to Check if element has class in JavaScript

In most cases you won’t mind it, but there are specific cases when you have to take one step back and look at the big picture. Take this, for example:

// The result of the expression (true && false) is assigned to $g // Acts like: ($g = (true && false)) $g = true && false; // The constant true is assigned to $h before the "and" operation occurs // Acts like: (($h = true) and false) $h = true and false; var_dump($g, $h); 

This will produce, respectively:

Источник

PHP — and / or keywords

To be more exact and have higher precedence than assignment operator ( ) while and have lower. ` But if you have always a set of consecutive days why don’t you do the following for the date part Solution 3: Your question is about the operator precedences in mysql and Alex has shown you how to «override» the precedence with parentheses.

PHP — and / or keywords

and and or have higher lower precedence than && and || . To be more exact && and || have higher precedence than assignment operator ( = ) while and and or have lower.

Usually it doesn’t make a difference, but there are cases when not knowing about this difference can cause some unexpected behaviour. See examples here:

Yes, they are logically the same. (I believe «&&» and «||» are the preferred choice in the Zend coding standards, but I can’t find any specific information on this, so it might all have been a dream. Or something.)

  1. «&&» and «||» are of a higher precedence than «AND» and «OR» (unlikely to ever be relevant, but you never know).
  2. A lot of other languages use «&&» and «||», rather than the textual equivalents so it might be an idea to go with this.
  3. As long as you use your choosen set of operators consistently it doesn’t really matter.
echo (false and false ? true : true); // (empty/false) 

You might guess there is only the possible output of «1» (true) as there is no case which could output a false. . but it will be «» (false).

Using && as a operator in this case satifies at least my expectations:

So, in some cases the usage matters significantly.

PHP combining boolean operators (AND, OR) in an if, PHP combining boolean operators (AND, OR) in an if statement. i wonder if its possible to combine both operators (OR and AND) in one if statement like this. What i want to state is: if apple equals 1 AND orange equals 2, OR cake equals 0 then do this. In other words, i need apple and orange to equal the …

MySQL «Or» Condition

Use brackets to group the OR statements.

mysql_query("SELECT * FROM Drinks WHERE email='$Email' AND (date='$Date_Today' OR date='$Date_Yesterday' OR date='$Date_TwoDaysAgo' OR date='$Date_ThreeDaysAgo' OR date='$Date_FourDaysAgo' OR date='$Date_FiveDaysAgo' OR date='$Date_SixDaysAgo' OR date='$Date_SevenDaysAgo')"); 
mysql_query("SELECT * FROM Drinks WHERE email='$Email' AND date IN ('$Date_Today','$Date_Yesterday','$Date_TwoDaysAgo','$Date_ThreeDaysAgo','$Date_FourDaysAgo','$Date_FiveDaysAgo','$Date_SixDaysAgo','$Date_SevenDaysAgo')"); 
mysql_query("SELECT * FROM Drinks WHERE email='$Email' AND (date='$Date_Today' OR date='$Date_Yesterday' OR date='$Date_TwoDaysAgo' OR date='$Date_ThreeDaysAgo' OR date='$Date_FourDaysAgo' OR date='$Date_FiveDaysAgo' OR date='$Date_SixDaysAgo' OR date='$Date_SevenDaysAgo' ) "); 

But you should alsos have a look at the IN operator. So you can say ´date IN (‘$date1′,’$date2’. )`

But if you have always a set of consecutive days why don’t you do the following for the date part

Your question is about the operator precedences in mysql and Alex has shown you how to «override» the precedence with parentheses.

But on a side note, if your column date is of the type Date you can use MySQL’s date and time functions to fetch the records of the last seven days, like e.g.

SELECT * FROM Drinks WHERE email='$Email' AND date >= Now()-Interval 7 day 

(or maybe Curdate() instead of Now())

Conditional Statements in PHP code Between HTML Code, I’m having a bit of an issue by using Conditional Statements in PHP separated by HTML code. This is the type of code I’m trying to write. This is a profile page and it should only be seen by the user . But I’ve seen PHP get confused over single-line blocks when you’re switching between PHP code and …

Cakephp OR condition

'Company' => array ( 'conditions' => array ( 'OR' => array( array('Company.status' => 0), array('Company.status' => $status), ) ) ) 

In the cookbook it says to wrap the or conditions in arrays if they are pertaining to the same field http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

I’m not sure to have understood what results you expect. If you want to retrieve all records having status = 0, plus let’s say the one having status = 3, you could use an ‘IN’ instead of an ‘OR’.

In Cake, you would write it like this:

$status = 3; $conditions = array('Company.status' => array(0, $status)); 

You can also fetch record by using following method: put values in an array e.g. $arr=array(1,2);

 $res = $this->Model->find('all', array( 'conditions' =>array('Model.filedname'=>$arr), 'model.id' => 'desc' )); 

I hope you will find answer.

If statement — php echo if two conditions are true, Browse other questions tagged php if-statement echo file-exists or ask your own question. The Overflow Blog A conversation with Spencer Kimball, creator of GIMP and CockroachDB (Ep. 472)

Источник

Php and or keywords

worth reading for people learning about php and programming: (adding extras to get highlighted code)

about the following example in this page manual:
Example#1 Logical operators illustrated

.
// «||» has a greater precedence than «or»
$e = false || true ; // $e will be assigned to (false || true) which is true
$f = false or true ; // $f will be assigned to false
var_dump ( $e , $f );

// «&&» has a greater precedence than «and»
$g = true && false ; // $g will be assigned to (true && false) which is false
$h = true and false ; // $h will be assigned to true
var_dump ( $g , $h );
?>
_______________________________________________end of my quote.

If necessary, I wanted to give further explanation on this and say that when we write:
$f = false or true; // $f will be assigned to false
the explanation:

«||» has a greater precedence than «or»

its true. But a more acurate one would be

«||» has greater precedence than «or» and than «=», whereas «or» doesnt have greater precedence than » default»>$f = false or true ;

If you find it hard to remember operators precedence you can always use parenthesys — «(» and «)». And even if you get to learn it remember that being a good programmer is not showing you can do code with fewer words. The point of being a good programmer is writting code that is easy to understand (comment your code when necessary!), easy to maintain and with high efficiency, among other things.

Evaluation of logical expressions is stopped as soon as the result is known.
If you don’t want this, you can replace the and-operator by min() and the or-operator by max().

c ( a ( false ) and b ( true ) ); // Output: Expression false.
c ( min ( a ( false ), b ( true ) ) ); // Output: Expression is false.

c ( a ( true ) or b ( true ) ); // Output: Expression true.
c ( max ( a ( true ), b ( true ) ) ); // Output: Expression is true.
?>

This way, values aren’t automaticaly converted to boolean like it would be done when using and or or. Therefore, if you aren’t sure the values are already boolean, you have to convert them ‘by hand’:

c ( min ( (bool) a ( false ), (bool) b ( true ) ) );
?>

This works similar to javascripts short-curcuit assignments and setting defaults. (e.g. var a = getParm() || ‘a default’;)

( $a = $_GET [ ‘var’ ]) || ( $a = ‘a default’ );

?>

$a gets assigned $_GET[‘var’] if there’s anything in it or it will fallback to ‘a default’
Parentheses are required, otherwise you’ll end up with $a being a boolean.

> > your_function () or return «whatever» ;
> ?>

doesn’t work because return is not an expression, it’s a statement. if return was a function it’d work fine. :/

This has been mentioned before, but just in case you missed it:

//If you’re trying to gat ‘Jack’ from:
$jack = false or ‘Jack’ ;

// Try:
$jack = false or $jack = ‘Jack’ ;

//The other option is:
$jack = false ? false : ‘Jack’ ;
?>

$test = true and false; —> $test === true
$test = (true and false); —> $test === false
$test = true && false; —> $test === false

NOTE: this is due to the first line actually being

due to «&&» having a higher precedence than «=» while «and» has a lower one

If you want to use the ‘||’ operator to set a default value, like this:

$a = $fruit || ‘apple’ ; //if $fruit evaluates to FALSE, then $a will be set to TRUE (because (bool)’apple’ == TRUE)
?>

instead, you have to use the ‘?:’ operator:

$a = ( $fruit ? $fruit : ‘apple’ ); //if $fruit evaluates to FALSE, then $a will be set to ‘apple’
?>

But $fruit will be evaluated twice, which is not desirable. For example fruit() will be called twice:
function fruit ( $confirm ) if( $confirm )
return ‘banana’ ;
>
$a = ( fruit ( 1 ) ? fruit ( 1 ) : ‘apple’ ); //fruit() will be called twice!
?>

But since «since PHP 5.3, it is possible to leave out the middle part of the ternary operator» (http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary), now you can code like this:

$a = ( $fruit ? : ‘apple’ ); //this will evaluate $fruit only once, and if it evaluates to FALSE, then $a will be set to ‘apple’
?>

But remember that a non-empty string ‘0’ evaluates to FALSE!

$fruit = ‘1’ ;
$a = ( $fruit ? : ‘apple’ ); //this line will set $a to ‘1’
$fruit = ‘0’ ;
$a = ( $fruit ? : ‘apple’ ); //this line will set $a to ‘apple’, not ‘0’!
?>

To assign default value in variable assignation, the simpliest solution to me is:

$v = my_function () or $v = «default» ;
?>

It works because, first, $v is assigned the return value from my_function(), then this value is evaluated as a part of a logical operation:
* if the left side is false, null, 0, or an empty string, the right side must be evaluated and, again, because ‘or’ has low precedence, $v is assigned the string «default»
* if the left side is none of the previously mentioned values, the logical operation ends and $v keeps the return value from my_function()

This is almost the same as the solution from [phpnet at zc dot webhop dot net], except that his solution (parenthesis and double pipe) doesn’t take advantage of the «or» low precedence.

NOTE: «» (the empty string) is evaluated as a FALSE logical operand, so make sure that the empty string is not an acceptable value from my_function(). If you need to consider the empty string as an acceptable return value, you must go the classical «if» way.

In PHP, the || operator only ever returns a boolean. For a chainable assignment operator, use the ?: «Elvis» operator.

JavaScript:
let a = false;
let b = false;
let c = true;
let d = false;
let e = a || b || c || d;
// e === c

$a = false ;
$b = false ;
$c = true ;
$d = false ;
$e = $a ?: $b ?: $c ?: $d ;
// $e === $c
?>

Credit to @egst and others for the insight. This is merely a rewording for (formerly) lost JavaScript devs like myself.

$res |= true ;
var_dump ( $res );
?>

does not/no longer returns a boolean (php 5.6) instead it returns int 0 or 1

Источник

Оцените статью