Introduction:
PHP is pretty amazing: A language strung together by unpaid assistant programmers that today has the enviable distinction of being in use on more than a third of the planet’s Web servers. Flexible, scalable, extensible, stable, open—PHP is all of these and more, which is why it’s one of the most popular programming toolkits in the world.
Ask me why I like PHP, though, and my explanation has nothing to do with any of the preceding buzzwords and everything to do with how friendly and nonthreatening the language is. There’s no tortuous syntax or obfuscated code in the average PHP script: instead, it’s clear, understandable, and easy to read, and this makes both programming and debugging with it a pleasure. This is no small achievement: a shorter learning curve makes it easier for novice programmers to quickly begin “doing something useful” with the language, and increases both user interest and adoption levels. This isn’t just good design: it’s smart marketing as well! As an open-source project, PHP is completely free, and supported by a worldwide community of volunteers. This open-source, community-driven approach has produced a platform that is significantly more robust and error-free than many commercial alternatives. So using PHP is also good economics for organizations: it allows them to save on licensing fees and expensive server hardware, while simultaneously producing higher-quality products in shorter time frames.
Key Skills & Concepts
● Know PHP’s history
● Learn PHP’s unique capabilities for Web application development
● See how the components of a PHP system interact
● Understand the basic grammar and structure of a PHP script
● Create and run a simple PHP program
● Embed PHP in an HTML page
● Understand PHP’s simple data types
● Become familiar with some of PHP’s built-in functions
● Perform arithmetic operations
● Compare and logically test variables
● Handle data submitted through a Web form
Basic Development Concepts:
When developing a PHP application for the Web, the typical approach is to embed PHP code into one or more standard HTML documents using special “tags,” or delimiters.
Components PHP Applications:
● A base operating system (OS) and server environment (usually Linux)
● A Web server (usually Apache on Linux or IIS on Windows) to intercept HTTP
requests and either serve them directly or pass them on to the PHP interpreter for
execution
● A PHP interpreter to parse and execute PHP code, and return the results to the
Web server
There’s also often a fourth optional but very useful component:
● A database engine (such as MySQL) that holds application data, accepts connections from the PHP layer, and modifies or retrieves data from the database
An important corollary of this approach is that the PHP code is executed on the server, and not on the client browser. This allows Web developers to write program code that is completely independent of, and thus impervious to, browser quirks—an important advantage over client-side scripting languages, such as JavaScript, which often require complex logic to account for browser-specific differences. Further, because the code is all executed on the server and only the output is sent to the client, it is impossible for users to see the source code of your PHP program—an important security advantage over languages like JavaScript
MIXING WITH HTML CODING:
When the PHP parser reads a script, it executes only the code found between PHP tags; everything outside these tags is ignored by the parser and returned “as is.” This makes it extremely easy to embed PHP code within an HTML document to create Web pages that have all the formatting bells and whistles of standard HTML but can additionally perform complex calculations or read and manipulate data from external sources
The values assigned to a PHP variable may be of different data types, ranging from simple string and numeric types to more complex arrays and objects. You’ve already seen two of these, strings and numbers, in action in previous examples. Here’s a full-fledged example, which introduces three more data types:
● Booleans are the simplest of all PHP data types. Like a switch that has only two states, on and off, it consists of a single value that may be set to either 1 (true) or 0 (false). In this listing, $validUser is a Boolean variable set to true.
● PHP also supports two numeric data types: integers and floating-point values. Floatingpoint values (also known as floats or doubles) are decimal or fractional numbers, while integers are round numbers. Both may be less than, greater than, or equal to zero. In this listing, $size holds an integer value, while $temp holds a floating-point value.
● For non-numeric data, PHP offers the string data type, which can hold letters, numbers, and special characters. String values must be enclosed in either single quotes or double quotes. In the previous listing, $cat is a string variable containing the value 'Siamese'.
● You may also encounter the NULL data type, which is a “special” data type first
introduced in PHP 4. NULLs are used to represent “empty” variables in PHP; a variable of type NULL is a variable without any data. In the preceding listing, $here is NULL.
What Does PHP Do?
PHP does anything you want, except sit on its head and spin. Actually, with a little
on-the-fly image manipulation and dynamic HTML, it could probably do that, too. According to the PHP manual, “The goal of the language is to allow Web
developers to write dynamically generated pages quickly.”
Here are some common uses of PHP, all of which are a part of what you’ll learn in
this book:
• Perform system functions: create, open, read from, write to, and close files on
your system; execute system commands; create directories; and modify
permissions.
• Gather data from forms: save the data to a file, send data via e-mail, and return
manipulated data to the user.
• Access databases and generate content on the fly, or create a Web interface for
adding, deleting, and modifying elements within your database.
• Set cookies and access cookie variables.
• Start sessions and use session variables and objects.
• Restrict access to sections of your Web site.
• Create images on the fly.
• Encrypt data.