Voir ce complet film If.... regarder en ligne avec sous-titres anglais bonne qualite

(PHP 4, PHP 5, PHP 7)

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:

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.

User Contributed Notes 17 notes

As an added note to the guy below, in such a case, use the !== operator like this.

$nkey = array_search($needle, $haystack);

The !== and the === compare the "types". So, with this type of comparision, 0 is not the same as the FALSE returned by the array_search array when it can not find a match. )

if ($nkey = array_search($needle, $haystack)) <.

if the returned key is actually the key 0, then the if won't be executed
===================================

When using if statements without the curly braces, remember than only one statement will be executed as part of that condition. If you want to place multiple statements you must use curly braces, and not just put them on the same line.

if ( 1 == 0 ) echo "Test 1." ; echo "Test 2" ;

Whereas some people would expect nothing to be displayed, this piece of code will show: "Test 2".

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.

Note that safe type checking (using === and !== instead of == and !=) is in general somewhat faster. When you're using non-safe type checking and a conversion is really needed for checking, safe type checking is considerably faster.

===================================
Test (100,000,000 runs):
<?php
$start = microtime ( true );
for( $i = 0 ; $i < 100000000 ; $i ++)
if( 5 == 10 ) <>
$end = microtime ( true );
echo "1: " .( $end - $start ). "<br /> " ;
unset( $start. $end );

$start = microtime ( true );
for( $i = 0 ; $i < 100000000 ; $i ++)
if( 'foobar' == 10 ) <>
$end = microtime ( true );
echo "2: " .( $end - $start ). "<br /> " ;
unset( $start. $end );

$start = microtime ( true );
for( $i = 0 ; $i < 100000000 ; $i ++)
if( 5 === 10 ) <>
$end = microtime ( true );
echo "3: " .( $end - $start ). "<br /> " ;
unset( $start. $end );

$start = microtime ( true );
for( $i = 0 ; $i < 100000000 ; $i ++)
if( 'foobar' === 10 ) <>
$end = microtime ( true );
echo "4: " .( $end - $start ). "<br /> " ;
unset( $start. $end );
?>

===================================
Result (depending on hardware configuration):
1: 16.779544115067
2: 21.305675029755
3: 16.345532178879
4: 15.991420030594