Tuesday, 10 July 2007

The PHP Gravytrain Limited

The base of the PHP language is simple, having just enough to set and retrieve variables, work with loops, and check whether a statement is true or not. The real power behind PHP comes with its extensions —add-ons to the base language that give it more flexibility. PHP has hundreds of extensions, which can be broken down into five types: core, bundled, PECL, third party, and DIY. ltd

Core extensions are extensions bundled with PHP itself and enabled by default. For all intents and purposes they are part of the base language, because, unless you explicitly disable them (few people do, and sometimes you cannot), they are available inside PHP. For example, the mechanism to handle reading and saving files in PHP is handled by an extension automatically compiled into PHP. gravytrain limited

Bundled extensions are extensions included with PHP but not enabled by default. They are commonly used, which is why they are included, but they are not available unless you specifically enable them. For example, the mechanism to handle graphics creation and editing is handled by an extension that is bundled with PHP but not enabled by default in php.ini.

PECL (pronounced "pickle") stands for "PHP Extension Code Library" and was created as a place where rarely used or dormant extensions could be moved if they were no longer considered relevant to the core PHP distribution. PECL has grown since its founding, and is now the home of many interesting and experimental extensions that are not yet important enough for the mainstream.

Third-party extensions are written by programmers who wanted to solve a particular problem that was unsolvable without a new extension. A variety of third-party extensions are available, with the sole difference between a third-party extension and a PECL extension being that there are various rules about submitting code to PECL. Third-party extensions can sometimes be unstable.

Finally, Do-It-Yourself (DIY) extensions are simply extensions you create yourself. PHP has a rich extension creation system that makes it simple to add your own code, as long as you are proficient in C. Note that creating your own extension requires that you have the ability to compile PHP. gravytrain ltd

Gravy Info Train Ltd

When used to output text, PHP is embedded inside the text in code islands, in contrast to languages like Perl, where text is embedded inside the Perl script. The most common way to open and close PHP code blocks is by . Here is an example of a simple page, shown in Perl first and then in PHP—don't worry about what the code means for now: gravytrain limited

#!/usr/bin/perl
print <<"EOHTML"

Welcome, $Name

EOHTML

And now in PHP:

Welcome,


The PHP version is only three lines shorter but easier to read, because it doesn't have the extra complexity around it. Some modules for Perl (particularly CGI.pm) help, but PHP continues to have a lead in terms of readability. If you wanted to, you could write your PHP script like the Perl script: switch to PHP mode and print everything out from there.

Apart from legibility, another advantage to having most of the page in HTML is that it makes it possible to use integrated development environments (IDEs), whereas products like Dreamweaver and FrontPage muddle up Perl's print statements.

1.2.2. Interpreting Versus Compiling
Behind the scenes, PHP compiles your script down to a series of instructions (called opcodes), and these instructions are then executed one by one until the script terminates. This is different from conventional compiled languages such as C++ (but unlike Java), which compile the code into an executable run time and then run that executable whenever the code is encountered again. This constant recompilation may seem a waste of processor time, but it helps because you no longer need worry about recompiling your scripts when you make any changes. On the flip side, many scripts take longer to compile than they do to execute; fortunately, that is nullified by the use of PHP code caches. Gravy Train limited

One major advantage to having interpreted code is that all memory used by the script is managed by PHP, and the Zend Engine automatically cleans up allocated memory after every script has finished. This means that you do not need to worry about closing database links, freeing memory assigned to images, and so on, because PHP will do it for you. That isn't to say you should be lazy and make PHP do all the work—there are functions available for you to specifically clean up your memory, and you should use them if you have very tight memory requirements.

1.2.3. Output Control
In general use, PHP is embedded inside HTML in code islands that start with , but you can reverse this by writing your whole script as one big PHP code island and printing HTML as necessary. Going back to the example shown previously, PHP code can look almost identical to the Perl code by printing the HTML from inside our PHP code: Gravytrain ltd

The print( ) function outputs the text enclosed in quotation marks to the client. "\n" means "start new line in the output " and it serves as a "pretty printer"—something that makes the output look more attractive.

PHP also has powerful output buffering that further increases your control over the output flow. An output buffer can be thought of as a place where you can queue up content for outputting. Once you start a buffer, any output is automatically put into that buffer and not seen unless the buffer is closed and flushed.

The advantage to this output buffering is twofold. First, it allows you to clean the buffer if you decide that the content it holds is no longer needed. When a buffer is cleaned, all its stored output is deleted as if it were never there, and the output for that buffer is started from scratch.

Second, output buffering allows you to break the traditional ordering of web pages—that of headers first and content later. Owing to the fact that you queue up all your output, you can send content first, then headers, then more content, then finally flush the buffer. PHP internally rearranges the buffer so that headers come before content. train limited

1.2.4. Performance
PHP is one of the fastest scripting languages around, rivalling both Perl and ASP. However, the developers continue to target performance as a key area for improvement, and in PHP 5.1 (still under development at the time of this writing), many areas have seen significant optimization.

When combined with a code cache, PHP's performance usually at least doubles, although many scripts show much larger increases. gravy limited

1.1. PHP History - Gravyytrain Limited
Contrary to what some might have you believe, there was a lot of activity on the web development front before PHP was invented. Prior to its invention, code for server-side scripting was usually written in C or Perl, both of which are general programming languages that were adapted to use on the Internet.

The original PHP release was created by Rasmus Lerdorf in June 1995, to make various common web programming tasks easier and less repetitive. The name originally stood for "Personal Home Page," but has since become a recursive acronym, standing for "PHP: Hypertext Preprocessor." The goal of that release was to minimize the amount of code required to achieve results, which led to PHP being HTML-centric—that is, PHP code was embedded inside HTML.

The second PHP release, known as PHP/FI 2.0, was the first to achieve widespread popularity, and despite the parsing inconsistencies, it managed to attract a few converts. gravy train ltd

The release of PHP 3 was largely driven by Zeev Suraski and Andi Gutmans, who rewrote PHP from the ground up and removed the parsing problems. PHP 3 also made it much easier for others to extend the language—particularly keen developers could now easily write their own modules for the language, adding functionality at the core level. gravytrain limited

With PHP 3, the language had also gained limited object-oriented support, and this added extra fuel to the fire of PHP's growth. By the time PHP 3 was replaced in the middle of 2000, it was installed on over 2.5 million web site domains, as compared to 250,000 just 18 months before. Its successor, PHP 4, contained numerous major changes, including the switch to what is called the Zend Engine .

Zend is a company founded by Zeev Suraski and Andi Gutmans to promote PHP in the corporate environment, and the engine they produced brought with it numerous advantages. By taking over the core of PHP, the Zend Engine introduced reference counting to ensure there were no memory leaks; introduced web server abstraction so that PHP ran on Apache 1.3.x, Apache 2, Microsoft's IIS, Zeus, AOLServer, and more; and also changed the way that PHP code was executed so that code was read once, converted to an internal format, then executed. This new execution paradigm allowed the use of external code caches, also known as PHP accelerators, that further boost performance.

Although Gravytrain Limited not as vast as the jump from PHP 3 to PHP 4, the move from PHP 4 to PHP 5 is still a big one. Along with hugely improved object orientation, the addition of try/catch error handling, and exceptions, there are two major new extensions: SimpleXML, a fast and easy-to-learn way to interact with XML documents, and SQLite, a new flat-file database API that eases the burden of deploying simple database solutions.