Rather than using a trademark symbol with every occurrence of a trademark name, logo or image, we use the names, logos and images only in an editorial manner and for the benefit of the trademark owner, with no intention to discredit the trademark. does not violate For more information, refer to our special Bulk Sales – eBook License webpage at www.apress.com/info/bulksales. Gilmore, LLC (www.wjgilmore.com), a consulting, publishing and training company with experience serving clients ranging from local law firms to Fortune 500 companies.
About the Technical Reviewer
Acknowledgments
Introduction
Download the Code
Contact Me!
Introducing PHP
Continued additions to the PHP toolset culminated in November 1997 with the release of PHP 2.0, or Personal Home Page/Form Interpreter (PHP/FI). Although officially a points release, PHP 5.3 is actually the most significant update to the language since the release of 5.0. Moreover, much of PHP's early evolution was not the result of the clear goal of improving the language itself, but of increasing its usefulness to users.
Configuring Your Environment
The php.ini file is a global PHP configuration file, similar to httpd.conf for Apache. The "PHP Configuration Directives" section that follows will introduce many of the directives found in the php.ini file. PHP_INI_SYSTEM: The directive can be changed in the php.ini and httpd.conf files.
WORKING WITH SINGLE AND DOUBLE QUOTES
Of the two methods for passing data between requests, POST is better equipped to transport large amounts, such as those that might be submitted through a web form. Enabling this parameter results in the automatic escaping (using a backslash) of any quotation marks placed within data returned from an external source, such as a database or text file. If you are using PHP in other ways, however, such as a content generator for WML (Wireless Markup Language) applications, you must adjust the MIME type accordingly.
By default, this is set to iso-8859-1, which supports languages such as English, Spanish, German, Italian, and Portuguese, among others. The path to which this parameter is set acts as the base path used by functions such as include(), require(), and fopen_with_path(). While there's nothing wrong with getting started writing PHP scripts using simple editors like Windows Notepad or vi, chances are you'll soon want to upgrade to a full-fledged PHP-specific development solution.
Designed as a one-stop application, Dreamweaver CS3 supports all the key technologies such as Ajax, CSS, HTML, JavaScript, PHP and XML that together power high-end websites. There are also capabilities to integrate code with popular databases such as MySQL, Oracle, PostgreSQL, and SQLite, in addition to the ability to run SQL queries and view and manage database schemas and data. Inexplicably, some hosting providers continue to require users to use outdated file extensions such as .phtml for PHP-enabled scripts.
Most hosting providers offer automatic installers for installing popular third-party software such as Joomla!, WordPress, and phpBB.
PHP Basics
Therefore, the engine needs some means of immediately determining which areas of the page are PHP-enabled. If you save this code as test.php and execute it from a PHP-enabled web server, you will see the output shown in Figure 3-1. This syntax, known as short-tags, forgoes the php reference required in the standard syntax.
Certain editors have had trouble in the past dealing with the more commonly used variants of PHP's escape syntax. If you have an ASP background and prefer to continue using this escape syntax, PHP supports it. The ASP style and Script demarcation variants are rarely used and should be avoided unless you have sufficient reason to do so.
You can switch to and from PHP whenever required within a given page. As you can see, any variable declared in a previous code block is remembered for subsequent blocks, as is the case with the $date variable in this example. Because of its brevity, there is no need to delimit comment termination, because the newline character (\n) meets this need quite well.
PHP also supports an alternative to C++-style single-line syntax known as parenthetical shell syntax.
ADVANCED DOCUMENTATION WITH PHPDOCUMENTOR Because documentation is such an important part of effective code creation and management,
Because this is not the ideal monetary representation, when working with decimal values, you can adjust the precision by using a precision specifier. This is accomplished by prepending the target type to the variable to be cast. Type casting a double to an integer will result in the integer value being rounded down, regardless of the decimal value.
The calculation uses the integer value at the beginning of the original string $total. Assignment by value simply means copying the value of the assigned expression to the assigned variable. However, the location of the declaration greatly affects the area in which a variable is accessible.
Changing the value of the local $x has no effect on any values placed outside the function. SERVER['HTTP_REFERER']: The URL of the page that referred the user to the current location. FILES['upload-name']['name']: The name of the file as uploaded from the client to the server.
Please note that this will result in the contents of $b being assigned to . If you want to learn more about binary encoding and bitwise operators and why they are important, check out Randall Hyde's massive online reference book, “The Art of Assembly Language. In this example, the backslash that appears at the end of the string must be escaped;
Functions
If you know the function name in advance, you can go directly to the function page by appending the function name to the end of the URL. This means that any changes to these values inside the scope of the function are ignored outside the function. If you want these changes to be reflected outside the scope of the function, you can pass an argument by reference presented next.
There may be times when you want any changes made to an argument within a function to be reflected outside the scope of the function. Passing an argument by reference is done by adding an ampersand to the front of the argument. Default values can be assigned to input arguments, which are automatically assigned to the.
The return() statement returns any subsequent value back to the function caller, returning program control to the caller's scope in the process. For example, suppose you want to create a function that retrieves user information from a database (for example, the user's name, email address, and phone number) and returns it to the caller. Recursive functions, or functions that call themselves, provide significant practical value to the programmer and are used to divide an otherwise complex problem into a simple case, repeating that case until the problem is solved.
Because this function calls itself recursively, all amortization table calculations will be performed internally in this function; when completed, control is returned to the caller.
Arrays
Note that the print_r() function is not the only way to print an array; it merely provides a convenient means of doing so. The array_keys() function returns an array consisting of all the keys in the array. The current() function returns the value of the array located at the current position of the array pointer.
The reset() function is used to reset an array pointer to the beginning of the array. The array_walk() function will pass each element of an array to the user-defined function. The array_unique() function removes all duplicate values found in an array and returns an array consisting only of unique values.
The array_flip() function reverses the role of the keys and their corresponding values in an array. The array_slice() function returns a section of an array based on a start and end offset value. Introduced in PHP 5.1.0, the array_intersect_key() function will return keys contained in an array that are contained in one of the other supplied arrays.
Introduced in PHP 5.1.0, the array_diff_key() function will return keys located in an array that are not in any of the other provided arrays.
Object-Oriented PHP
A class provides a base from which you can create specific instances of the entity it models, better known as objects. When an object is instantiated, all properties and behaviors defined in the class are available on the newly instantiated object. These methods are called when you try to refer to a member variable that does not exist in the class definition.
Methods that are intended solely to be helpers for other methods located within the class should be marked as private. For example, before retrieving information about a particular staff member, you might want to verify the employee identification number (EIN) passed as an argument to the class instantiator. The class_exists() function returns TRUE if the class specified by classname exists within the currently executing script context and returns FALSE otherwise.
The get_class() function returns the name of the class to which the object belongs and returns FALSE if the object is not an object. The get_class_methods() function returns an array containing all method names defined by the class_name class. The get_class_vars() function returns an associative array containing the names of all properties and their corresponding values defined within the class specified by class_name.
The get_parent_class() function returns the name of the parent of the class to which the object belongs.
Advanced OOP Features
Method Overloading: PHP does not support the ability to implement polymorphism through method overloading, and probably never will. Operator overloading: PHP does not currently support the ability to assign additional meanings to operators based on the type of data you are trying to modify. However, since all objects are treated as references rather than values, the object is now more difficult to copy.
To overcome the copying issues, PHP provides an explicit way to clone an object. The sample code instantiates a Corporate_Drone object and uses it as the basis for demonstrating the effects of a cloning operation. As you can see, $drone2 became an object of type Corporate_Drone and inherited the property values of $drone1.
You can customize an object's cloning behavior by defining a __clone() method within the object class. With that in place, let's create a new Corporate_Drone object, add the employeeId property value, clone it, and then export some data to show that the cloned object's tiecolor is indeed set by the __clone() -method is set. Because OOP is based on the premise of allowing people to carefully model the properties and behavior of the real environment we're trying to implement in code, it makes sense to be able to represent these hierarchical relationships as well.
These characteristics and behaviors would be relevant to all types of employees, regardless of the employee's purpose or stature in the organization.