Programming:PHP

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search



Image:PHPWikibookCover.png

See also Programming:Complete PHP


[edit] What is PHP?

PHP is a scripting language designed to fill the gap between SSI (Server Side Includes) and Perl, intended largely for the web environment. PHP has gained quite a following in recent times, and it is one of the forerunners in the Open Source software movement. Its popularity derives from its C-like syntax, its speed and its simplicity. PHP is currently divided into two major versions: PHP 4 and PHP 5. PHP 6 is currently under development.

If you've ever been to a website that needs you to login, you've probably encountered a server-side scripting language. Due to its market saturation, this means you've probably come across PHP. PHP was designed by Rasmus Lerdorf to display his resume online and to collect data from his visitors.

Basically, PHP allows a static web document to become dynamic. "PHP" is a recursive acronym that stands for "PHP: Hypertext Preprocessor". PHP preprocesses (that is, PHP processes before the output is sent to the browser) hypertext documents. Because of this, the pages can change before the user sees them, based on conditions. This can be used to write something to the page, create a table with a number of rows equal to the number of times the user has visited, or integrate the web page with a web database, such as MySQL.

Before you embark on the wonderful journey of Server Side Processing, it is recommended that you have some basic understanding of the HyperText Markup Language. PHP is also being used to build GUI-driven applications; PHP-GTK is used to build Graphical User Interfaces.

[edit] Setup and Installation

Uses of PHP

Note: Before contributing, check out the Talk page. How to write your examples.

[edit] Learning the Language

[edit] 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!" Development stage: 100% (as of 10 Feb 2006) (10 Feb 2006)
  2. Nuts and Bolts Development stage: 75% (as of 14 Jan 2006) (14 Jan 2006)
  3. Commenting and Style Development stage: 100% (as of 14 Jan 2006) (14 Jan 2006)
  4. Arrays Development stage: 75% (as of 10 Feb 2006) (10 Feb 2006)
  5. Control structures
    1. The if Structure Development stage: 50% (as of 14 Jan 2006) (14 Jan 2006)
    2. The switch Structure Development stage: 75% (as of 14 Jan 2006) (14 Jan 2006)
    3. The while Loop Development stage: 75% (as of 14 Jan 2006) (14 Jan 2006)
    4. The do while Loop Development stage: 50% (as of 14 Jan 2006) (14 Jan 2006)
    5. The for Loop Development stage: 50% (as of 14 Jan 2006) (14 Jan 2006)
    6. The foreach Loop Development stage: 75% (as of 14 Jan 2006) (14 Jan 2006)
  6. Functions Development stage: 75% (as of 10 Feb 2006) (10 Feb 2006)
  7. Files Development stage: 100% (as of 12 Feb 2008) (12 Feb 2008)
  8. Mailing Development stage: 50% (as of 14 Jan 2006) (14 Jan 2006)
  9. Cookies Development stage: 75% (as of 14 Jan 2006) (14 Jan 2006)
  10. Sessions Development stage: 75% (as of 2008-05-07) (2008-05-07)
  11. Databases
    1. MySQL Development stage: 75% (as of 14 Jan 2006) (14 Jan 2006)
    2. PHP Data Objects Development stage: 00% (as of 18 Dec 2006) (18 Dec 2006)
  12. Integration Methods (HTML Forms, etc.) Development stage: 75% (as of 14 Jan 2006) (14 Jan 2006)

[edit] Advanced PHP

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

[edit] Object Oriented Programming (OOP)

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

[edit] Templating

  1. Why Templating
  2. Templates
  3. Caching
  4. SMARTY templating system
  5. PRADO Component Framework
  6. Flat Frog templating system Development stage: 25% (as of 23 march 2006) (23 march 2006)
  7. XSL

[edit] Libraries

PHP PEAR

[edit] Frameworks

CodeIgniter
CakePHP
Zend Framework
Symfony

[edit] Security

  1. Configuration: Register Globals
  2. SQL Injection Attacks
  3. Cross Site Scripting Attacks

[edit] Command-Line Interface (CLI)

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

[edit] Code snippets

Useful for any beginners to learn code from.

[edit] PHP 4 & 5

[edit] 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:

Bobby

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

$var

  • $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:

1214

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

[edit] PHP 4

[edit] Basic level

Include beginner code samples, which are made by the beginners

[edit] OOP

Include OOP based examples, made by experienced developers

[edit] PHP 5 Only

[edit] 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).

[edit] OOP

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

[edit] Editors

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

[edit] Resources

[edit] Contributors

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 switch...case 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.
Personal tools