PHP 4 Bible

september 12, 2011

Tim converse and Joyce Park

PHP is embedded.
PHP is embedded within HTML. In other words, PHP pages are ordinary HTML pages that “escape” into PHP mode only when necessary. Here is an example:
<HTML><head><title>A greeting</title></head><body><P>Hi,<?php
/* We have now escaped into PHP mode. Instead of static variables, the next three lines could easily be database calls. */
$firstname = “Mata”; $lastname = “Hari”; $title = “Ms. “; PRINT (“$title $lastname”);
// We’re about to go back to HTML now. ?>.
May I call you <?php PRINT (“$firstname”); ?> ?</p></body></html>
When a client requests this page, the web server preprocesses it. This means it goes through the page from top to bottom, looking for sections of PHP, which it will try to resolve. For one thing, the parser will suck up all assigned variables (marked by dollar signs) and try to plug them into later PHP commands (in this case, the print () function ). If everything goes smoothly, the preprocessor will eventually return a normal HTML page to the client’s browser, as shown in Figure 1-1.

If you peek at the source code from the client browser (select “source” or “page source” from the view menu, or right click on AOL), it will look like this:
<html><head><title>A greeting</title></head><body><p>Hi, Ms. Hari. May I call you Mata?</p></body></html>
This is exactly the same as if you wrote the HTML by hand. So simple!
The HTML-embeddedness of PHP has many helpful consequences:
*PHP can quickly be added to code produced by WYSIWYG editors.
*PHP lends itself to a division of labor between designers and scripters.
*Every line of HTML does not need to be rewritten in a programming language.
*PHP can reduce labor costs and increase efficiency.
PHP isn’t tag-based
PHP is a real programming language. Coldfusion, by contrast, is a bunch of predefined tags, like HTML. In PHP, you can define functions to your heart’s content just by typing a name and a definition. In ColdFusion, you have to use tags developed b other people, or go through the custom tag extension development process. As a witty PHP community member once said, “Coldfusion makes easy things easy, and a medium-hard thing impossible.” And as every programmer will agree: once you experience the power of curly brackets and loops, you never go back to tags.

Summary:
PHP isn’t  the panacea for every web development problem, but it has a lot of advantages. It’s built by web developers for web developers and supported by a large and enthusiastic community. It sits on a sweet-spot of lightness, power, reliability, and easy of use. It offers best-of-breed connectivity to backend servers of all types. And did we mention it’s free? To know PHP is to love it for many of the most common Web development tasks have been selected.

Programming versus scripting.
The difference between programming and scripting is being increasingly blurred by recent developments in programming languages. For instance, PHP definitely uses most of the same control structures as other programming languages. However, fully-interpreted HTML-embedded languages such as ASP are still considered to be on the scripting side of the line, whereas separately compiled binaries are a definite mark of programming. But since PHP 4 is now dynamically compiled (the compiled binary is stored and reused until the source code changes), it’s officially a “real” programming language – and don’t let anyone tell you otherwise. This change accounts for much of the screaming speed of PHP 4, which moves into the same class as Perl.

Advertisements

Legg igjen en kommentar

Fyll inn i feltene under, eller klikk på et ikon for å logge inn:

WordPress.com-logo

Du kommenterer med bruk av din WordPress.com konto. Logg ut / Endre )

Twitter picture

Du kommenterer med bruk av din Twitter konto. Logg ut / Endre )

Facebookbilde

Du kommenterer med bruk av din Facebook konto. Logg ut / Endre )

Google+ photo

Du kommenterer med bruk av din Google+ konto. Logg ut / Endre )

Kobler til %s

%d bloggers like this: