Jump to content

If... ...else Statements.


190319m9

Recommended Posts

1. Simply put, you need an L-value for every comparison. There is no shorthand, although at times I'd like there to be. Thus:($val < 5 && $val > 0)2. It's no big deal in a little routine like this, but if you find yourself calling the same function several times just to get the same value, it's more efficient to call it just once and store the value in a variable. Thus:

$val = func($arg);if  ($val < 5 && $val > 0) {   // do something} elseif  ($val < -5 && $val < 0) {   // do something else}

Link to comment
Share on other sites

  • 2 weeks later...

As said, there must always be a Left value for every comparrison. There's no shorthand.

<?php$d=date("H");if ($d>"19" && $d<"22")echo "Good Morning!";elseif ($d>"00" && $d<"04")echo "Good Afternoon!";elseecho "Enjoy, your time.";?>
Funny thing though, you can see what happens when there IS a shorthand for something (like "Left value") - people get confused.
Link to comment
Share on other sites

Just to make it crystal clear, then, in case anyone searches these hallowed archives:The term L-value is short for Left-value. The term is used with reference to the assignment and comparison operators. Left and right refer to the sides of such an operator. So in the expression a = 5, a is the left, or L-value, and 5 is the right value. (I've never used the term R-value in actual practice, but I suppose it exists.)The most common L-value error is assigning a value to a constant or to the return value of a function, such that 4 = 7 and getCheese() = 3 are illegal statements.Most modern programming languages require an L-value for every comparison operator, even when the operators are used in combination. So this is illegal (x < 10 && > 5). The legal expression is (x < 10 && x > 5).Here's a case that seems to defy the rule: a = s = d = f = g = h = 5. This expression assigns the value of 5 to all those variables. A way of understanding it is to say that s through h function as both L- and R-values.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...