downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

else> <Opérateur de type
[edit] Last updated: Mon, 01 Nov 2010

view this page in

Chapitre 16. Les structures de contrôle

Tous les scripts PHP sont une suite d'instructions. Une instruction peut être une assignation, un appel de fonction, une instruction conditionnelle ou bien une instruction qui ne fait rien (une instruction vide). Une instruction se termine habituellement par un point virgule (";"). De plus, plusieurs instructions peuvent être regroupées en bloc, délimité par des accolades ("{}"). Un bloc est considéré comme une instruction. Les différents types d'instruction sont décrits dans ce chapitre.

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 16-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 16-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 16-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.



else> <Opérateur de type
[edit] Last updated: Mon, 01 Nov 2010
 
add a note add a note User Contributed Notes Les structures de contrôle
wintermute 29-Aug-2007 12:45
Sinured: You can do the same thing with logical OR; if the first test is true, the second will never be executed.

<?PHP
if (empty($user_id) || in_array($user_id, $banned_list))
{
exit();
}
?>
Sinured 01-Aug-2007 11:59
As mentioned below, PHP stops evaluating expressions as soon as the result is clear. So a nice shortcut for if-statements is logical AND -- if the left expression is false, then the right expression can’t possibly change the result anymore, so it’s not executed.

<?php
/* defines MYAPP_DIR if not already defined */
if (!defined('MYAPP_DIR')) {
   
define('MYAPP_DIR', dirname(getcwd()));
}

/* the same */
!defined('MYAPP_DIR') && define('MYAPP_DIR', dirname(getcwd()));
?>
dougnoel 05-May-2006 06:29
Further response to Niels:

It's not laziness, it's optimization.  It saves CPUs cycles.  However, it's good to know, as it allows you to optimize your code when writing.  For example, when determining if someone has permissions to delete an object, you can do something like the following:

if ($is_admin && $has_delete_permissions)

If only an admin can have those permissions, there's no need to check for the permissions if the user is not an admin.
niels dot laukens at tijd dot com 26-Dec-2004 07:49
For the people that know C: php is lazy when evaluating expressions. That is, as soon as it knows the outcome, it'll stop processing.

<?php
if ( FALSE && some_function() )
    echo
"something";
// some_function() will not be called, since php knows that it will never have to execute the if-block
?>

This comes in nice in situations like this:
<?php
if ( file_exists($filename) && filemtime($filename) > time() )
   
do_something();
// filemtime will never give an file-not-found-error, since php will stop parsing as soon as file_exists returns FALSE
?>

 
show source | credits | sitemap | contact | advertising | mirror sites