w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP If...Else
The if, elseif and else statements in PHP are used to perform different actions based on different conditions.
PHPÖеēif¡¢elseifºÍelse”µÄÓï¾ä£¨¼´£ºÌõ¼þÓï¾ä£©£¬ËüÊÇ×÷ÓÃÊǸù¾Ý²»Í¬µÄÌõ¼þ£¬Ö´Ðв»Í¬µÄÓï¾ä¡£
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 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Óï¾ä£»
if (Ìõ¼þ) |
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> <?php </body> |
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> <?php </body> |
If you want to execute some code if one of several conditions are true use the elseif statement
Èç¹ûÐèÒª¼ÙÉèµÄÌõ¼þ²»Ö¹Ò»¸öʱ£¬¿ÉÒÔʹÓÃelseifÓï¾ä¡£
if (Ìõ¼þ1) |
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> <?php </body> |
ÆÀÂÛ (0)
All