And operand in php

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.

Источник

And operand 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

Источник

PHP AND Operator

Summary: in this tutorial, you’ll learn about the PHP AND operator and how to use it to build a complex logical expression.

Introduction to the PHP AND operator

The logical AND operator accepts two operands and returns true if both operands are true ; otherwise, it returns false .

PHP uses the and keyword to represent the logical AND operator:

expression1 and expression2

The following table illustrates the result of the and operator:

expression1 expression2 expression1 and expression2
true true true
true false false
false true false
false false false

Since PHP keywords are case-insensitive, the AND and and operators are the same:

expression1 AND expression2

By convention, you should use the and operator in the lowercase format.

In addition to using the and keyword, PHP uses && as the logical AND operator:

The && and and operators return the same result. The only difference between the && and and operators are their precedences.

The and operator has higher precedence than the && operator. The precedence of an operator specifies the order in which PHP evaluates.

PHP AND operator examples

Suppose that you want to offer discounts to customers who buy more than three items with a price of more than 99. To determine whether customers can get a discount or not, you can use the AND operator as follows:

 $price = 100; $qty = 5; $discounted = $qty > 3 && $price > 99; var_dump($discounted); Code language: HTML, XML (xml)
bool(true)Code language: JavaScript (javascript)

If you change the $qty to 2 , the $discounted will be false like this:

 $price = 100; $qty = 2; $discounted = $qty > 3 && $price > 99; var_dump($discounted);Code language: HTML, XML (xml)

In practice, you’ll use the logical AND operator in the if, if-else, if-elseif, while, and do-while statements.

Short-circuiting

When the value of the first operand is false , the logical AND operator knows that the result must be also false . In this case, it doesn’t evaluate the second operand. This process is called short-circuiting.

See the following example:

 $debug = false; $debug && print('PHP and operator demo!'); Code language: HTML, XML (xml)
  • First, define the variable $debug and initialize it to false .
  • Second, use the logical AND operator to combine the $debug and print() . Since $debug is false , PHP doesn’t evaluate the call to the print() function.

If you change the $debug to true , you’ll see a message in the output:

 $debug = true; $debug && print('PHP and operator demo!');Code language: HTML, XML (xml)
PHP and operator demo!Code language: plaintext (plaintext)

Summary

  • Use the PHP AND operator ( and , && ) to combine two boolean expressions and returns true if both expressions evaluate to true; otherwise, it returns false .
  • The logical AND operator is short-circuiting.

Источник

Читайте также:  Css как вставлять спецсимволы
Оцените статью