Medium 9780596004026

PHP Pocket Reference

Views: 1083
Ratings: (0)

Simple, to the point, and compact--in fact, exactly what you've come to expect in an O'Reilly Pocket Reference--the second edition of PHP Pocket Reference is thoroughly updated to include the specifics of PHP 4.Written by the founder of the PHP Project, Rasmus Lerdorf, PHP Pocket Reference is both a handy introduction to PHP syntax and structure, and a quick reference to the vast array of functions provided by PHP. The quick reference section organizes all the core functions of PHP alphabetically so you can find what you need easily; the slim size means you can keep it handy beside your keyboard for those times when you want to look up a function quickly without closing what you're doing.This valuable little book provides an authoritative overview of PHP packed into a pocket-sized guide that's easy to take anywhere. It is also the ideal companion for O'Reilly's comprehensive book on PHP, Programming PHP.The PHP Pocket Reference an indispensable (and inexpensive) tool for any serious PHP coder.

List price: $7.99

Your Price: $6.39

You Save: 20%

 

14 Slices

Format Buy Remix

1.1. Introduction

ePub

 

1.2. Installation and Configuration

ePub

PHP works with many different web servers in many different ways, but by far the most popular way to run PHP is as an Apache module with Apache 1.3.x. Full installation instructions for all the different ways to install PHP can be found in the PHP documentation. Here, I cover the Apache module installation.

If you are compiling from the PHP source tarball, follow the instructions in the INSTALL file found inside the PHP distribution file. A tarball is a compressed tar file. tar stands for tape archive, but these days it has little to do with tapes. It is simply a way to lump multiple files and directories into a single file for distribution. Normally tarballs have the .tar.gz extension to indicate a tar file compressed with gzip. To untar a tarball, use:

On Windows, many utilities (including WinZip) understand tarballs.

If you are installing from a precompiled binary package such as an rpm file, most of the work should be done for you. But doublecheck that the Apache configuration described below is correct.

 

1.3. Embedding PHP in HTML

ePub

You embed PHP code into a standard HTML page. For example, here's how you can dynamically generate the title of an HTML document:

The <?echo $title?> portion of the document is replaced by the contents of the $title PHP variable. echo is a basic language statement that you can use to output data.

There are a few different ways to embed your PHP code. As you just saw, you can put PHP code between <? and ?> tags:

This style is the most common way to embed PHP, but it is a problem if your PHP code needs to co-exist with XML, as XML may use that tagging style itself. If this is the case, turn off this style in the php.ini file with the short_open_tag directive. Another way to embed PHP code is within <?php and ?> tags:

This style is always available and is recommended when your PHP code needs to be portable to many different systems. Embedding PHP within <script> tags is another style that is always available:

One final style, in which the code is between <% and %> tags, is disabled by default:

 

1.4. Language Syntax

ePub

 

1.5. Variables

ePub

In PHP, all variable names begin with a dollar sign ($). The $ is followed by an alphabetic character or an underscore, and optionally followed by a sequence of alphanumeric characters and underscores. There is no limit on the length of a variable name. Variable names in PHP are case-sensitive. Here are some examples:

In PHP, unlike in many other languages, you do not have to explicitly declare variables. PHP automatically declares a variable the first time a value is assigned to it. PHP variables are untyped; you can assign a value of any type to a variable.

PHP uses a symbol table to store the list of variable names and their values. There are two kinds of symbol tables in PHP: the global symbol table, which stores the list of global variables, and the function-local symbol table, which stores the set of variables available inside each function.

Sometimes it is useful to set and use variables dynamically. Normally, you assign a variable like this:

Now let's say you want a variable whose name is the value of the $var variable. You can do that like this:

 

1.6. Data Types

ePub

PHP provides four primitive data types: integers, floating point numbers, strings, and booleans. In addition, there are two compound data types: arrays and objects.

Integers are whole numbers. The range of integers in PHP is equivalent to the range of the long data type in C. On 32-bit platforms, integer values range from -2,147,483,648 to +2,147,483,647. PHP automatically converts larger values to floating point numbers if you happen to overflow the range. An integer can be expressed in decimal (base-10), hexadecimal (base-16), or octal (base-8). For example:

Floating point numbers represent decimal values. The range of floating point numbers in PHP is equivalent to the range of the double type in C. On most platforms, a double can be between 1.7E-308 to 1.7E+308. A double may be expressed either as a regular number with a decimal point or in scientific notation. For example:

PHP also has two sets of functions that let you manipulate numbers with arbitrary precision. These two sets are known as the BC and the GMP functions. See http://www.php.net/bc and http://www.php.net/gmp for more information.

 

1.7. Expressions

ePub

 

1.8. Operators

ePub

 

1.9. Control Structures

ePub

The control structures in PHP are very similar to those used by the C language. Control structures are used to control the logical flow through a PHP script. PHP's control structures have two syntaxes that can be used interchangeably. The first form uses C-style curly braces to enclose statement blocks, while the second style uses a more verbose syntax that includes explicit ending statements. The first style is preferable when the control structure is completely within a PHP code block. The second style is useful when the construct spans a large section of intermixed code and HTML. The two styles are completely interchangeable, however, so it is really a matter of personal preference which one you use.

The if statement is a standard conditional found in most languages. Here are the two syntaxes for the if statement:

The if statement causes particular code to be executed if the expression it acts on is true. With the first form, you can omit the braces if you only need to execute a single statement.

 

1.10. Functions

ePub

A function is a named sequence of code statements that can optionally accept parameters and return a value. A function call is an expression that has a value; its value is the returned value from the function. PHP provides a large number of internal functions. The "Function Reference" section lists all of the commonly available functions. PHP also supports user-definable functions. To define a function, use the function keyword. For example:

When you define a function, be careful what name you give it. In particular, you need to make sure that the name does not conflict with any of the internal PHP functions. If you do use a function name that conflicts with an internal function, you get the following error:

After you define a function, you call it by passing in the appropriate arguments. For example:

You can also create functions with optional parameters. To do so, you set a default value for each optional parameter in the definition, using C++ style. For example, here's how to make all the parameters to the soundcheck() function optional:

 

1.11. Web-Related Variables

ePub

PHP automatically creates variables for all the data it receives in an HTTP request. This can include GET data, POST data, cookie data, and environment variables. The variables are either in PHP's global symbol table or in one of a number of superglobal arrays, depending on the value of the register_globals setting in your php.ini file.

In PHP 4.2.0 and after, the default setting for register_globals is off. With register_globals off, all the various variables that are usually available directly in the global symbol table are now available via individual superglobal arrays. There is a limited set of superglobals and they cannot be created from a user-level script. The superglobal array to use depends on the source of the variable. Here is the list:

GET-method variables. These are the variables supplied directly in the URL. For example, with http://www.example.com/script.php?a=1&b=2, $_GET['a'] and $_GET['b'] are set to 1 and 2, respectively.

POST-method variables. Form field data from regular POST-method forms.

 

1.12. Sessions

ePub

Sessions are used to help maintain the values of variables across multiple web pages. This is done by creating a unique session ID that is sent to the client browser. The browser then sends the unique ID back on each page request and PHP uses the ID to fetch the values of all the variables associated with this session.

The session ID is sent back and forth in a cookie or in the URL. By default, PHP tries to use cookies, but if the browser has disabled cookies, PHP falls back to putting the ID in the URL. The php.ini directives that affect this are:

When on, PHP will try to use cookies

When on, PHP will add the ID to URLs if cookies are not used

The trans_sid code in PHP is rather interesting. It actually parses the entire HTML file and modifies/mangles every link and form to add the session ID. The url_rewriter.tags php.ini directive can change how the various elements are mangled.

Writing an application that uses sessions is not hard. You start a session using session_start( ), then register the variables you wish to associate with that session. For example:

 

1.13. Examples

ePub

The best way to understand the power of PHP is to examine some real examples of PHP in action, so we'll look at some common uses of PHP in this section.

Here is a simple page that prints out the browser string and the IP address of the HTTP request. Create a file with the following content in your web directory, name it something like example.php3, and load it in your browser:

You should see something like the following in your browser window:

Here is a slightly more complex example. We are going to create an HTML form that asks the user to enter a name and select one or more interests from a selection box. We could do this in two files, where we separate the actual form from the data handling code, but instead, this example shows how it can be done in a single file:

There are a few things to study carefully in this example. First, we have isolated the display of the actual form to a PHP function called show_form(). This function is intelligent, in that it can take the default value for each of the form elements as an optional argument. If the user does not fill in all the form elements, we use this feature to redisplay the form with whatever values the user has already entered. This means the user only has to fill the fields he missed, which is much better than asking the user to hit the Back button or forcing him to reenter all the fields.

 

1.14. Function Reference

ePub

The rest of this book provides an alphabetical summary of the functions that are available in PHP. The synopsis for each function lists the expected argument types for the function, its return type, and the version of PHP in which the function was introduced. The possible types are int, double, string, array, void, and mixed. mixed means that the argument or return type can be of any type. Optional arguments are shown in square brackets. Note that PHP didn't start tracking version numbers for functions until PHP 3.0, so functions that are listed as 3.0 are likely to have existed in Version 2.x.

As of PHP 4.3, approximately 2,750 functions came bundled with PHP. The bulk of these are in optional extensions. Out of these functions, I selected 1,404 for this pocket reference. Even with close to half the functions cut, I'm still pushing the limits of what the average pocket can hold without busting a few seams. Here's a list of the function groups that survived the cut, followed by the ones that didn't:

 

Details

Print Book
E-Books
Slices

Format name
ePub
Encrypted
No
Sku
9780596552169
Isbn
9780596552169
File size
0 Bytes
Printing
Not Allowed
Copying
Not Allowed
Read aloud
No
Format name
ePub
Encrypted
No
Printing
Allowed
Copying
Allowed
Read aloud
Allowed
Sku
In metadata
Isbn
In metadata
File size
In metadata