RE: chrislabricole at yahoo dot fr on 09-Aug-2008 05:53
You're referring to the ternary operator.
http://php.net/manual/en/language.operators.comparison.php
if
The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:
if (expr)
statement
As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. More information about what values evaluate to FALSE can be found in the 'Converting to boolean' section.
The following example would display a is bigger than b if $a is bigger than $b:
<?php
if ($a > $b)
echo "a is bigger than b";
?>
Often you'd want to have more than one statement to be executed conditionally. Of course, there's no need to wrap each statement with an if clause. Instead, you can group several statements into a statement group. For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:
<?php
if ($a > $b) {
echo "a is bigger than b";
$b = $a;
}
?>
If statements can be nested infinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program.
if
28-Aug-2008 04:22
14-Aug-2008 07:50
RE: henryk dot kwak at gmail dot com's comment from 04-May-2008 05:01
I think you made a mistake.
For maximum efficiency, assuming each expression requires the same amount of processing, the expression that is least likely to be true should come first for expressions connected by && (and). This will reduce the probability that later expressions will need to be evaluated.
The opposite is true for || (or). If the most likely expression comes first, then the probability of needing to evaluate later expressions is reduced.
10-Aug-2008 02:53
You can do IF with this pattern :
<?php
$var = TRUE;
echo $var==TRUE ? 'TRUE' : 'FALSE'; // get TRUE
echo $var==FALSE ? 'TRUE' : 'FALSE'; // get FALSE
?>
05-May-2008 02:01
When you use if command with many condidions like
if ( expr1 && expr2 && expr3 && etc. )
it is more effective to put expressions in special order
Firstly you should put that, which has the biggest
probability to occur.
This is because PHP checks each condition in order from left to right and it takes some time to check each condition.
10-Mar-2008 12:41
re: #80305
Again useful for newbies:
if you need to compare a variable with a value, instead of doing
<?php
if ($foo == 3) bar();
?>
do
<?php
if (3 == $foo) bar();
?>
this way, if you forget a =, it will become
<?php
if (3 = $foo) bar();
?>
and PHP will report an error.
09-Jan-2008 11:54
This is aimed at PHP beginners but many of us do this Ocasionally...
When writing an if statement that compares two values, remember not to use a single = statement.
eg:
<?php
if ($a = $b)
{
print("something");
}
?>
This will assign $a the value $b and output the statement.
To see if $a is exactly equal to $b (value not type) It should be:
<?php
if ($a == $b)
{
print("something");
}
?>
Simple stuff but it can cause havok deep in classes/functions etc...
