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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Sample Chapter: PHP Pocket Reference - Arrays
(Page 5 of 10 )

An array is a compound data type that can contain multiple data values, indexed either numerically or with strings. For example, an array of strings can be written like this:

$var[0]="Hello";
$var[1]="World";


Note that when you assign array elements like this, you do not have to use consecutive numbers to index the elements.

As a shortcut, PHP allows you to add an element onto the end of an array without specifying an index. For example:

$var[] ="Test";

PHP picks the next logical numerical index. In this case, the "Test" element is given the index 2 in our $var array: if the array has non-consecutive elements, PHP selects the index value that is one greater than the current highest index value. This auto-indexing feature is most useful when dealing with multiple-choice HTML <SELECT> form elements, as we'll see in a later example.

Although we have called strings a primitive data type, it is actually possible to treat a string as a compound data type, where each character in the string can be accessed separately. In other words, you can think of a string as an array of characters, where the first character is at index 0. Thus, you can pick the third character out of a string with:

$string[2]

Arrays can also be indexed using strings; these kinds of arrays are called associative arrays :

$var["January"]=1;
$var["February"]=2;


In fact, you can use a mix of numerical and string indices with a single array. That is because internally PHP treats all arrays as hash tables and the hash, or index, can be whatever you want.

All arrays in PHP can be traversed safely with the following mechanism:

while(list($key,$value)=each($array)) {
echo "array[$key]=$value<br>\n";
}


This is the most common way to loop through each element of an array, whether it is a linear or an associative array. PHP provides a number of array manipulation functions; these are detailed later in the "Function Reference" section.

Objects
An object is a compound data type that can contain any number of variables and functions. PHP's support for objects is very basic in Version 3. PHP Version 4 will improve the object-oriented capabilities of PHP. In PHP 3.0 the object-oriented support is designed to make it easy to encapsulate data structures and functions in order to package them into reusable classes. Here's a simple example:

class test {
var $str = "Hello World";
function init($str) {
$this->str = $str;
}
}

$class = new test;
print $class->str;
$class->init("Hello");
print $class->str;


This code creates a test object using the new operator. Then it sets a variable called str within the object. In object-speak, a variable in an object is known as a property of that object. The test object also defines a function, known as a method, called init( ). This method uses the special-purpose $this variable to change the value of the str property within that object.

If you are familiar with object-oriented programming, you should recognize that PHP's implementation is minimal. PHP3 does not support multiple inheritance, data protection (or encapsulation), and destructors. PHP does have inheritance and constructors, though.

Boolean Values
Every value in PHP has a boolean truth value (true or false) associated with it. This value is typically used in control structures, like if/else and for. The boolean value associated with a data value is determined as follows:
  • For an integer or floating point value, the boolean value is false if the value is 0; otherwise the boolean value is true.
  • For a string value, the boolean value is false if the string is empty; otherwise the boolean value is true.
  • For an array, the boolean value is false if the array has no elements; otherwise the boolean value is true.
  • For an object, the boolean value is false if the object has no defined variables or functions; otherwise the boolean value is true.
  • For an undefined object (a variable that has not been defined at all), the boolean value is false.
PHP has two built-in keywords, true and false, where true represents the integer value 1 and false represents the empty string.

Type Casting
Variables in PHP do not need to be explicitly typed. PHP sets the type when a variable is first used in a script. You can explicitly specify a type using C-style casting.

For example:

$var = (int) "123abc";

Without the (int) in this example, PHP creates a string variable. With the explicit cast, however, we have created an integer variable with a value of 123. The following table shows the available cast operators in PHP:



Although they are not usually needed, PHP does provide the following built-in functions to check variable types in your program: gettype( ), is_long( ), is_double( ), is_string( ), is_array( ), and is_object( ).

Expressions
An expression is the basic building block of the language. Anything with a value can be thought of as an expression. Examples include:

5
5+5
$a
$a==5
sqrt(9)

By combining many of these basic expressions, you can build larger and more complex expressions.

Note that the echo statement we've used in numerous examples cannot be part of a complex expression because it does not have a return value. The print statement, on the other hand, can be used as part of complex expression, as it does have a return value. In all other respects, echo and print are identical--they output data.

Operators
Expressions are combined and manipulated using operators. The following table shows the operators available in PHP, along with their precedence (P) and associativity (A). The following table lists the operators from highest to lowest precedence. These operators should be familiar to you if you have any C, Java, or Perl experience.


blog comments powered by Disqus
PHP ARTICLES

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

Developer Shed Affiliates

 




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