php



[1] 什麼是 php?

php 是網頁後端的東西。
在寫 HTML 的時候,如果想跟使用者互動,像是寫登入、登出的網頁,可以利用 php 在網頁間互相傳值。
如果之後要將網頁連結資料庫的話也可以使用 php。

在寫 php 時,需要在結尾加「;」,像在寫 C++ 一樣。

在程式中使用 php 的方式

  1. <?php php程式碼 ?>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. <script language='php'>php程式碼</script>
    (目前新版本的 php 不支援)

[2] 基本語法

註解

// 單行註解
/* */ 註解

變數

使用變數時不需要宣告 int、float、bool……

直接:

$變數名稱=內容;

php 變數常用的資料型態

資料型態 例子
int $var = 10 ;
float $var = 1.5 ;
string $var = 'rilakkuma' ;
array $var[0] = 12 ;
bool $var = True ;

運算子

跟 C++ 差不多。
有 +、-、*、/、%、++、、=、+=、==、!=、>、<、&&、||……


[3] 字串

字串連接

字串.字串;

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

印出字串

echo

echo 字串;

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用 echo 不會有回傳值,因此比較快。

print(字串);

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用 print( ) 會有回傳值,因此比 echo 慢一點。


[4] 陣列

php 的陣列在使用上跟 C++ 很像,但在建立 php 的陣列時不需要給陣列長度。

用 php 建陣列的方式

$var = array('501','lala','bear') ;
$var[0] = '501' ;
$var[1] = 'lala' ;
$var[2] = 'bear' ;

count( )

計算元素個數。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

array_sum( )

將陣列的元素加總。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

sort( )

將陣列的元素進行排序。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

除此之外還有很多,這裡只是把比較常用的列出來。


[5] 判斷式與迴圈

if、else 判斷式

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

while 迴圈

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

for 迴圈

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


[6] 函式

function

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

常用函式

empty( )

檢查變數是否為零或尚未指定。

例 1.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

例 2.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

例 3.

unset( )

清除變數,讓變數尚未指定數值。

檢查變數型態

函數 意思
is_bool( ) 檢查變數是否為布林值
is_int( ) 檢查變數是否為整數
is_float( ) 檢查變數是否為浮點數
is_string( ) 檢查變數是否為字串
is_numeric( ) 檢查變數是否為數字或數字字串
is_array( ) 檢查變數是否為陣列

變數型態轉換(好用!)

函數 意思
intval( ) 將 string 轉成 int
strval( ) 將 int 轉成 string

字串相關函數(好用!)

函數 意思
strlen( ) 取得字串長度
trim( ) 清除字串前後空白字元
strpos(字串, 搜尋的字串或字元) 取得搜尋的字串或字元在字串裡的位置,可帶入第三個參數設定開始搜尋的位置
strchr(字串, 搜尋的字串或字元) 字串開頭搜尋字串並印出搜尋字串或字元之後的字串(包含搜尋的字串或字元)
strrchr(字串, 搜尋的字串或字元) 字串結尾搜尋字串並印出搜尋字串或字元之後的字串(包含搜尋的字串或字元)
substr(字串, 字元開始位置) 取得部分字串內容,可代入第三參數設定取得字串長度
explode(字串1, 字串2) 根據字串 1 將字串 2 分割成陣列,可代入第三參數設定分割陣列元素
implode(字串, 陣列) 根據字串將陣列串接


[7] php 與 HTML

如果想製作可互動的網頁,像是登入頁面、購物網站等等需要多個頁面可以相傳遞資料的話,php 會是一個很好的選擇。

在 .php 檔中,可以直接寫入:

<html>HTML語法</html>

php 與 HTML 互相嵌入

  1. php 中使用 HTML
    最簡單的方式就是把 HTML 包成字串,再利用 echo 印出來。

  2. HTML 中使用 php
    同上,最簡單的方式也是把 php 包成字串,再利用 echo 印出來,只是要記得一定要把 <?php ?> 也跟著一起包起來。

HTML 透過 php 傳值

個人覺得 php 最好用的地方就在這裡,利用 HTML 語法中的 form 表單,就可以簡單方便地把資料傳送出去。

傳送資料的方式:

  1. get
    在傳送資料時,使用網址傳送資料,傳送的資料量有限,一共只能傳輸 255 個字元(包含網址的字元)。
    接收資料時用:

    ​​​​$_GET['資料名稱'];
    

  2. post
    利用通訊協定的標頭傳送資料,資料不會顯示在網址上,也可以傳大量的資料(個人覺得比較好用)。
    接收資料時用:

    ​​​​$_POST['資料名稱'];
    

比對資料格式 -preg_match( )

在設定帳號、密碼或是填寫 e-mail 的時候,會要求用特定的資料格式,如果不符會出現警告要求修改,這個可以用 php 寫出來。

  1. 若資料符合格式,回傳 1,反之回傳 0。
preg_match(格式, 字串)
  1. 將符合格式的資料存入陣列中。
preg_match(格式, 字串, 儲存陣列)

格式的寫法:

格式 = '/寫法/'
寫法 意思
[ ] 在列舉的範圍內,填入想要比對的資料格式
[^ ] 不在列舉的範圍內,填入想要比對的資料格式
\w 字詞字元,即 [a-zA-Z0-9_]
\W 非字詞字元,即 [^a-zA-Z0-9_]
\d 數字,即 [0-9]

在寫格式時,也可以直接寫 /[a-z]/,依需求填入 [ ] 內就可以了,在比對時只要都是在 [ ] 內有出現的字元就算符合,跟順序無關。

如果要將比對後的資料存數陣列中,則要寫成:

/(格式1+)(格式2+)/

像是:

/([0-9]+)([a-zA-Z]+)/

這個網站可以協助我們摸索格式的寫法:
https://regex101.com/

期待大家用 php 寫出好看的網頁~


tags: ckefgisc23rd