开发者

php AND question

The following code does not give any error. But I am buffled with the second part.

  ..........
 // Get meta data for the module
$this->template->module_details = $this->module_details =     
$this->module_m->get($this->module);

// If the module is disabled, then show a 404.
empty($this->module_details['enabled']) AND show_404();
..........

For me the second part should be something like this.

if(empty($this->module_details['enabled']){
show_404();
}

Q1. Is it the same? What's going on with AND?

Q2. Can you put = twice as in the first part? Why do you need to do it? (A=B=C)

开发者_运维知识库Thanks in advance.


Q1: Every boolean operator acts "lazy". That means, if the expression definitly resolves to true, or false, it will not evaluate the following (sub)expression

true or doSomething();
false and doeSomething();

In both cases doSomething() is not executed, because the boolean expression is already known to be true/false. The other way round

true and doSomething();
false or doSomething();

here doSomething() is always executed, because the result of the (complete) expression is unknown until its executed.

So you are right, that show_404() is executed, if the part with empty() evaluates to true, what results in the if-statement you've given.

Q2: In PHP every operator has a return value. In case of = (the assignment) the return value is simply the value, that is assigned.

$a = $b = 1;
// =>
$a = ($b = 1); // where ($b = 1) === 1

Nobody really needs it, but sometimes its useful, because its more obvious, that $a and $b has the same value at this point.


It shouldn't confuse you too much, you've probably seen the alternates, but just haven't connected it.

Remember

mysql_query($sql) OR die(" :( "); //false or die('died');

This means, do the query OR die!

empty($this->module_details['enabled']) AND show_404();

Here it's the opposite.

If it's empty, show 404; //true and die('died');

You can use || instead of OR just like in other conditions, it's just that the operator precedence is different.

As for your first question.

It just means that we're setting the same values for a, b and c;

$a = $b = $c = 0; //setting all to 0;


Question One

The and is used as a logical short circuit.

In order for the second part of the expression to be executed the first part needs to be true. This essentially forms a condition of the LHS holding true for the RHS to be executed -- a useful, if sometimes unreadable syntactic shortcut.

If the LHS is false then the interpreter knows it doesn't need to bother checking the RHS because the entire statement will evaluate to false anyway since (false && (true || false) === false).

Question Two

An assignment always returns the value of the RHS.

You could just as easily break it down to:

$this->module_details = $this->module_m->get($this->module);
$this->template->module_details = $this->module_details;   

..or adding brackets makes it a little more obvious:

$this->template->module_details = 
    ($this->module_details = $this->module_m->get($this->module));

As an example, evaluating the RHS of assignment is commonly used in while loops, e.g.:

while ($row = mysql_fetch_assoc($query)) {}

This works because mysql_fetch_assoc returns false on no results and therefore ($row = false) evaluates to false and stops the while loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜