Home arrow PHP arrow Page 4 - Sample Chapter: PHP Pocket Reference

Sample Chapter: PHP Pocket Reference

Never worked with PHP? Curious? In this article Tim takes a look at a sample chapter from O'Reillys PHP Pocket Reference. This chapter is a great introduction to PHP and is explained simply and clearly.

Author Info:
By: Tim Pabst
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 15, 2002
  1. · Sample Chapter: PHP Pocket Reference
  2. · Introduction
  3. · Installation and Configuration
  4. · Including Files
  5. · Arrays
  6. · Control Structures
  7. · Functions
  8. · Showing the Browser and IP Address
  9. · Web Database Integration
  10. · Conclusion

print this article

Sample Chapter: PHP Pocket Reference - Including Files
(Page 4 of 10 )

An important feature of PHP is its ability to include files. These files may contain additional PHP tags. When you are designing a web application, it can be useful to break out some common components and place them in a single file. This makes it much easier to later change certain aspects in one place and have it take effect across the entire application. To include a file, you use the include keyword:

$title="My Cool Web Application";
include "header.inc";

The header.inc file might look as follows:

<TITLE><?echo $title?></TITLE>

This example illustrates two important concepts of included files in PHP. First, variables set in the including file are automatically available in the included file. Second, each included file starts out in HTML mode. In other words, if you want to include a file that has PHP code in it, you have to embed that code just as you would any PHP code.

Language Syntax
Variable names in PHP are case-sensitive. That means that $A and $a are two distinct variables. However, function names in PHP are not case-sensitive. This applies to both built-in functions and user-defined functions.

PHP ignores whitespace between tokens. You can use spaces, tabs, and newlines to format and indent your code to make it more readable. PHP statements are terminated by semicolons.

There are three types of comments in PHP:

/* C style comments */
// C++ style comments
# Bourne shell style comments

The C++ and Bourne shell style comments can be inserted anywhere in your code. Everything from the comment characters to the end of the line is ignored. The C-style comment tells PHP to ignore everything from the start of the comment until the end-comment characters are seen. This means that this style of comment can span multiple lines.

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

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

$var = "hello";

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

$$var = "World";

PHP parses $$var by first dereferencing the innermost variable, meaning that $var becomes "hello". The expression that is left is then $"hello", which is just $hello. In other words, we have just created a new variable named hello and assigned it the value "World". You can nest dynamic variables to an infinite level in PHP, although once you get beyond two levels, it can be very confusing for someone who is trying to read your code.

There is a special syntax for using dynamic variables inside quoted strings in PHP:

echo "Hello ${$var}";

This syntax is also used to help resolve an ambiguity that occurs when variable arrays are used. Something like $$var[1] is ambiguous because it is impossible for PHP to know which level to apply the array index to. ${$var[1]} tells PHP to dereference the inner level first and apply the array index to the result before dereferencing the outer level. ${$var}[1], on the other hand, tells PHP to apply the index to the outer level.

Dynamic variables may not initially seem that useful, but there are times when they can shorten the amount of code you need to write to perform certain tasks. For example, say you have an associative array that looks like this:

$array["abc"] = "Hello";
$array["def"] = "World";

Associative arrays like this are returned by various functions in the PHP modules. mysql_fetch_array( ) is one example. The indices in the array usually refer to fields or entity names within the context of the module you are working with. It can be handy to turn these entity names into real PHP variables, so you can refer to them as simply $abc and $def. This can be done as follows:

while(list($index,$value) = each($array)) {
$$index = $value;

Data Types
PHP provides three primitive data types: integers, floating point numbers, and strings. 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 can 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
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 range from 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:


Note that PHP also has a set of functions known as the BC (binary calculator) functions. These functions can manipulate arbitrary precision numbers. If you are dealing with very large numbers or numbers that require a high degree of precision, you should use these functions.

A string is a sequence of characters. A string can be delimited by single quotes or double quotes:

'PHP is cool'
"Hello, World!"

Double-quoted strings are subject to variable substitution and escape sequence handling, while single quotes are not. For example:

echo "Hello\t$a\n";

This displays "Hello" followed by a tab and then "World" followed by a newline. In other words, variable substitution is performed on the variable $a and the escape sequences are converted to their corresponding characters. Contrast that with:

echo 'Hello\t$a\n';

In this case, the output is exactly "Hello\t$a\n". There is no variable substitution or handling of escape sequences.

The following table shows the escape sequences understood by PHP:

Next: Arrays >>

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials