Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box
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. add a note

User Contributed Notes 7 notes

robk
10 years ago
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 ?>
techguy14 at gmail dot com
12 years ago
You can have 'nested' if statements withing a single if statement, using additional parenthesis.
For example, instead of having:

<?php
if( $a == 1 || $a == 2 ) {
if(
$b == 3 || $b == 4 ) {
if(
$c == 5 || $ d == 6 ) {
//Do something here.
}
}
}
?>

You could just simply do this:

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

Hope this helps!
Christian L.
12 years ago
An other way for controls is the ternary operator (see Comparison Operators) that can be used as follows:

<?php
$v
= 1 ;

$r = ( 1 == $v ) ? 'Yes' : 'No' ; // $r is set to 'Yes'
$r = ( 3 == $v ) ? 'Yes' : 'No' ; // $r is set to 'No'

echo ( 1 == $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.
cole dot trumbo at nospamthnx dot gmail dot com
6 years ago
Any variables defined inside the if block will be available outside the block. Remember that the if doesn't have its own scope.

<?php
$bool
= true ;
if (
$bool ) {
$hi = 'Hello to all people!' ;
}
echo
$hi ;
?>

It will print 'Hello to all people!'

On the other hand, this will have no output:

<?php
if ( false ) {
$hi = 'Hello to all people!' ;
}
echo
$hi ;
?>
chiel at comfitech dot nl
7 months ago
Note that the IF statement does not always evaluates all expressions. In the next example the second expression will not be evaluated causing our function not to run. Because the result is already TRUE and the rest of the expressions are behind the OR operator they will be ignored.

<?php

$a
= 0 ;

if (
true || $a = important_function () ) echo $a ;

function
important_function (){
// Do very important stuff here.
return 1 ;
}

?>