Chapter 1
From Phpmaniac
Contents |
Introduction
This book is for people interested in learning the PHP scripting language. It caters for people who learn visually by example. We will walk through many examples of different uses for PHP whether it be for building dynamic web pages, or scripting system administration tasks, this book can will be a title to have on any developers bookshelf.
The book will primarily focus on using PHP to develop dynamic web pages using different data sources including text files, databases, email, and an API (Application programming interface). We will additionally look into installing and configuring PHP on a number of platforms, command line scripts, and extending PHP by writing our own functions and classes.
About the Author
Leigh Finch has been developing projects in PHP for the last six years after seeing Rasmus Lerdorf present a seminar on PHP at the Australian National University in 2002. Since then he has developed projects for companies in Australia and The Peoples Republic of China.
What is PHP
PHP is the “PHP: Hypertext Preprocessor “ which is a recursive acronym. it was first developed by Rasmus Lerdorf in 1994 for his personal website. In 1995 Rasmus released PHP publicly naming it PHP 2. This early version contained the basic functionality that we see in PHP today such as embedding in code HTML. In 1998 PHP 3 was released which included the rewritten parser. In 2000 PHP 4 was released which included the Zend engine version 1. In 2004 PHP 5 was released which included the Zend engine version 2, with enhanced Object Orientation capabilities.
Syntax
PHP Draws upon C based languages such as C, C++ and Java for it's syntax. What this means is that people already familiar with the aforementioned languages will find PHP very easy to get started. Whitespace in PHP is not significant, but for clear code reading I recommend you use an established indenting method, I use the Allman style, which puts a brace at the same indentation on the line below the control statement preceding it, and code contained within a set of braces to be indented.
- if ($test == true)
- {
- exit();
- }
Functions
Functions provide the ability to do many tasks such as string manipulation, connections to databases, write to or reading from files, and printing to output. Most functions have a return value which will give you the result of the function you have called.
Many of the functions in PHP are named after their C counterparts allowing a C or C++ developer jump right into the world of PHP.
Functions are called by naming the function, and placing an open parenthesis, any variables or results of any code you want to pass to the function, followed by a close parenthesis.
- print('Hello World!');
To capture the result of the above print you would place a variable followed by an equals symbol.
- $result = print('Hello World!');
Variables
Variables are data containers we use to store information in memory. This information can be a number, text, or true/false data.
The format for naming variables is a text string prefixed with a dollar. The first character in a variable after the dollar symbol must not be a number.
- $age = 25;
Naming a variable with a number as the first character will result in an error simlar to:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in number.php on line 2
Types
Types is the word used to describe what kind of data is held in a variable. In PHP variables are dynamically typed which means that a type information is not associated with the variable itself, but the data within that variable.
Booleans
Booleans are the simplest data type, it is either true or false (1 or 0 respectively).
- $is_male = true;
Strings
Strings are text, and probably the most commonly used data types in PHP for web development. All strings must be encapsulated in inverted commas.
- $error_message = 'You must enter a contact phone number.';
Strings are an important part of PHP and a whole chapter is dedicated to it
Integers
An Integer is a number that has a maximum and a minimum value which varies depending on the platform you are using.
- $member_number = 500;
Floating Point Numbers
Floating point numbers can be numbers with decimals, or large numbers with some loss of precision. Like integers floating point numbers have a maximum and a minimum which varies from platform to platform.
- $height = 175.6;
Arrays
Arrays Arrays are like lists of variables that you want to group together, unlike languages like C and Java, arrays can be lists of different types of data, making it very flexible.
- $books = array();
- $books[0] = 'My little pony';
- $books[1] = 'Tomorrow when the war began';
Resources
Resources are connections to data source and similar to a file pointer in C. Resources can be connections to files, databases, and llocal or remote tcp/udp connections.
- $file_pointer = fopen(“visits.txt”, “r”);
Objects
Objects are instances of classes, which may contain a group of related functions, or the ability to interface with another system among other things.
- $blogger = new blogger_class();
Tags
The Open Tag in PHP tells the interpreter to start php mode.
- <?php
The above tag is the full open tag, although PHP 5 supports the use of short tags (<?, <?=, <%) the use of them should be discouraged as they are likely to be deprecated in PHP 6, thus making your code incompatible. The Close Tag in PHP tells the interpreter to exit PHP mode.
- ?>
Comments
Comments in your Code are important, as they allow you to look at a section of code, and quickly understand what it does a year or more later, it also means that other people can look at your code and understand what it does
- // Single line comment
- /*
- Multi
- line
- comment
- */
- # Shell style comment

