跳轉到內容

PHP 和 MySQL 程式設計/語法概述

來自華夏公益教科書,開放的書籍,面向開放的世界

PHP 標籤

[編輯 | 編輯原始碼]

<?php [code] ?>

[編輯 | 編輯原始碼]
  • 包含 PHP 程式碼
  • 嵌入在普通 HTML 程式碼中
  • 在 PHP 標籤中,語句用 ; 分隔(通常也緊跟著一個換行符)。

示例

 <?php
 print "Hello World\n";
 $date = date("Y-m-d H:i:s");
 print "The date and time at the moment is $date";
 ?>

註釋掉一行

示例

echo "Hello"; // Everything from the hash is commented out

示例

// This entire line is commented out

// 功能相同

/* (text) */

[編輯 | 編輯原始碼]

註釋掉 /* 和 */ 之間的所有內容

示例

 /*All of this is
 commented out.
 Even this line!*/

PHP 中的變數用 $ 字首表示。

示例

 $a = "Hello World"; # this assigns the string "Hello World" to $a.
 $b = "$a, I'm Ralfe"; # this assigns "Hello World, I'm Ralfe" to $b.
 $b = $a.", I'm Ralfe"; # exactly the same as the previous example.

PHP 支援動態變數。

示例

$c = “response”;

$$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response.


PHP 變數不需要事先宣告,也不需要型別定義。PHP 處理所有資料型別轉換。

  • 示例
$a = 4;

$b = 12;

print “The value of a is $a.; # Uses a as a String.

$c = $a + $b; # $a is now used as an Integer again.

PHP 支援布林變數,可以賦值為 1 或 0,或 true 或 false。

示例

$a = true;

$b = 1;  # $a and $b are equal, though not identical.

$c = false;

$d = 0; # $c and $d are equal, though not identical.

有用的定義

[編輯 | 編輯原始碼]
相等 vs 相同
相等
值在數值上相等,但型別可能不同。使用 == 比較運算子。例如 false 等於 0。
相同
值在數值上相等,型別也相同。使用 === 比較運算子。例如 false 僅與 false 相同,它與 0 不相同。

運算子

[編輯 | 編輯原始碼]

算術運算子

[編輯 | 編輯原始碼]

示例

$a = 4;

$b = 2;

$a + $b = 6;  // Addition

$a - $b = 2;  // Subtraction

$a * $b = 8;  // Multiplication

$a / $b = 2;  // Division

$a % $b = 0;  // Modulus

$a++;  // Increment

$a--;  // Decrement

賦值運算子

[編輯 | 編輯原始碼]

示例

$a = 4;

$b = $a;

// $b = 4;

比較運算子

[編輯 | 編輯原始碼]

示例

$a == $b // test if two values are equal

$a === $b // test if two values are identical

$a != $b // test if two values are not equal

$a !== $b // test if two values are not identical

$a < $b // test if the first value is less than the second

$a > $b // test if the first value is greater than the second

$a <= $b // test if the first value is less than or equal to the second

$a >= $b // test if the first value is greater than or equal to the second

示例

$a = "Fill the halls ";

$b = "with poisoned ivy...";

$c = $a . $b;  # the '.' operator concatenates two variables.

// $c = "Fill the halls with poisoned ivy...";

PHP 支援數值索引陣列和關聯陣列。

示例

$a = array(1, 2, 3, 4);

// $a[0] = 1;

// $a[1] = 2;

// $a[2] = 3;

// $a[3] = 4;

$b = array("name" => "Fred", "age" => 30);

// $b['name'] = "Fred";

// $b['age'] = 30;

決策和迴圈語句

[編輯 | 編輯原始碼]

If ... else 語句

[編輯 | 編輯原始碼]

示例

 $a = 1;
 $b = 10;
 
 if ($a > $b) {
    echo "a is greater than b";
 }
 else if ($a == $b) {
    echo "a is equal to b";
 }
 else {
    echo "a is not greater than b";
 }
 
 // OUTPUT:
 // a is not greater than b

Switch 語句

[編輯 | 編輯原始碼]

示例

 $a = 100;
 
 switch($a) {
    case(10):
       echo "The value is 10";
       break;
 
    case(100):
       echo "The value is 100";
       break;
 
    case(1000):
       echo "The value is 1000";
       break;
 
    default:
       echo "Are you sure you entered in a valid number?";
 }
 
 // OUTPUT:
 // The value is 100

For 語句

[編輯 | 編輯原始碼]

示例

 for ($i = 0; $i < 10; $i++) {
    # initialize $i ; while condition ; increment statement
    echo $i;
 }
 
 // OUTPUT:
 // 0123456789

Foreach 語句

[編輯 | 編輯原始碼]

示例

 $a = array(1, 2, 3, 4, 5);
 
 foreach ($a as $val){
    echo $val;
 }
 
 // OUTPUT:
 // 12345

While 語句

[編輯 | 編輯原始碼]

示例

 while ($row = mysql_fetch_row($result)){
    print $row[0];
 }

Do ... while 語句

[編輯 | 編輯原始碼]

示例

 $i = 0;
 # Note that it might seem that $i will
 do{
    # never be printed to the screen, but
    print $i;
    # a DO WHILE loop always executes at
 } while ($i >0);
 # least once!

示例

 function square_number($number) {
    return ($number * $number);
 }
 
 $answer = square_number(10);
 echo "The answer is {$answer}";
 
 // OUTPUT:
 // The answer is 100

示例

 class dog {
    var $name;
    
    function __construct($name){
       $this->name = $name;
    }
    
    function bark(){
       echo "Woof! Woof!";
    }
    
    function who_am_i() {
       echo "My name is {$this->name}, and I am a dog";
    }
 }
 
 $the_dog = new dog("John");
 $the_dog->who_am_i();
 
 // OUTPUT:
 // My name is John, and I am a dog

從 PHP 5.3.3 開始,與類同名的函式將不再充當建構函式。5.3.3 之前的版本

function __construct($name)

行可以替換為

function dog($name)

以實現相同的效果。 [1]


  1. https://php.net.tw/manual/en/language.oop5.decon.php
華夏公益教科書