PHP 是一本正在翻譯的教科书




PHP 是一种脚本语言,主要是用途在于处理动态网页

PHP 目前被广泛的应用,它也是开源软件运动中一个成功的例子。PHP受欢迎是因为其语法类似C、它的速度以及它的简单。 PHP是目前分为两个主要版本: PHP 7和PHP 8。

相信大家也曾经尝试过在网站入使用登入登出的功能。而这类登入的功用,大多数会利用伺服器语言来达成,而你也很可能与PHP打交道。 PHP语言由Rasmus Lerdorf发明。当初的他写这个小程式,目的只是希望追踪浏览者的资料。

基本上,PHP使静态的网页动态化。“PHP”是一个递归的缩写,它表示“PHP: Hypertext Preprocessor(PHP:超文本预处理器)”。PHP 对超文本文档进行预处理(即 PHP 在输出被发送到浏览器之前进行处理)。因此,页面可以根据条件,在被用户看到之前做出改变。这可以被用来在页面上写一些东西,创建一个多行的表格,使它的行数正好等于用户访问的次数,或者,将网页与 web 数据库(例如 MySQL)相整合。

在开始服务器处理的奇妙旅程之前,您最好对超文本标记语言作一些基本的了解。PHP 也被用来开发 GUI 应用程序;PHP-GTK 用来构建图形用户界面(Graphical User Interfaces,GUI)。




Learning the Language


The Basics

This section is about things that are important for any type of PHP development. Useful for a PHP programmer of any level.
  1. Beginning with "Hello World!"  (10 Feb 2006)
  2. Nuts and Bolts  (14 Jan 2006)
  3. Commenting and Style  (14 Jan 2006)
  4. Arrays  (10 Feb 2006)
  5. Control structures
    1. The if Structure  (19 Mar 2009)
    2. The switch Structure  (14 Jan 2006)
    3. The while Loop  (14 Jan 2006)
    4. The do while Loop  (14 Jan 2006)
    5. The for Loop  (14 Jan 2006)
    6. The foreach Loop  (14 Jan 2006)
  6. Functions  (10 Feb 2006)
  7. Files  (12 Feb 2008)
  8. Mailing  (14 Jan 2006)
  9. Cookies  (14 Jan 2006)
  10. Sessions  (2008-05-07)
  11. Databases
    1. MySQL  (14 Jan 2006)
    2. PHP Data Objects  (18 Dec 2006)
  12. Integration Methods (HTML Forms, etc.)  (14 Jan 2006)

Advanced PHP

Advanced PHP includes high level programming and PHP techniques designed to make PHP even more useful and powerful.

Object Oriented Programming (OOP)

  1. Classes
  2. Special Methods
  3. Class Extensions (Inheritance)


  1. Why Templating
  2. Templates
  3. Caching
  4. SMARTY templating system
  5. PRADO Component Framework
  6. Flat Frog templating system  (23 march 2006)
  7. XSL







  1. Configuration: Register Globals
  2. SQL Injection Attacks
  3. Cross Site Scripting Attacks
  4. Building a secure user login system

See also the section on avoiding session fixation in the Sessions chapter.

Command-Line Interface (CLI)

  1. PHP CLI
  2. PHP-GTK
  3. Daemonization

Code Snippets


Useful for any beginners to learn code from.

PHP 4 & 5


Basic Level

  • echo "the text to print"; - This language construct will echo the text between the quotes. This is not a function but a language construct.
  • echo "$var"; - Notice the double quotation marks. Because double quotation marks are used, this will print the value of the variable. If $var="Bobby", this will output:


  • echo '$var'; - Notice that the quotation marks are now single. This will output the literal keystrokes inside the quotes. The example will output:


  • $var="Jericho";echo "Joshua fit the battle of $var."; - Other than substituting the value of a variable for the variable name (and one or two other minor items), double quotes will quote literal keystrokes. So this will output:

Joshua fit the battle of Jericho.

Again, if single quotes were used — 'Joshua fit the battle of $var'; — this would output:

Joshua fit the battle of $var.

  • echo $var; - If you only want to print the value of a variable, you don't need quotes at all. If the value of $var is "1214", the code will output:


  • require "url"; - This language construct will include the page between the quotes. Can NOT be used with dynamic pages, e.g. require("main.php?username=SomeUser"); would not work. This is not a function but a language construct.
  • date("Date/time format"); - Function which returns a date from a Unix Timestamp - where H is the hour, i is the minutes, s is the seconds, d is the day, m is the month and Y is the year in four digits - e.g. date("H:i:s d/m/Y"); would return 12:22:01 10/08/2006 on 10th August 2006 at 12:22:01.
  • unlink("filename"); - Function which deletes the file specified in filename.

Basic Level

  $variable1 = 'beginning';
  //This is a comment after a variable being defined
  if ($variable1 == 'beginning') {
    //If is a test to see if a variable has certain
    //value and initiates the wanted sequences if true
    echo 'Hello World!';
    //The echo displays to the page

Include OOP based examples, made by experienced developer

PHP 5 Only


Basic Level


Basics, working only on PHP 5.

  • file_put_contents("filename", "Text to save"); - Functions which saves the text specified in Text to save to the file specified in filename. Will overwrite existing file contents unless another parameter FILE_APPEND is added.

E.g. file_put_contents("filename", "Text to save"); will write Text to save to filename, but will overwrite existing text whereas file_put_contents("filename", "Text to save", FILE_APPEND); will write Text to save to filename, but will not overwrite existing text (instead it appends).

  1. Input validation by Kgrsajid.
  2. Advanced Input validation by nemesiskoen.



For reviews of numerous PHP editors, see PHP-editors.


  • From C/C++ to PHP Most people program in C++ but not in PHP. This tutorial will explain the important differences.


Jatkins: PHP 4 & 5 examples, and PHP 5 only examples.
Douglas Clifton: New editor. Hoping to add more soon!
James Booker: Minor corrections. Hoping to add more content in time.
Spoom: Original for and articles, various reformatting and additions.
IBB: See profile.
Kander: Minor edits to the PHP and MySQL section.
Qrc: Started initial page for Configuration:Register Globals.
Bumppo: Started object-oriented PHP section
programmabilities: Minor edits.
ahc: Significant editing to existing sections.
Liu Chang: Added "Setting up PHP" section. Hoping to add more in time
Monkeymatt: Fixed some typos, fixed the templating section.
Charles Iliya Krempeaux: Added PHP CLI section. Minor cleanups on existing sections. Added PHP-GTK section.
scorphus: Fixes in installation procedures on Debian systems (to conform standards - we now use aptitude)
immortalgeek: Added some php web links to Resource section.
Wykis: Working on Smarty section, foreach, arrays, sessions, all basic programming
Bolo: working on Flat Frog section.
KGR Sajid: PHP5 editor. Also edited some other minor things.
banzaimonkey: Formatting changes and editing.
Meemo: Some small edits, fixing a few scripts and the spelling of Rumpelstiltskin. ;)
Justin Kestelyn: Added a link in Resources.
Sam Wilson: Elaboration on session fixation. -PHP, MySQL Training and development.