Introduction to PHP PHP介紹

Introduction to PHP PHP介紹

PHP in Action 活躍的PHP

PHP is a programming language that can do all sorts of things: evaluate form data sent from a browser, build custom web content to serve the browser, talk to a database, and even send and receive cookies (little packets of data that your browser uses to remember things, like if you're logged in to Codecademy).


Check out the code in the editor. Looks familiar, doesn't it? That's because a lot of it is regular old HTML! The PHP code is written in the <?php and ?> . See how it generates numbers, creates lists, and adds text directly to your webpage?

看一看在右方編輯器的程式碼,是否有點熟悉?這是因為絕大多數都是尋常的Html碼!而PHP程式碼則是被寫在了 <?php ?> 之間。想看看它是如何生成數字.創建列表,並且在你的網頁上加入文字?

Instructions 操作指引


Click Save & Submit Code to start learning how it all works!

按下Save & Submit Code按鈕來學習這些事如何運作的,看看最右邊的結果來猜猜中間的程式碼做了些什麼... 完成後,按下Start Next Lesson來進入下一課

Why learn PHP? 為什麼要學習PHP?

"So what?" You might say. "I can do that with JavaScript." And that's true! But JavaScript's knowledge can be limited.

"那又怎樣?" 你可能會這樣說。”我用 JavaScript也能做到同樣的事情”,你說的沒錯,但是JavaScript能做的事情相對PHP受限較多。

JavaScript generally runs in the browser, or client. This means it only really knows what's going on in your browser, plus whatever information it gets from the website(s) you're connecting to.


PHP, on the other hand, runs on the same computer as the website you're visiting, which is known as the server. This means that it has access to all the information and files on that machine, which allows it to construct custom HTML pages to send to your browser, handle cookies, and run tasks or perform calculations with data from that website.


Instructions 操作指引

We've written a little PHP in the editor to the right, but it's not complete! On line 8, type My first line of PHP! between the ” ”s.

我們在右邊的編輯區寫了一段簡單但未完成的程式碼!在第8行填入 My first line of PHP! ,就在兩個引號(“ ")之間。

Stuck? Get a Hint! 卡住了嗎? 這裡有一些提示!



PHP code can be written right into your HTML, like this:


          <?php   echo "I'm learning PHP!";     ?>   
<?php echo "I'm learning PHP!"; ?>

Your PHP code goes inside the <?php and ?> delimiters. Here we use the function echo to output I'm learning PHP!. We also end the line with a semicolon.

你的PHP程式碼需要寫在<?php?>之間。這裡使用了echo函式來輸出I'm learning PHP!內容到網頁上。在程式碼的最後加上分號作為結束。

Instructions 操作指引

Try it out. On line 8, use echo to output your name. Make sure to end your line with a semicolon.

試看看,在第8行使用 echo 函式來輸出你的名字,記得在最後加上分號。

PHP Files

You might have noticed that our main file is now index.php instead of index.html. This is important! It tells the PHP interpreter that there's PHP code in the file to evaluate.

你可能注意到我們的主要檔案是 index.php,而非 index.html (注意副檔名的不同)。這是很重要的!它告訴了PHP編譯器裡頭有PHP程式碼需要被處理。

Instructions 操作指引

Ready to learn a few PHP commands? Click Save & Submit Code to get started!

準備好來學習一些PHP指令了嗎? 按下Save & Submit Code來開始吧!


The echo function outputs strings. If you type


  echo "Hello!";

PHP will output Hello!.

PHP將會輸出 Hello!

Make sure to end your line of PHP code with a semicolon.


Instructions 操作指引

On line 8 in between the <?php and ?>, use echo to output "I'm learning PHP". Make sure to end your PHP code with a semicolon.

在第8行 <?php ?> 之間,使用echo來輸出 “I’m learning PHP”。記得在PHP指令列最後加上分號。

Strings 字串

A string is a word or phrase between quotes, like so: "Hello, world!"

一個字串,指的是一個字或一段文字被包在兩個引號之間,就像是 "Hello, world!” 

You can type a string all at once, like this:


  echo "Hello, world!";

Or use the concatenation operator, which glues several strings together:

又或者使用連結運算子( . ),把數個字串連結在一起:

   echo "Hello," . " " . "world" . "!";

The concatenation operator is just a dot (.). (If you're coming to PHP from JavaScript, the dot does the same thing for strings that + does in JavaScript.)

所謂的連結運算子,只需要輸入一個( . )。(如果你之前學過JavaScript,它使用 + 來作為字串的連結運算子。)

Instructions 操作指引

Go ahead and echo a string of your choice on line 8. Try out the concatenation operator if you're feeling bold!


Arithmetic 數學運算

In addition to outputting strings, PHP can also do math.


  echo 5 * 7;

Here we use echo to multiply 5 and 7, and we end our line of code with a semicolon. PHP will output 35.

這裡我們使用 echo 來輸出 5和7相乘的結果,並在最後加上分號,PHP將會輸出35。

Instructions 操作指引

On line 8 in between the <?php and ?>, use echo to calculate 17 * 123. Make sure to end your PHP code with a semicolon.

在第8行,<?php?> 之間,使用 echo 來計算 17*123。記得最後要加上分號。

Variables 變數

So far we've been outputting strings and doing math.


To do more complex coding, we need a way to "save" these values. We can do this using variables. A variable can store a string or a number, and gives it a specific case-senstive name.


Examples 例子:

  • $myName = "Beyonce";
  • $myAge = 32;

All variable names in PHP start with a dollar sign ( $ ).

所有在PHP裡的變數名稱都是用錢號( $ )來作開頭,很好辨認。

Instructions 操作指引

On line 8, create a variable named $myNameand set it equal to your name. Make sure to end your PHP code with a semicolon.

在第8行,建立一個名為 $myName 的變數,並用等號存入你的名字。記得最後要加分號。

Semicolons 分號

You've probably noticed that our lines of PHP code end in semicolons (;). PHP requires semicolons at the end of each statement, which is the shortest unit of standalone code. (For example, echo"Hello!"; or 2 + 2;)

你可能注意到我們PHP程式碼的最後都加上了分號。PHP要求分號來作為每一行 statement ,或稱為指令列的結束。你可理解為程式碼就是由一行行的指令列組合而成的。

(例如,echo “Hello!”; or 2+2;)

You can think of a statement is a complete PHP thought. 19 + or echo aren't complete thoughts, so you wouldn't put semicolons at the end of them!

你可以想成一個指令列就是一個完整的PHP程式。 19 + 或者是 echo 則並不完整,後面還需要加上數字或字串,所以你還不會接分號在後面。

<?php echo "Use your semicolons!"; ?>

Instructions 操作指引

We've accidentally forgotten our semicolon on line 8. Add it in!


Comments 註解

Just like we sometimes put comments in our CSS (using /* this syntax */) or in our HTML (using ), we can also put comments in our PHP code! We do that using two forward slashes (//), like so:

就像是我們偶而會在CSS檔案裡頭加上註解 (使用 /* 語法 */) ,或者是在 HTML (使用),我們也能夠在PHP程式碼裡面加上註解,只需要在被註解內容前面加上兩個往前的斜線 (//) 即可,如下例:

    echo "I get printed!";
    // I don't! I'm a comment.

Instructions 操作指引

Go ahead and add a comment to our PHP code. It can say whatever you like!


Creating a Variable 建立一個變數

Practice makes perfect! Let's get started by creating a variable and giving it a value.


Instructions 操作指引

Declare a variable, $myName, and give it your name as a string.

宣告一個變數, $myName ,並且存入你的名字字串。

Let the Machine Do the Math 命令機器進行運算

Remember, computers were built to do the stuff we don't want to! And that includes doing tedious computations.


Instructions 操作指引

After your first variable, declare a second, $myAge, and set it equal to your age as a number. Remember: no quotes around numbers!

在你第一個變數之後,宣告第二個名為 $myAge 的變數,並存入你的年齡數字。記得數字不需要用雙引號括起來!(只有字串才需要)

Echo It! 回應!

Nice work! PHP computed the value for you, but it didn't appear in your .php document because we didn't print it using echo. Let's fix that!

幹得好!PHP為你進行了運算,但卻沒有顯示在結果頁面上,那是因為我們沒有使用 echo 函式來印出來。來作點修改吧!

Instructions 操作指引

Let's finish this up! Beneath your existing PHP code, use echo to print out your name and your age, like so:


echo $myName;
echo $myAge;


發佈自 林品爵

一個堅信程式教育能夠改變下一代未來的狂熱者,青少年揚帆計畫的推動者。十年以上全端程式開發,上百場程式教學經驗,累積學生數達百人以上。 成大學士畢業,使用過J2EE.PHP.MySQL.CoronaSDK.Android.Objective-C.Swift.Python等技術知識,歷任友訊科技總部Java工程師首席.黑快馬股份有限公司研發經理.諾亞數碼娛樂執行長等職位,現任哥布林程式教育學苑創辦人,所開發過專案涵括產品管理系統.POS系統.拍賣平台.擴增實境.問券.社區App等等。 程式啟蒙班講師 . Laravel網頁程式班講師 . App程式班講師