PHP 基礎觀念和語法

PHP 基礎觀念和語法

Intro & Syntax

  • PHP 是一種樣板語言,可以混合 HTML 直接輸出成網頁。
  • 為純文字檔案是以 .php 結尾。
  • 撰寫 PHP 語法時,必須將語法包裹在  <?php ?> 裡面去寫。
  • 每一段程式碼需要以 ; 結尾。
    1
    2
    3
    4
    <?php
    $var = 'A string';
    echo $var;
    ?>
  • PHP 的變數使用上都是 case-sensitive,除了內建的keyword以外(e.g. if, else, while, echo, etc.)。ㄒ
  • PHP 的 Comment 使用上有三種
    1
    2
    3
    4
    5
    6
    7
    <?php
    // This is a single-line comment.
    # This is a single-line comment.
    /*
    This is a multiple-line comment block
    */
    ?>

Output 輸出訊息

PHP 可以使用 echo 和 print 這兩個statement來做資料輸出至畫面上,兩者在使用上基本沒有什麼差異性。

1
2
3
4
5
6
7
8
<?php
$x = 10;
echo "<h2>Hello PHP</h2>";
echo "This a variable $x";
print "<p>Today is a good day</p>";
print "Show result: $x";
?>


Declare Variable 宣告變數

  • PHP 宣告 Variable 皆以 $ 為開頭宣告。
  • Variable 只能以 _a-z,A-Z作為開頭,不可以用數字作為開頭。
  • PHP 是 弱型別 的語言,所以在賦值的時並不需要指定型別。
  • Variable names 使用上都是 case-sensitive,$name 和 $NAME 是不同的variable。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?php
    # legal variable
    $myVar = 100;
    $MyVar = "100";
    $_name = "Derrick";
    # illegal variable
    $5Var = 500;
    ?>

Variable Scope 變數可視範圍

PHP 的Variable scope 劃分為三種:

  • local
  • global
  • static

Global Scope

宣告在 function 之外的 variable 為Global scope,只能在 function 之外被存取使用。

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$x = 5; // Global scope
function myTest () {
// 這行會產生 error 的訊息
echo "Variable x inside function is : $x";
}
myTest();
echo "Variable x outside function is : $x";
?>


Local scope

宣告在 function 之中的 variable 是屬於 Local scope,只能在 function 之內被存取使用。

1
2
3
4
5
6
7
8
9
10
<?php
function mytest() {
$x = 5; // Local scope
echo "Variable x inside function is : $x";
}
myTest();
// 這行會產生 error 的訊息
echo "Variable x outside function is : $x";
?>

同樣的local variable name 可以存在在不同的 function 之中,因為 function 不會知道其他 function 裡面宣告了什麼 variable。


global keyword & $GLOBALS[index]

使用這個 global keyword,可以讓 global variable 在 function內被存取使用。

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$x = 5;
$y = 10;
function sum() {
global $x , $y;
$y = $x + $y;
}
sum();
echo $y; // 這行 $y 會輸出 15。
?>

所有的 Global variable 會存放在 GLOBALS[index] 這個陣列之中,而 index 代表的是每一個 variable 的 name,也可以透過這個陣列在 function 中使用 Global variable。

1
2
3
4
5
6
7
8
9
10
<?php
$x = 5;
$y = 10;
function sum() {
GLOBALS['y'] = GLOBALS['x'] + GLOBALS['y'];
}
sum();
echo $y; // 這行 $y 會輸出 15。
?>


static keyword 靜態變數

當 function 執行完畢後,所有的 Local variable 被會被清除掉,如果想要保留住某些 local variable 不被清除,此時就要用到 static 這個keyword。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
/*
執行完後的結果會如下:
0
1
2
*/
?>


Constant 常數

  • 常數是定義了以後就不能再改變其值。
  • 常數的name須以 _ 或是 a-zA-Z 為開頭。
  • 常數是屬於全域的變數且包含 function 裡也可以使用。

Syntax

  • name: 常數的名稱
  • value: 常數的值
  • case-insensitive: 是否忽略字母大小寫, 預設是 false。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    define(name, value, case-insensitive)
    // Example - 1
    <?php
    define("GREETING", "Welcome to W3Schools.com!");
    echo GREETING;
    // 輸出結果:Welcome to W3Schools.com!
    // Example - 2
    <?php
    define("GREETING", "Welcome to W3Schools.com!");
    function myTest() {
    echo GREETING;
    }
    myTest(); // 輸出結果:Welcome to W3Schools.com!
    ?>
    ?>