Re : henryk dot kwak at gmail dot com
<?php function message($m)
{
echo "$m <br />\r";
return true;
}
$k=false;
if (message("first")&& $k && message("second")){;}
// will show
//first
class
$k=true;
if (message("first")&& $k && message("second")){;}
// will show
//first
//second
?>
if
L'instruction if est une des plus importantes instructions de tous les langages, PHP inclus. Elle permet l'exécution conditionnelle d'une partie de code. Les fonctionnalités de l'instruction if sont les mêmes en PHP qu'en C :
Exemple #1 Instruction if ()
if (expression)
commandes
Comme nous l'avons vu dans le paragraphe consacré aux expressions, expression est convertie en sa valeur booléenne. Si l'expression vaut TRUE, PHP exécutera l'instruction et si elle vaut FALSE, l'instruction sera ignorée. Plus de détails sur les valeurs qui valent FALSE sont disponibles dans la section Conversion en booléen.
L'exemple suivant affiche la phrase a est plus grand que b si $a est plus grand que $b :
Exemple #2 Instruction if () (2)
<?php
if ($a > $b)
print "a est plus grand que b";
?>
Souvent, vous voulez que plusieurs instructions soient exécutées après un branchement conditionnel. Bien évidemment, il n'est pas obligatoire de répéter l'instruction conditionnelle autant de fois que vous avez d'instructions à exécuter. À la place, vous pouvez rassembler toutes les instructions dans un bloc. L'exemple suivant affiche a est plus grand que b, et assigne la valeur de la variable $a à la variable $b :
Exemple #3 Instruction if () et bloc
<?php
if ($a > $b) {
echo "a est plus grand que b";
$b = $a;
}
?>
Vous pouvez imbriquer indéfiniment des instructions if les unes dans les autres, ce qui permet une grande flexibilité dans l'exécution d'une partie de code suivant un grand nombre de conditions.
if
28-Sep-2008 02:03
25-Sep-2008 05:24
@henryk (and everybody):
You should put your arguments in order by *least* likely to be true. That way if php is going to be able to quit checking, it will happen sooner rather than later, and your script will run (what amounts to unnoticeably) faster.
At least, that makes the most sense to me, but I don't claim omniscience.
28-Aug-2008 04:22
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
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...
