To ducky:
Actually it's a good idea to start the habit of separating logic from presentation since the beginning (like when you're just starting to learn html and css). Otherwise you'll get used to not work that way, and when you want to do it it'll be much harder.
一個有實際用途的程式
現在讓我們來編寫一個具有實際用途的程式,用它來識別拜訪者所使用的瀏覽器種類。識別瀏覽器的方法是檢查瀏覽器傳遞給伺服器的 user agent 字串。此字串是 HTTP 要求的一部份,而它的資訊是以變數的形式儲存。PHP 的變數是以一個錢字符號($)為開端,而現在我們要的變數是 $_SERVER["HTTP_USER_AGENT"]。
Note: $_SERVER 是一個特別的 PHP 保留變數,包含所有網頁伺服器的資訊。它被稱為 超全域變數(superglobal),它含有網頁伺服器的所有資訊。參閱手冊中superglobal這些特別的變數是在 PHP » 4.1.0 版本起才加入的。在此版本之前,我們使用的是舊式的 $HTTP_*_VARS 陣列,如 $HTTP_SERVER_VARS。雖然它們已廢棄不用,但仍舊存在。請順便參考一下舊程式碼的注意事項。
顯示此變數,我們可以使用下列程式碼:
Example#1 輸出陣列中的變數
<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>
上述腳本的輸出範例:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
PHP 擁有許多不同型別的變數,而上述例子中,我們只不過列印出了陣列的其中一個元素。
$_SERVER 只是其中一個 PHP 自動提供給您的變數。詳細清單請查看手冊中保留的變數一節,或藉由之前章節中範例的phpinfo() 函式輸出取得完整的列表。
當然,您可以在一個 PHP 標籤裡編寫多行、功能不同的 PHP 敘述,組合成為一個程式區塊。例如,我們要檢查造訪者的瀏覽器是否是 Internet Explorer,我們可以用:
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
echo 'You are using Internet Explorer.<br />';
}
?>
上述腳本的輸出範例為:
You are using Internet Explorer.<br />
在此,我們介紹了一個新的概念,使用了 if 敘述。如果您熟悉 C 的基本語法,那麼您就不會對它感到陌生了。若您對 C 語言或任何其他程式語言沒有任何認識的話,或許您應該先參考一些 PHP 入門書籍或閱讀此手冊的語法參考一節。
我們介紹的第二個概念:strpos() 函式的使用。strpos() 是 PHP 提供的其中一個函式,而它的功能是在一個字串中找尋另一個字串。如,我們要在 $_SERVER["HTTP_USER_AGENT"] (稱為 haystack)中尋找 "MSIE" (稱為 needle)這一字串。如果找到該字串,此函式傳回從 haystack 開始算起 needle 的相對位置,否則就傳回 FALSE。如果不是傳回 FALSE,那麼 if 敘述的評估值將是 TRUE,然後 { 大括號 } 裡的程式碼也就被執行了。相反的,若 if 的評估值是 FALSE,那 if 敘述就被跳過了。請嘗試使用 if,else,或其他函式如 strtoupper() 及 strlen() 來建立類似的程式。如果您不知如何使用某函式,請參閱其手冊。每一個函式的手冊都有範例和說明。此外,PHP 手冊中的如何閱讀函式定義和 PHP 函式也有助於對 PHP 語法的了解。
接下來我們將示範如何在 PHP 程式區塊中進入和跳離 PHP 模式。
Example#3 混合使用 HTML 和 PHP 模式
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
<h3>strpos() must have returned non-false</h3>
<p>You are using Internet Explorer</p>
<?php
} else {
?>
<h3>strpos() must have returned false</h3>
<p>You are not using Internet Explorer</p>
<?php
}
?>
上述程式的輸出樣本為:
<h3>strpos() must have returned non-false</h3> <p>You are using Internet Explorer</p>
除了可以使用 echo 敘述輸出字串,我們也可以跳離 PHP 模式而直接以 HTML 輸出文字。使用這種方法的重大意義在於程式本身的邏輯並不受到影響,而哪一個 HTML 區塊會被輸出還是取決於 strpos() 所傳回的值,即有否找到 MSIE 這一字串。
一個有實際用途的程式
02-Sep-2008 01:26
17-Jun-2008 04:25
Seperating logic and presentation should be introduced at start. I'm just learning PHP but have background in Perl, C and C++ and find mixed html/code extremly complex to maintain. It would be very nice if this beginners tutorial already presented on how to seperate code/html. Now to read up on that smarty ;).
23-Apr-2008 05:36
I don't think there is a bad time to start separating logic from presentation. It is actually a good idea to start doing this from the get-go so it becomes a habit. This is a great habit.. Everything now a days is going away from mixing logic and presentation for simplicity sake among other things (look at html and css). I think a good thing to read up on is smarty. It can do wonders separating your logic from presentation.
03-Apr-2008 06:54
To the above poster:
That's probably too much to think about when you're starting out... You should probably at first just concentrate on getting the stuff running and learning syntax before you consider best practices and the like.
21-Dec-2006 02:00
While it's easy to get carried away mixing your logic and presentation together since it's so easy to do, your better off using PHP within HTML only to fill in values, or include other source files.
Keep your actual processing in separate libraries that are called before you send any headers to the page. Try to avoid calling a script that retrieves or sets information, or manipulates it in the middle of your HTML. You'll find it's much easier to maintain.
