如果學過C或C++語言的人,看到php會覺得超簡單DER~
用法幾乎一模一樣!
(1)基本介紹:
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x <= 3; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
------------------ Output ------------------
The number is: 0
The number is: 1
The number is: 2
The number is: 3
(2)呼叫function 用法:
<!DOCTYPE html>
<html>
<body>
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>
</body>
</html>
------------------ Output ------------------
5 + 10 = 15
7 + 13 = 20
2 + 4 = 6
(3) PHP 裡的 sort function: 利用 array( ) 這個function 先存好值,接著丟到 $number裡面, 再利用sort的function讓他排序,最後印出。
ps. count( ) 這個function 主要是用來計算 array長度
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
</body>
</html>
------------------ Output ------------------
2
4
6
11
22
參考: w3school.com