2012年7月10日 星期二

PHP+MySQL課程 -- 20120710

判斷流程一:

if  (boolean)
{
true  要執行的工作;
}
else
{
false 要執行的工作;
}

----------------------------------------------------------
$a == $b (a等於b)
$a != $b  (a不等於b)
$a >= $b (a大等於b)
$a <= $b (a小等於b)
$a > $b   (a大於b)
$a < $b   (a小於b)
----------------------------------------------------------

舉例:(PHP程式中,參雜HTML語法)

<?php
$a=32;
if($a>=60){

?>
<stonge>成績passed!!</stonge>      // HTML語法
<?php
}
else
{

?>
<em>成績未達最低標準!!</em>     // HTML語法
<?php
}   
?>


 -------------------------------------------------------

判斷流程二:

if (boolean)
{
要執行的工作(true);
}
elseif
{
要執行的工作(ture);
}
esle
{
'其他'要執行的工作;
}


舉例:(PHP程式中,參雜HTML語法)

<?php
$b=88;
if ($b>=90){

?>
<strong>恭喜您拿[優]等!!</strong>    // HTML語法
<?php
}
elseif($b>=80){

?>
<strong>恭喜您拿[甲]等!!</strong>   // HTML語法
<?php
}
else
{

?>
<strong><font color='red'>您拿了[乙]等以下!</font></strong>   // HTML語法
<?php
}
?>

-----------------------------------------------------------------------------
舉例:(function 與 return 與 array 與 ifelse..綜合用法)

<?php
$arr=array('name'=>'jacky','score'=>'78');
function comment($a){
if ($a>=90){
 return '優等';
}
elseif($a>=80){
 return '甲等';
}
else
return '乙等';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
</head>

<body>

<table width="800" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">姓名</th>
    <th scope="col">分數</th>
    <th scope="col">級等</th>
  </tr>
  <tr>
    <td align="center"><?php echo $arr['name']  ?></td>
    <td align="center"><?php echo $arr['score'] ?></td>
    <td align="center"><?php echo comment($arr['score'])?></td>
  </tr>
</table>

</body>
</html>

----------------------------------------------------------------------------------

循環流程:(迴圈loop)
while (true)                                 →條件符合,繼續往下跑!
true 繼續要執行的工作}         → 執行完畢後,將值再丟回 while判斷!

舉例:

<?php
$i=1;
while($i<=10)              
{
 echo '<p>'.$i.'</p>';
 ++$i;                              //再將值丟回到while判斷}
?>




沒有留言:

張貼留言