ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP If...Else

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
WINDOWSϰ²×°MyS..
PHP ÖÆ×÷ ÍøÕ¾/·þ..
ÓÃPHPºÍCSSÖÆ×÷»î..
PHP µ¥¼þģʽ
PHP MVCģʽ£¬Àà·..
PHP ÖÐʹÓÃÕýÔò±í..
PHP ·ÀÖ¹ SQL ×¢È..
PHP ¿çÕ¾µã½Å±¾¹¥..
PHP ·ÀÖ¹Óû§²Ù×Ý..
PHP ·ÀÖ¹Ô¶³Ì±íµ¥..

PHP If...Else


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:6563 :: ::

The if, elseif and else statements in PHP are used to perform different actions based on different conditions.
PHPÖеēif¡¢elseifºÍelse”µÄÓï¾ä£¨¼´£ºÌõ¼þÓï¾ä£©£¬ËüÊÇ×÷ÓÃÊǸù¾Ý²»Í¬µÄÌõ¼þ£¬Ö´Ðв»Í¬µÄÓï¾ä¡£


Conditional Statements
Ìõ¼þÓï¾ä

Very often when you write code, you want to perform different actions for different decisions.
ÄãÔÚÊéд´úÂëÊǾ­³£»áʹÓÃÌõ¼þÓï¾ä£º

You can use conditional statements in your code to do this.
Äã¿ÉÒÔÔÚ´úÂëÖпÉÒÔʹÓõÄÌõ¼þÓï¾ä¼°¹¦ÄÜÈçÏ£º

  • if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
    if...else Óï¾ä£ºÈç¹ûÄãÏ£ÍûÔÚÌõ¼þÎªÕæ£¨true£©»òΪ¼Ù£¨false£©Ê±Ö´ÐÐij¶Î´úÂ룬Äã¿ÉÒÔʹÓÃÕâ¸öÓï¾ä£»
  • elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true
    elseif statement£ºÕâ¸öÓï¾äÊǺÍif...elseÓï¾äÒ»ÆðʹÓõġ£Èç¹ûÐèÒª¼ÙÉèµÄÌõ¼þ²»Ö¹Ò»¸öʱ£¬¿ÉÒÔʹÓÃÕâ¸öÓï¾ä£»

The If...Else Statement
If...ElseÓï¾ä

If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.
Èç¹ûÄãÏ£ÍûÔÚÌõ¼þÎªÕæ£¨true£©»òΪ¼Ù£¨false£©Ê±Ö´ÐÐij¶Î´úÂ룬Äã¿ÉÒÔʹÓÃif....elseÓï¾ä£»

Syntax
Óï·¨

if (Ìõ¼þ)
µ±Ìõ¼þÎªÕæ´úÂë¾Í»áÖ´ÐÐ;
else

µ±Ìõ¼þΪ¼ÙÕâ¶Î´úÂë¾Í»áÖ´ÐÐ;

Example
°¸Àý

The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":
Èç¹û½ñÌìÊÇÐÇÆÚÎ壬ÏÂÃæÕâ¸öÀý×Ó½«Êä³ö“Have a nice weekend!”£»·ñÔò£¬Ëü½«Êä³ö“Have a nice day!”£º

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>

If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
ÔÚijһ¸öÌõ¼þ£¨Ìõ¼þÎªÕætrue/¼Ùfalse£©µÄÇé¿öÏ£¬Èç¹û²»Ö¹Ò»Ìõ´úÂëÐèÒª±»Ö´ÐУ¬ÄÇô¿ÉÒÔÓôóÀ¨ºÅ“{}”°ÑËü°üº¬ÔÚÄÚ£º

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>


The ElseIf Statement
ElseIfÓï¾ä

If you want to execute some code if one of several conditions are true use the elseif statement
Èç¹ûÐèÒª¼ÙÉèµÄÌõ¼þ²»Ö¹Ò»¸öʱ£¬¿ÉÒÔʹÓÃelseifÓï¾ä¡£

Syntax
Óï·¨

if (Ìõ¼þ1)
Âú×ãÌõ¼þ1ʱ¾ÍÖ´ÐÐÕâ¶Î´úÂë;
elseif (Ìõ¼þ2)

Âú×ãÌõ¼þ2ʱ¾ÍÖ´ÐÐÕâ¶Î´úÂë;
else
Á½¸öÌõ¼þ¶¼²»Âú×ãµÄ¾ÍÖ´ÐÐÕâ¶Î´úÂë;

Example
°¸Àý

The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
Èç¹û½ñÌìÊÇ“ÐÇÆÚÎ唣¬ÏÂÃæµÄÀý×Ó½«Êä³ö“Have a nice weekend!”£»Èç¹û½ñÌìÊÇ“ÐÇÆÚÈÕ”£¬ÏÂÃæµÄÀý×Ó½«Êä³ö“Have a nice Sunday!”£»Èç¹ûÊÇÆäËüÇé¿öÔòÊä³ö“Have a nice day!”£º

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>

ÆÀÂÛ (0) All