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 indefinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program.

Коментарии

re: #80305

Again useful for newbies:

if you need to compare a variable with a value, instead of doing

<?php
if ($foo == 3bar();
?>

do

<?php
if (== $foobar();
?>

this way, if you forget a =, it will become

<?php
if ($foobar();
?>

and PHP will report an error.
2008-03-10 05:41:06
http://php5.kiev.ua/manual/ru/control-structures.if.html
You can have 'nested' if statements withing a single if statement, using additional parenthesis.
For example, instead of having:

<?php
if( $a == || $a == ) {
    if( 
$b == || $b == ) {
        if( 
$c == || $ == ) {
             
//Do something here.
       
}
    }
}
?>

You could just simply do this:

<?php
if( ($a==|| $a==2) && ($b==|| $b==4) && ($c==|| $c==6) ) {
   
//do that something here.
}
?>

Hope this helps!
2011-01-06 15:39:11
http://php5.kiev.ua/manual/ru/control-structures.if.html
Автор:
An other way for controls is the ternary operator (see Comparison Operators) that can be used as follows:

<?php
$v 
1;

$r = (== $v) ? 'Yes' 'No'// $r is set to 'Yes'
$r = (== $v) ? 'Yes' 'No'// $r is set to 'No'

echo (== $v) ? 'Yes' 'No'// 'Yes' will be printed

// and since PHP 5.3
$v 'My Value';
$r = ($v) ?: 'No Value'// $r is set to 'My Value' because $v is evaluated to TRUE

$v '';
echo (
$v) ?: 'No Value'// 'No Value' will be printed because $v is evaluated to FALSE
?>

Parentheses can be left out in all examples above.
2011-01-25 12:58:14
http://php5.kiev.ua/manual/ru/control-structures.if.html
Автор:
easy way to execute conditional html / javascript / css / other language code with php if else:

<?php if (condition): ?>

html code to run if condition is true

<?php else: ?>

html code to run if condition is false

<?php endif ?>
2013-05-20 04:02:05
http://php5.kiev.ua/manual/ru/control-structures.if.html

    Поддержать сайт на родительском проекте КГБ