John Davidson

operators - Two-sided inequalities in PHP

0 comments
Message:


In PHP, is there any way to express a two-sided inequality like this:


if( 10 <= $your_price < 15 ){echo 'Bingo!';}

without having to do


if( 10 <= $your_price and $your_price < 15 ){echo 'Bingo!';}

You would think that the former is more efficient than the latter as there is some redudancy in having to write and and repeat $your_price.



My Answer:
Yes, in PHP you can express a two-sided inequality using the logical operators. For example, to check if a variable is between two values, you can use the "&&" (AND) operator to combine two separate inequalities.

For example, to check if a variable $x is between 10 and 20, you can write:

php
if ($x > 10 && $x < 20) {
// do something
}


This will only execute the code inside the if statement if $x is greater than 10 and less than 20.

Rate this post

4 of 5 based on 4333 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us