There are two ways to express range in PHP: Between operator: start <= value <= end (inclusive range) Range operator: start ... end (exclusive range, only applicable to PHP 5.3 and later, and only for numeric types)
Methods for representing ranges in PHP
PHP The range of a value can be expressed in two ways:
1. Use the between operator
The syntax of the between operator (inclusive range) is:
<code class="php">start <= value <= end</code>
For example:
<code class="php">if ($age >= 18 && $age <= 65) { // 18 岁到 65 岁之间的成年人 }</code>
2. Use the range operator
The syntax of the range operator (exclusive range) is:
<code class="php">start ... end</code>
For example:
if ($age >= 18 && $age < 65) {
// 不包括 65 岁,18 岁到 64 岁之间的成年人
}
Note:
- The range operator is only available in PHP 5.3 and above.
- For floating point numbers, the range operator will return false even if the number is within the range.
- The between operator can be used with strings, numbers, and datetime objects.
- Range operators can only be used with numeric types such as integers and floating point numbers.
The above is the detailed content of How to express a range of values in php. For more information, please follow other related articles on the PHP Chinese website!