2019/10/12 (更新內容)
2023/10/07 (補充內容)
2023/10/26 (補充內容)
2024/09/08 (更新內容)
2024/09/10 (更新環境設定投影片)
2024/09/17 (微調內容)
利用PHP開發web應用,需要Apache Web Server、資料庫(MySQL/MariaDB)、PHP,也有一些安裝包(如:AppServ、MAMP、XAMPP)將這些軟體包裝好,可以一次安裝好,不必一個一個的安裝。使用安裝包的時候要注意,每個安裝包所內含的軟體是不太一樣的,目前最大的差異在於使用MySQL或MariaDB,例如,XAMPP內含MariaDB,AppServ內含MySQL,WampServer則是MySQL及MariaDB,另外,還要注意的是支援的作業系統,AppServ及WampServer支援windows,XAMPP號稱跨平台,所以,支援Windows、Mac、Linux版本。不過,因為XAMPP後來支援MariaDB,很多的設定都跟MySQL不一樣,會有一些困擾。
在家使用Windows的同學,可以採用XAMPP或AppServ,目前資訊中心安裝的版本有AppServ及XAMPP,但是,AppServ從2019之後就沒再更新了,所以,不建議大家繼續使用。使用Mac的同學採用MAMP或XAMPP。
上資料庫課程時,老師會要求助教告訴大家直接安裝mysql,原因是要讓大家知道,如果不用AppServ或XAMPP,也可以使用mysql。我們可以下載免費的community server,目前最新的Long Term Support (LTS)版本是8.4.2。另外,可下載MySQL Workbench,目前最新版本是: 8.0.38,要注意,XAMPP內含的是MariaDB與MySQL不完全相容。(2024/09/08)
安裝步驟請詳參:
WAMP (Windows, Apache, MySQL and PHP)
AppServ : Apache + PHP + MySQL (Release Date : 2019-09-29)
安裝時會要求設定mysql的密碼
AppServ 9.3.0
Apache 2.4.41
PHP 7.3.10
MySQL 8.0.17
phpMyAdmin 4.9.1
Support TLS,SSL or https
For 64bit only
XAMPP安裝與操作初步 (2018)(Windows、Mac、Linux)
XAMPP Apache + MariaDB + PHP + Perl
要自行設定mysql密碼
XAMPP 8.2.12 (for Windows), 8.2.12 (for Linux) and 8.2.4 (for OS X) (Release Date: 2023-04-08)
PHP 8.2.12
Apache 2.4.58
MariaDB 10.4.32
phpMyAdmin 5.2.1
OpenSSL 3.1.3
curl 8.4.0_6
Tomcat 8.5.96
Step 1: Press F4 and search for "Privacy and Security"
Step 2: Here, scroll down to the "Security" section and you'll see the blocked app. Click the "Open Anyway" option and continue with your installation.
Note that before doing this, I've already attempted installing the app and the error message has been thrown.
機房環境
啟動XAMPP
啟動Apache
啟動MySQL
Visual Studio
安裝XAMPP
以Windows環境為例
安裝VS Code
安裝Git
寫第一個php程式
目前資訊中心的電腦教室及系機房安裝AppServ及XAMPP。
在資訊中心的電腦教室裡,由於一些管理上的需要,Apache及MySQL的服務是被停掉的,所以,必須手動開啟。
在資訊中心的電腦教室裡,AppServ預設php 5,如果用到php 7的語法,必須更換版本。
如果是安裝在自己家裡的電腦,就不需要這些動作了。
以windows下的xampp為例,先找到xampp的安裝路徑 (如:c:\xampp),在xampp下有htdocs的資料夾,請先產生一個新的資料夾(如: web),再新增一個index.php
以windows下的appserv為例,先找到appserv的安裝路徑 (如:c:\AppServ),在AppServ下有www的資料夾,請先在www下產生一個新的資料夾(如: web),在新資料夾中新增一個index.php
** 有些appserv版本預設不顯示錯誤,所以,要編輯php.ini,將
display_errors = Off
改為
display_errors = On
index.php的內容:
<?php
echo "Hello";
?>
啟動Apache後,在瀏覽器上輸入:
http://localhost/web
Apache就會執行web資料夾下的index.php,所以,就可以看到
Hello
也可以找出電腦的ip,試著從另一台電腦(或手機)連上剛剛寫的網頁
** 如果看到「 localhost 拒絕連線」,通常是我們還沒啟動Apache。如果看到「 Error 404」,通常是檔案名稱錯誤或資料夾名稱錯誤。
PHP Manual (php.net)
PHP Tutorial (w3schools)
<?php ?>中間就是php的程式碼,echo 或 print就是印出內容的意思,所以,這程式會印出「My first PHP script!」(詳參: PHP 7 echo and print Statements)
<?php
echo "My first PHP script!";
?>
如果內容包含HTML,那就成了網頁了。php是以「.」來連接兩個字串,另外,php有一些內建的函數,如date。(詳參: PHP 7 Date and Time & PHP 7 Date/Time Functions & PHP 7 Calendar Functions)
<?php
echo "My first PHP script!";
echo "<p>";
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l") . "<br>";
echo "The time is " . date("h:i:sa"). "<br>";
?>
也可以寫成:
My first PHP script!<p>
Today is <?php echo date("Y/m/d") ?><br>
Today is <?php echo date("Y.m.d") ?><br>
Today is <?php echo date("Y-m-d") ?><br>
Today is <?php echo date("l") ?><br>
Today is <?php echo date("h:i:sa") ?><br>
php 5以後,可以利用<?= ?>,簡化<? php echo ?> (詳參: [鐵人賽Day12]PHP的簡寫標籤與使用方式 )。
My first PHP script!<p>
Today is <?= date("Y/m/d") ?><br>
Today is <?= date("Y.m.d") ?><br>
Today is <?= date("Y-m-d") ?><br>
Today is <?= date("l") ?><br>
Today is <?= date("h:i:sa") ?><br>
d - 代表日 (01 to 31)
m - 代表月 (01 to 12)
Y - 代表年 (四位數)
l (小寫 'L') - 星期幾
H - 以24小時方式表達時 (00 to 23)
h - 以24小時方式表達時 (01 to 12)
i - 代表分鐘 (00 to 59)
s - 代表秒 (00 to 59)
a - 小寫的上午或下午 (am or pm)
PHP的變數使用方式跟python比較像,不需要設定變數的資料型態。跟python不同的是,PHP的變數名稱前要加上"$" (詳參: PHP 7 Variables & PHP 四種變數範圍比較:區域、全域、靜態、參數)。
<?php
$x = 2;
$y = 3;
echo $x + $y;
?>
畫面可以看到:
5
跟python很像,同一個變數,在不同的時間可以儲存不同資料型態的內容。
<?php
$x = 'hello world';
echo $x;
echo "<br>";
$x = 1;
echo $x;
echo "<br>";
?>
畫面可以看到:
hello world
1
這時候,數字相加(+)跟字串相加(.)的效果就不一樣了,而PHP會自動的轉換對應的資料型態,試試看以下的程式:
<?php
$x = 2;
$y = 3;
echo $x + $y;
echo "<br>";
echo $x . $y;
echo "<br>";
$x = "20";
$y = "30";
echo $x + $y;
echo "<br>";
echo $x . $y;
echo "<br>";
?>
畫面可以看到:
5
23
50
2030
也可以在字串裡直接放變數,可以省掉字串相加的麻煩,這也是PHP變數要加「$」的原因。
$txt = "PHP";
echo "I love ".$txt."!";
echo "I love $txt!";
php變數名稱的規則:
PHP的變數名稱前加上"$"
PHP的變數名稱開頭要是字母或底線(underscore),PHP的變數名稱開頭不能是數字
變數名稱只允許字母、數字或底線
變數名稱大小寫會被認為是不同的變數,例如:$age跟$AGE會是不同的變數
PHP的判斷式跟java比較像 (詳參: PHP Operators),不過,源頭應該不是java,而是c語言,因為java與PHP的公開使用都是在1995年左右。
不過因為PHP變數的使用比java有彈性,所有會有"=="及"==="的差別
"=="會忽略變數類型
PHP的operator比較多元,例如,可以使用「$age > 1 and $age < 60」也可以使用「$age > 1 && $age < 60」,不等於可以使用「$x != $y」也可以使用「$x <> $y」
var_dump()就是把變數內容印出來
$x = 100;
$y = "100";
var_dump($x == $y); // 顯示bool(true)因為會忽略變數類型
"==="不會忽略變數類型
$x = 100;
$y = "100";
var_dump($x === $y); // 顯示bool(false)因為變數類型不同
php的if跟java的表示方法一樣。
php取亂數的方法是利用內建函數rand (詳參: rand / PHP rand() Function)
<?php
$r = rand(1,30);
if ($r < 20) {
echo "小於20";
}
else {
echo "大於20";
}
?>
php的if跟java的表示方法一樣。
<?php
$r = rand(1,6);
switch ($r) {
case 1:
echo "扣五分";
break;
case 2:
echo "扣三分";
break;
case 3:
echo "扣一分";
break;
default:
echo "得三分";
}
?>
這個語法在java及python也都有,如果問號前的條件為真,回傳冒號前的內容,否則,回傳冒號後的內容。
$statuslist = isset($_POST["status"])? $_POST["status"] : ["N/A"];
如果??前的內容存在且不是空值,則傳回??前的值,否則,傳回??後的值。(詳參: Null coalescing operator)
//new syntax in php 7
$statuslist = $_POST["status"]?? ["N/A"];
相當於
$statuslist = isset($_POST["status"])||!is_null($_POST["status"])? $_POST["status"]:["N/A"];
接著我們看一下如何利用HTML Form呼叫PHP
PHP的迴圈跟java比較像。
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
結果會是:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
同樣的程式可以利用for來改寫
<?php
for ($x = 0; $x <= 5; $x++) {
echo "The number is: $x <br>";
}
?>
<?php
echo "<table border='1'>";
for ($col = 0; $col < 10; $col++) {
echo "<tr>";
for ($row = 0; $row <10; $row ++){
echo "<td>".$row."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
也可以寫成,這種寫法對開發者來講很難看的懂,但是一般的語法檢查器(linter)比較能幫我們檢查語法:
<table border='1'>
<?php
for ($col = 0; $col < 10; $col++) {?>
<tr>
<?php
for ($row = 0; $row <10; $row ++){ ?>
<td><?=$row?></td>
<?php }?>
</tr>
<?php}?>
</table>
兩種寫法都是把HTML跟PHP混雜在一起,其實很容易眼花撩亂。尤其是寫好HTML之後,要把PHP的邏輯放進去之後,很容易就會跑版。或者是寫好PHP之後再去改HTML,就會讓PHP出現語法錯誤。
php的陣列表示方法跟java或python不太一樣,不過原理滿相似的。就跟python一樣,陣列裡的資料型態可以不同。
print_r() (也可以用var_dump()) 可以印出陣列的內容。
<?php
$customers = array( "Ben", "Mary", "Tom", "Nancy", "Peter");
print_r ($customers);
echo "<br>";
$customers[2] = "Sam";
$customers[9] = "Steven";
print_r ($customers);
echo "<br>";
?>
結果會是
Array ( [0] => Ben [1] => Mary [2] => Tom [3] => Nancy [4] => Peter )
Array ( [0] => Ben [1] => Mary [2] => Sam [3] => Nancy [4] => Peter [9] => Steven )
陣列的寫法也可以寫成:
<?php
$customers = ["Ben", "Mary", "Tom", "Nancy", "Peter"];
print_r ($customers);
$customers[2] = "Sam";
$customers[9] = "Steven";
print_r ($customers);
?>
結果會是:
Array ( [0] => Ben [1] => Mary [2] => Tom [3] => Nancy [4] => Peter )
Array ( [0] => Ben [1] => Mary [2] => Sam [3] => Nancy [4] => Peter [9] => Steven )
PHP還有一種比較特別的陣列Associative Arrays(詳參:PHP 7 Arrays、PHP 7 Multidimensional Arrays)。
$customers = array( "Ben"=>20, "Mary"=>30, "Tom"=>40, "Nancy"=>60, "Peter"=>50);
print_r ($customers);
echo "<br>";
結果會是:
Array ( [Ben] => 20 [Mary] => 30 [Tom] => 40 [Nancy] => 60 [Peter] => 50 )
php的陣列表示方法跟java或python不太一樣,不過原理滿相似的。就跟python一樣,陣列裡的資料型態可以不同。PHP還有一種比較特別的陣列Associative Arrays(詳參:PHP 7 Arrays、PHP 7 Multidimensional Arrays)。
陣列也可以使用foreach,php的foreach其實在java與python都有類似的語法。
print_r() (也可以用var_dump()) 可以印出陣列的內容。
<?php
$customers = array( "Ben", "Mary", "Tom", "Nancy", "Peter");
foreach( $customers as $customer ) {
echo "Value is $customer <br />";
}
$customers[2] = "Sam";
$customers[9] = "Steven";
print_r ($customers);
echo "</br>";
foreach( $customers as $cus ) {
echo "Value is $cus <br />";
}
?>
陣列的寫法也可以寫成:
<?php
$customers = ["Ben", "Mary", "Tom", "Nancy", "Peter"];
foreach( $customers as $customer ) {
echo "Value is $customer <br />";
}
$customers[2] = "Sam";
$customers[9] = "Steven";
print_r ($customers);
echo "</br>";
foreach( $customers as $cus ) {
echo "Value is $cus <br />";
}
?>
結果會是:
Value is Ben
Value is Mary
Value is Tom
Value is Nancy
Value is Peter
Array ( [0] => Ben [1] => Mary [2] => Sam [3] => Nancy [4] => Peter [9] => Steven )
Value is Ben
Value is Mary
Value is Sam
Value is Nancy
Value is Peter
Value is Steven
<?php
$customers = array( "Ben"=>20, "Mary"=>30, "Tom"=>40, "Nancy"=>60, "Peter"=>50);
foreach( $customers as $customer ) {
echo "Value is $customer <br />";
}
$customers["Mary"] = 35;
$customers["Steven"] = 30;
print_r ($customers);
echo "</br>";
foreach( $customers as $cus ) {
echo "Value is $cus <br />";
}
?>
陣列的寫法也可以寫成:
<?php
$customers = ["Ben"=>20, "Mary"=>30, "Tom"=>40, "Nancy"=>60, "Peter"=>50];
foreach( $customers as $customer ) {
echo "Value is $customer <br />";
}
$customers["Mary"] = 35;
$customers["Steven"] = 30;
print_r ($customers);
echo "</br>";
foreach( $customers as $cus ) {
echo "Value is $cus <br />";
}
?>
結果會是:
Value is 20
Value is 30
Value is 40
Value is 60
Value is 50
Array ( [Ben] => 20 [Mary] => 35 [Tom] => 40 [Nancy] => 60 [Peter] => 50 [Steven] => 30 )
Value is 20
Value is 35
Value is 40
Value is 60
Value is 50
Value is 30
如果要同時列出key及value: (詳參: foreach)
<?php
$customers = array( "Ben"=>20, "Mary"=>30, "Tom"=>40, "Nancy"=>60, "Peter"=>50);
foreach( $customers as $name => $age ) {
echo "Name is $name and age is $age <br />";
}
?>
php的函數語法混合了java及python的語法 (詳參: PHP 7 Functions),引數(argument)可以有預設值,在php7之後,引數可以設資料型態,並利用return回傳值。
<?php
function println($p="-----") {
echo $p."<br>";
}
println("hello"); // call the function
println("my name");
println();
println("is ben");
?>
結果會是:
hello
my name
-----
is ben
接著我們看一下如何利用HTML Form呼叫PHP,從Input/Checkbox開始。
Top Code Editors and IDE for PHP Development (2023)
IDE
PHPStorm
Netbeans
Aptana Studio
Eclipse
Visual Studio (with Xamarin)
Zend Studio / Laminas
Visual Studio Code
Code Editor
Sublime Text
Notepad++
Brackets
SlickEdit
CodeLobster
Codeanywhere
UltraEdit
Top 10 PHP Development Tools That Every Developer Have To Know (2018)
Eclipse
NetBeans
PHPStorm
NUSphere
Zend Studio
Sublime Text
Komodo
Aptana Studio
PHPDesigner
Codelobster
26 Best PHP Development Tools That Every Developer Must Explore (2023)
PHPStorm
Codelobster
NetBeans
Rapid PHP editor
Eclipse PDT
Dreamweaver
Zend Studio
Komodo
Visual Studio Code
NuSphere
Sublime Text 3
Atom
Notepad ++
Cloud 9
Codelite
Cloud IDE
PHP Sandbox (onlinephpfunctions.com)
Online PHP Sandbox (wtools.io)
Cloud IDEs For Web Developers – Best Of (2018)
Cloud 9 (PHP)
Codeanywhere (PHP)
Sourcekit (PHP)
Kodingen (PHP)
ShiftEdit (PHP)
Top 10+ Best Cloud IDE For The Developers
Cloud 9 (PHP)
AWS Cloud9 is one of the best cloud IDE of the world. It is a cloud-based Ide which allows you to code, run and debug your source code using a simple browser. AWS includes the debugger, code editor and terminal. Cloud9 packed with important tools for common programming languages, containing PHP, JavaScript, Python, and more.
Codeanywhere (PHP)
Codeanywhere Cloud IDE is a cloud-based integrated development environment. This is one of the best IDE Software according to user review.
Koding (PHP)
Koding is one of the best Cloud IDE software. It let you create and share a fully automated development environment. The developers get an interface to streamline development workflow from Koding.
SourceLair (PHP)
Sourcelair is another wonderful cloud integrated development environment. It has a frictionless development facility in your browser. Sourcelair develops software from any device using PHP, HTML5, Python, Node.js and more.
Eclipse Next-Generation (PHP)
Eclipse Next-Generation cloud IDE is a developer workspace server for team and organization.
Repl.it (PHP)
Repl.it IDE Platform is an online IDE where it needs only 2 seconds to boot up an environment for the programming language. When a program runs on it, then it opens a port.
The Ultimate Guide to Deploying PHP Apps in the Cloud
Heroku
Google Cloud
IBM BlueMix
Microsoft Azure
Amazon Web Services