1.
<?php
echo "string"
?>
PHP以<?php... codecodecode... ?>為前後引號
echo將後面的code顯示於網頁上
2.
"."就像"+"號
3.
所有PHP裡 variable names 是以( $
).做開頭
EX:
$myName = "Beyonce";
$myAge = 32;
4.
基礎運算符號
>
Greater than<
Less than<=
Less than or equal to>=
Greater than or equal to==
Equal to!=
Not equal to
5.
if判斷式
EX:
<?php $age = 17; if( $age > 16 ) { echo "You can drive!"; }?>
6.Switch
<?php
switch (2) {
case 0:
echo 'The value is 0';
break;
case 1:
echo 'The value is 1';
break;
case 2:
echo 'The value is 2';
break;
default:
echo "The value isn't 0, 1 or 2";
}
?>
7.多樣選擇(if/else & Swith)
EX.
if ($i == 1 || $i == 2 || $i == 3) { echo '$i is somewhere between 1 and 3.';}
在swith的case之後,不要加上break,繼續往下增加case,亦可以達成,此方法稱為 falling through
EX.
<?php
$i = 5;
switch ($i) {
case 0:
echo '$i is 0.';
break;
case 1:
case 2:
case 3:
case 4:
case 5:
echo '$i is somewhere between 1 and 5.';
break;
case 6:
case 7:
echo '$i is either 6 or 7.';
break;
default:
echo "I don't know how much \$i is.";
}
?>
8.陣列
在PHP中之陣列如下例所示:
<?php
$array = array("Egg", "Tomato", "Beans");
?>
陣列中之編號從0開始
所以$array = array("Egg", "Tomato", "Beans");
$array[0] //"Egg"
$array[1] //"Tomato"
$array[2]//"Beans"
移除陣列中某個項目
<?php
$array = array("red", "blue", "green");
unset($array[2]);
?>
或是整個陣列
<?php
unset($array);
?>
9.For迴圈
EX:
<?php
for ($i = 0; $i < 10; $i++) {
echo $i;
}
// echoes 0123456789
?>
foreach:疊代指定項目(通常是陣列)中所有物件
EX:
<?php
$numbers = array(1, 2, 3);
foreach($numbers as $item) {
echo $item;
}
?>
10.While迴圈
當指定條件未達成時持續進行迴圈操作
ex:
$loopCount = 0;
while ($loopCount<4){
echo "<p>Iteration number: {$loopCount}</p>";
$loopCount ++;
}
也可以寫成這樣
while(cond):
// looped statements go here
endwhile;
運用do-while迴圈,可以先運作一次迴圈內程式後再判定是否符合繼續迴圈條件
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
11.換行
.
"<br>"
EX:
echo $len."<br>";
print $mygf;
12.for 迴圈
EX:
<?php //for $x等於 1 而$x 小於等於 10時 迴圈執行,每執行一次迴圈則自動+1<br> for($x=1;$x<=10;$x++){ echo $x."<br />"; //下面二行為了防止無限迴圈 if($x>15) exit; } ?> |
留言列表