php mulup,逻辑运算符 - [ php中文手册 ] - 在线原生手册 - php中文网

用户评论:

[#1]

faffaf at gmail dot com [2015-11-01 12:51:04]

afafa;fkafa

afjaljfalsfjaljf

alfjalfjalfjal

[#2]

4077 [2015-05-04 18:20:36]

Assign a value to a variable if it isn't defined

print$x;// 123?>

instead of:

}// or$x= isset($x) ?$x:123;// or$x= isset($x) ?:123;?>

editor's note: In PHP 7 you could use the coalesce operator and do:

$x = $x ?? 123;

[#3]

Anonymous [2015-03-30 13:05:21]

you can also use the operator "xor" using "^"

[#4]

samantha at adrichem dot nu [2015-02-27 15:28:16]

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

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

[#5]

Jake [2014-11-10 03:13:05]

A gotcha for C/C++ programmers here...

The && and || operators behave as expected.  PHP also has a Boolean 'xor' operator which would fulfill many dreams.

But you may find it not working as expected, as it has *lower* precedence than assignment.  (Both languages ought to have ^^ as Boolean XOR; that's another story.)

[#6]

anatoliy at ukhvanovy dot name [2014-06-12 13:41:24]

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:

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'!?>

[#7]

tekreme73 at hotmail dot fr [2014-05-07 03:08:38]

// Operators &= and |= work as well

// "a &= b"  <=>  "a = a && b" <=> "a = a AND b"

// "a |= b"  <=>  "a = a || b" <=> "a = a OR b"

$res = true;

var_dump($res);

$res &= false;

var_dump($res);

$res |= true;

var_dump($res);

// This code will display :

bool(true)

bool(false)

bool(true)

[#8]

void at informance dot info [2014-04-16 22:53:40]

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.

[#9]

brian at zickzickzick dot com [2013-07-19 15:23:08]

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

//If you're trying to gat 'Jack' from:$jack=falseor'Jack';// Try:$jack=falseor$jack='Jack';//The other option is:$jack=false?false:'Jack';?>

[#10]

dartello at gmail dot com [2012-12-12 14:35:44]

Unlike in C/C++ the invertor (Not) in PHP assumes a string:

$a=1;$t= !$a;var_dump($t);?>

The above example will output:

string(0) => ""

To approach the C/C++ handling, this can be solved as follows:

$a=1;

(int)$t= !$a;var_dump($t);

(bool)$u= !a;var_dump($u);?>

The above example will output:

int(0)

bool(false)

[#11]

phpnet at zc dot webhop dot net [2012-12-07 15:07:06]

This works similar to javascripts short-curcuit assignments and setting defaults. (e.g.  var a = getParm() || '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.

[#12]

momrom at freenet dot de [2009-04-19 08:32:43]

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

functionb($x) { echo'is '; return$x; }

functionc($x) { echo$x?'true.':'false.';}c(a(false) andb(true) );// Output: Expression false.c(min(a(false),b(true) ) );// Output: Expression is false.c(a(true) orb(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) ) );?>

[#13]

pepesantillan at gmail dot com [2007-12-23 15:23:20]

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

about the following example in this page manual:

Example#1 Logical operators illustrated

...

_______________________________________________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 "=", so

$f=falseortrue;//is like writting($f=false) ortrue;//and$e=false||true;is the sameas$e= (false||true);?>

same goes for "&&" and "AND".

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.

[#14]

paranoiq at centrum dot cz [2007-11-19 08:00:01]

and, or and xor can be used as conditional constructs:

}?>

for understanding what happens if $b is NULL or do_this() returns NULL, read the avbentem's comment on NULL type. generaly speaking, NULL is threated like false in most cases.

[#15]

peter dot kutak at NOSPAM dot gmail dot com [2007-10-01 12:36:00]

$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

($test = true) and false;

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

[#16]

Lawrence [2007-08-28 12:04:13]

Note that PHP's boolean operators *always* return a boolean value... as opposed to other languages that return the value of the last evaluated expression.

For example:

$a = 0 || 'avacado';

print "A: $a\n";

will print:

A: 1

in PHP -- as opposed to printing "A: avacado" as it would in a language like Perl or JavaScript.

This means you can't use the '||' operator to set a default value:

$a = $fruit || 'apple';

instead, you have to use the '?:' operator:

$a = ($fruit ? $fruit : 'apple');

[#17]

Andrew [2007-08-13 08:49:48]

> <?php>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. :/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值