简体   繁体   中英

What does this statement mean?

While

$w is an Array ( [0] => 4, [1] => 6 )

what does this statement mean:

$day == $w[0] || $day == $w[1] || $day < ((7 + $w[1] - $w[0]) % 7);

Please help. I have not seen the || operator inside other than an if or while statement. Thank you.

EDIT 01:

This is the original function where it is used to find the number of a particular day in a date range:

// find number of a particular day (sunday or monday or etc) within a date range
function number_of_days($day, $start, $end){
    $w = array(date('w', $start), date('w', $end));
    return floor( ( date('z', $end) - date('z', $start) ) / 7) + ($day == $w[0] || $day == $w[1] || $day < ((7 + $w[1] - $w[0]) % 7));
}

This was not created by me. But I wanted to edit this function because when the end day is a Saturday, it is taking the following Sunday into account too, which is wrong.

它只是一个复合布尔表达式,如果以下四个子表达式中的任何一个为true则返回true

  • $day == $w[0]
  • $day == $w[1]
  • $day < ((7 + $w[1] - $w[0]) % 7)

You were right in one of your comments that the boolean expression gets added as to the integer as 1 or 0 .

If you cast a boolean value to an integer then FALSE gets 0 and TRUE gets 1 .

If you add variables with different datatypes and one of the variable is an integer, then the other variables are casted to integers, which makes:

var_dump(1+true);
// Result: int(2)

Two links that explain what happens if you use + on different data types and what happens if a certain datatype is casted to an integer:

http://php.net/manual/en/language.types.type-juggling.php
http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting

$day == $w[0] || $day == $w[1] || $day < ((7 + $w[1] - $w[0]) % 7);

The statement will evaluate (nothing is assigned in the example) to a boolean value of true/false.

The statements are effectively computed in order

For example

true || false || false => true

false || false || false => false

This means that if any of the "subexpressions" are true then the whole expression will evaluate to true. This can be assigned to a variable $v = expression , or use in an if (expression)

|| is the logical OR operator. Please see the documentation for more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM