if

(PHP 4, PHP 5, PHP 7, PHP 8)

if 结构是很多语言包括 PHP 在内最重要的特性之一,它允许按照条件执行代码片段。PHP 的 if 结构和 C 语言相似:

<?php
if (expr)
  statement
?>

如同在表达式一章中定义的,expr 按照布尔求值。如果 expr 的值为 true,PHP 将执行 statement,如果值为 false ——将忽略 statement。有关哪些值被视为 false 的更多信息参见转换为布尔值一节。

如果 $a 大于 $b,则以下例子将显示 a is bigger than b

<?php
if ($a $b)
  echo 
"a is bigger than b";
?>

经常需要按照条件执行不止一条语句,当然并不需要给每条语句都加上一个 if 子句。可以将这些语句放入语句组中。例如,如果 $a 大于 $b,以下代码将显示 a is bigger than b 并且将 $a 的值赋给 $b

<?php
if ($a $b) {
  echo 
"a is bigger than b";
  
$b $a;
}
?>

if 语句可以无限层地嵌套在其它 if 语句中,这给程序的不同部分的条件执行提供了充分的弹性。

User Contributed Notes

Lazik 08-Sep-2021 06:41
<?php

$var
= null ?? true;
var_dump($var); //bool(true)

$var = 0 ?? true;
var_dump($var); //int(0)

$var = false ?? true;
var_dump($var); //bool(false)

$var = 'PHP' ?? true;
var_dump($var); //string(3) "PHP"

?>
mparsa1372 at gmail dot com 11-Mar-2021 06:05
<?php

//Print the day or night message by if command

$t = date("H");

if (
$t < "20") {
  echo
"Have a good day !";
    if (
$t > "20") {
    echo
"Have a good Night!";
    }
  }

?>
kemalcelikturk at gmail dot com 30-Jan-2021 12:38
Normal if example;

if($value["cargostatus"]==0):

     echo 'In the Procurement Process';

 elseif ($value["cargostatus"]==1):

     echo 'In packaging';

 elseif ($value["cargostatus"]==2):

     echo 'In cargo';

 else:
     echo 'not specified';
 endif;

Multiple comparison operator example in a single line;

($value["cargostatus"] == "0") ? 'In the Procurement Process' : (($value["cargostatus"] == "1") ? 'In packaging
' : (($value["cargostatus"] == "2") ? 'In cargo' : 'not specified'))

What should not be forgotten;
After the first query, a parenthesis will be added to the end of each comparison.
aliashour592 at gmail dot com 15-Aug-2020 11:14
//to use if condition and html together

<?php if(condition) : ?>

// just html code

<?php  elseif( condition ): ?>

// just html code

<?php  else: ?>
// just html code
<?php endif?>
Ahmed Youssef Elshrief 26-Jun-2020 11:03
You can use this syntax :

 if (condition):

     // your code

else if:

  // your code

else:
  // Your code

endif ;
cole dot trumbo at nospamthnx dot gmail dot com 31-May-2017 05:41
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;
?>
robk 20-May-2013 01:02
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 ?>
Donny Nyamweya 11-Feb-2011 08:30
In addition to the traditional syntax for if (condition) action;
I am fond of the ternary operator that does the same thing, but with fewer words and code to type:

(condition ? action_if_true: action_if_false;)

example

(x > y? 'Passed the test' : 'Failed the test')
Christian L. 25-Jan-2011 10:58
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.
techguy14 at gmail dot com 06-Jan-2011 01:39
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!
grawity at gmail dot com 10-Mar-2008 03: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.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有