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).

PHP是一種網頁程式語言可用來執行以下這些任務:驗證從瀏覽器傳回的資料.生成自訂的網頁內容以提供給前台.和資料庫溝通,甚至是發送或接收cookies(小包裝的數據,用來讓瀏覽器能夠記住一些事情,比如Codecademy就是用cookies來記住你已經完成登入)

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 操作指引

在這個區塊,CodeAcademy通常會需要你利用編輯器進行一些操作,作為練習

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.

JavaScript一般來說是運行在瀏覽器上,或被稱為client(客戶端)。這代表他只會知悉與瀏覽器有關,以及從你的網頁上所得到的輸入資訊。

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.

PHP,從另一方面來說,則是運行在你所拜訪網頁儲存所在的那一台電腦上,或被稱為server(伺服器)。這代表他能夠取用這台電腦的所有檔案與資訊,這允許它得以生成自訂網頁來提供給瀏覽器,處理cookies,運行排程工作,運算從網頁取回的資料等。

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 and HTML

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

PHP能夠被寫在HTML程式碼裡頭,就像這樣:


<body>
       <p>     
          <?php   echo "I'm learning PHP!";     ?>   
       </p>
</body>
<?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來開始吧!


Echo

The echo function outputs strings. If you type

echo函式能輸出字串,假如你輸入


<?php
  echo "Hello!";
?>

PHP will output Hello!.

PHP將會輸出 Hello!

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

務必記得在你PHP指令列的最後加入一個分號。

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:

你可以一次把一句話打成一個字串像這樣:


<!?php
  echo "Hello, world!";
?>

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

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


<!?php
   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!

繼續往前,在第8行輸出一個字串。試著使用連結運算子來連接多個字串如果可以的話!


Arithmetic 數學運算

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

除了輸出字串,PHP也能夠算數。


<?php
  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.

如果需要作更複雜的程式,需要”儲存”一些資料。我們可以利用variables,或稱為變數來達到。一個變數可以儲存一個字串或數字,並給它一個大小寫敏感的名字。(以我的名字來說,ZACK和zack在PHP來看是不同的)

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程式碼裡面加上註解,只需要在被註解內容前面加上兩個往前的斜線 (//) 即可,如下例:


<?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!

繼續在PHP程式碼加上一個註解。註解內容是什麼都可以!


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:

讓我們來完成它!在你完成的PHP程式碼下方,使用echo來輸出你的名字和年齡,就像這樣:


echo $myName;
echo $myAge;
分享這篇文章:

發佈自 林品爵

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

關聯文章:

留下留言