Use and operator in php

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result Show it
+ Addition $x + $y Sum of $x and $y Try it »
Subtraction $x — $y Difference of $x and $y Try it »
* Multiplication $x * $y Product of $x and $y Try it »
/ Division $x / $y Quotient of $x and $y Try it »
% Modulus $x % $y Remainder of $x divided by $y Try it »
** Exponentiation $x ** $y Result of raising $x to the $y’th power Try it »

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is «=». It means that the left operand gets set to the value of the assignment expression on the right.

Assignment Same as. Description Show it
x = y x = y The left operand gets set to the value of the expression on the right Try it »
x += y x = x + y Addition Try it »
x -= y x = x — y Subtraction Try it »
x *= y x = x * y Multiplication Try it »
x /= y x = x / y Division Try it »
x %= y x = x % y Modulus Try it »
Читайте также:  Django channels websocket python

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result Show it
== Equal $x == $y Returns true if $x is equal to $y Try it »
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type Try it »
!= Not equal $x != $y Returns true if $x is not equal to $y Try it »
<> Not equal $x <> $y Returns true if $x is not equal to $y Try it »
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type Try it »
> Greater than $x > $y Returns true if $x is greater than $y Try it »
Less than $x < $y Returns true if $x is less than $y Try it »
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y Try it »
Less than or equal to $x Returns true if $x is less than or equal to $y Try it »
Spaceship $x $y Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. Try it »

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value.

The PHP decrement operators are used to decrement a variable’s value.

Operator Name Description Show it
++$x Pre-increment Increments $x by one, then returns $x Try it »
$x++ Post-increment Returns $x, then increments $x by one Try it »
—$x Pre-decrement Decrements $x by one, then returns $x Try it »
$x— Post-decrement Returns $x, then decrements $x by one Try it »

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Operator Name Example Result Show it
and And $x and $y True if both $x and $y are true Try it »
or Or $x or $y True if either $x or $y is true Try it »
xor Xor $x xor $y True if either $x or $y is true, but not both Try it »
&& And $x && $y True if both $x and $y are true Try it »
|| Or $x || $y True if either $x or $y is true Try it »
! Not !$x True if $x is not true Try it »

PHP String Operators

PHP has two operators that are specially designed for strings.

Operator Name Example Result Show it
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2 Try it »
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1 Try it »

PHP Array Operators

The PHP array operators are used to compare arrays.

Operator Name Example Result Show it
+ Union $x + $y Union of $x and $y Try it »
== Equality $x == $y Returns true if $x and $y have the same key/value pairs Try it »
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types Try it »
!= Inequality $x != $y Returns true if $x is not equal to $y Try it »
<> Inequality $x <> $y Returns true if $x is not equal to $y Try it »
!== Non-identity $x !== $y Returns true if $x is not identical to $y Try it »

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions:

Operator Name Example Result Show it
?: Ternary $x = expr1 ? expr2 : expr3 Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE
Try it »
?? Null coalescing $x = expr1 ?? expr2 Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7
Try it »

Источник

Use and operator in php

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

Источник

How to Use the AND Operator in PHP

The AND operator is one of the logical operators of PHP. The AND operator will turn true once both of the operands are true.

Below, we will demonstrate an example of using this operator:

 // Declare a variable and initialize it $a = 100; $b = 10; // Check the condition if ($a == 100 && pow($b, 2) == $a) < echo "True"; > else < echo "False"; > //outputs True ?>

In the example above, $a == 100 && pow($b, 2) == $a is true as the AND operator states that merely once both of the operands are true, the result will be true, as well. In case one of the conditions is not fulfilled, then the output will be false.

Sometimes the AND operator is considered the same as the && , yet these two operators have significant differences.

The similarity is these operators are true when both of the operands are true. The main difference between them is that the precedence of the AND operator is low, and it’s high for the && operator.

About Operators in PHP

The symbols, telling the PHP processor to act in a specific way, are called operators. Generally, the PHP operators are classified in the following way:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Incrementing and Decrementing operators
  • Logical operators
  • String operators
  • Array operators
  • Spaceship Operator.

Logical Operators in PHP

PHP supports standard logical operators. They work in the following way: first, they convert their operands to boolean, then implement a respective comparison.

The logical operators are && , || , xor , ! , AND , or .

These operators are mainly used for combining conditional statements.

Источник

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