This article is a follow-up to Free
Software Profile: PHP, which did not deal with PHP4's new
features.
|
The
long-awaited PHP4 version has been available since the end of
May 2000. After having acquired successively Beta-test and
Release Candidate status, the final version which can be used
for real-world site production is today a
reality. |
The questions that come up
systematically with each new version of a software program are, 'What's
new in this version?' and 'Which of these new features are truly useful?'
These are certainly legitimate questions, as the simple availability of a
new version does not justify migration to it. The thing is that behind
what seems to be a simple update looms a myriad of potential problems that
must be zeroed in on before moving forward, especially because once
updated, there's often no turning back.
I'd like to take a minute to
point out that if you are currently questioning the availability date of
PHP4 support by your favorite host, it is important to know that such a
task represents a considerable amount of work.
In this article, we'll take a
quick look at PHP4's new features, which will allow you to appreciate the
tremendous work that has been carried out by Zeev Suraski and Andi
Gutmans, as well as all of the other contributors.
What's the difference between PHP3 and
PHP4?
PHP4 is a complete rewrite of
PHP3 at the heart of the language's engine. But why did the language's
authors decide to completely rewrite it?
The manner in which PHP3's scripting engine works is not
adapted to the execution of rich, large-scale applications such as
Phorum or KeyStone. Indeed, this engine reads PHP script
instructions line-by-line and then executes them one-by-one, which enables
PHP3 to obtain good performance with short and simple scripts, but yields
rather mediocre performance with larger applications. It is important to
understand that PHP3 was not designed to execute such
applications.
It is for this reason that
PHP3's two creators decided to implement a scripting engine that would be
capable of reading all PHP code and "compiling" it before executing it:
Zend. This was not an easy task. Although incompatibilities could be
conceived at the time of migration from PHP2 to PHP3, such differences had
to be limited as much as possible between PHP3 and PHP4 due to the
installed base and the number of PHP3 scripts already in use. We can
congratulate PHP4's authors on a job well done because the
incompatibilities that do exist are only minor.
Although Zend is an integral
part of PHP4, it is in fact a scripting/runtime engine that is independent
of the PHP language. On this account, it is possible to use Zend in MySQL
as the basis for the development of a stored procedure language specific
to this database system.
Without going into the
details of internal implementation, we can point out a few things about
the Zend/PHP4 couple.
PHP4's new architecture
allowed its authors to add an abstraction layer in relation to the Web
server at the heart of its language. Up until now with PHP3, we had the
choice between using it as a specific module for the Apache server or in
CGI mode for all other HTTP servers. With PHP4 it is now possible to
imagine better integration with Web servers other than Apache. In fact,
this is the case for Microsoft's HTTP server, IIS. PHP4 now works as an
ISAPI filter for Microsoft's IIS server. For this reason, different parts
of PHP4 code had to be made compliant with use in a multi-thread
environment (PHP4 is "thread-safe").
There are many new features
concerning PHP4's internal implementation, but they don't really concern
PHP programmers directly. Nonetheless, it is worth noting one of the most
important points: memory allocation and reference counting mechanisms, in
other words those mechanisms that free up resources which were no longer
used, were completely rethought. Performance and memory use are thus
improved, especially for objects and tables. The more data the variables
contain, the more memory is saved.
A Developer's View
What are the major
differences between PHP3 and PHP4 from a user's (a PHP4
designer/developer) point of view?
Sessions
One of PHP4's most
anticipated functions is native session support. Indeed, nothing in the
language's base functions provided for management of user sessions, in
other words all of the values and variables that are linked to a user and
can be transferred from page to page within the same
application.
With PHP3, it was necessary
to build a session system manually by using cookies, by making an
identifier a parameter, or by using PHPLib. PHPLib can still be used with
PHP4 because it offers other functionalities besides session
management.
In PHP4, a whole group of configuration features and
directives in the php.ini file take the burden of session and user
context management off of the programmer. PHP4 session variables can
be stored in a simple ASCII file or in a relational database. All of
these features are now documented in the official PHP
manual.
Output Buffering
With PHP3 all of the content produced
by echo () or print() was directly sent to the server. In PHP4 an
intermediate storage layer makes it possible to defer the sending the Web
server's output flow. In order to do this, new features, which haven't
been documented yet, are available:
- ob_start(): activates
output buffering
- ob_end_flush(): sends the
contents of the output buffer and deactivates buffering
- ob_end_clean(): empties the
output buffer and deactivates buffering
- ob_get_contents(): returns
the output buffer contents
These functionalities make it possible to prepare the
contents that are displayed via complex processes requiring several
database queries, for example, and the sending of results only when all of
the queries have been carried out successfully. In the opposite case, you
can empty the output buffer contents and display an error message in its
place. The example below shows a typical case of how these functionalities
are used:
|
<?php // activate output
buffers ob_start(); // the output is not displayed, it
enters the buffer print "Hello, world!"; // read buffer
contents $output = ob_get_contents(); // deactivate the
buffer, empty the buffer ob_end_clean(); // display
contents print
$output; ?> |
Evaluate for Identical Operator
A new evaluate
for identical operator makes its debut in PHP4. This operator makes
it possible to test the equality of the values and types of two
different variables or expressions. With implicit PHP-type
conversions,the value 5 could be compared with the string containing
the character "5." These two values were considered equal in a
comparison. The new operator is represented by the three equal signs
("= = =") and only sends back TRUE if the variable type and value
are identical. Thus, in the following example, the four tests using
the equality operator with the numbers 1 to 4 are true, while with
the following four, which use the evaluate for identical operator,
only tests 5 and 8 are true.
$valint1 =
1; $valint2 = 2; $valint3 = 3;
$valfloat1 =
1.0; $valfloat2 = 2.0; $valfloat3 = 3.0;
$valstr3 =
'3';
$res = (($valint1+$valint2)==$valint3) ?
('TRUE'):('FALSE') ; print " TEST 1 : " .
$res . "<br>\n";
$res =
($valint3==$valfloat3) ? ('TRUE') : ('FALSE') ; print " TEST 2
: " . $res . "<br>\n";
$res =
($valstr3==$valint3) ? ('TRUE') : ('FALSE') ; print " TEST 3 :
" . $res . "<br>\n";
$res =
(($valfloat1+$valfloat2)==$valfloat3) ? ('TRUE') : ('FALSE') ;
print " TEST 4 : " . $res .
"<br>\n<br>";
"; $res =
(($valint1+$valint2)===$valint3) ? ('TRUE') : ('FALSE')
; print " TEST 5 : " . $res . "<br>\n";
$res =
($valint3===$valfloat3) ? ('TRUE') : ('FALSE') ; print " TEST
6 : " . $res . "<br>\n";
$res =
($valstr3===$valint3) ? ('TRUE') : ('FALSE') ; print " TEST 7
: " . $res . "<br>\n";
$res =
(($valfloat1+$valfloat2)===$valfloat3) ? ('TRUE') : ('FALSE')
; print " TEST 8 : " . $res .
"<br>\n"; |
In passing, it is worth noting that PHP4 now includes
Boolean-type data.
COM support on Windows
On Windows platforms, PHP4
now offers support for COM components. This means that practically
all Windows applications can be manipulated from PHP4. The COM model
that is at the heart of Microsoft's architecture is such that any
Windows application can provide a certain number of components and
exposed methods, which are entry points for manipulating
applications from other applications.
Since a few lines of code tend to be more useful than a long
description, let's look at the following example. The code below
does nothing less than boot Word, create a new document from it, add
text to this document, and then save everything on the disk before
quitting.
<?php $word = new COM("word.application")
or die("Impossible to instantiate
WordApp"); print "Word is running, version
{$word->Version}\n</br>";
$word->Visible=1;
$word->Documents->Add(); $word->Selection->TypeText("This
is a test...");
$word->Documents[1]->SaveAs("test_com_php.doc");
$word->Quit();
?> |
Displaying portions of raw HTML code
Another newcomer to PHP4 is a new syntax that is
modeled after Perl's "here printing." It is possible to display complete
portions of HTML code by flanking them with either a print or echo
instruction and an end label. Unlike Perl, the operator used is not a
double less than sign (<<), but a triple less than sign
(<<<) so as to distinguish it from the binary shift operator. For
example:
<?php
$version =
phpversion(); $valstr = "Example of
HerePrinting\n";
echo
"<hr><br>/n";
print
<<< mylabel
<h4>PHP's New
Features</h4> <ul> <li>ISAPI
support <li>COM support on Win32 <li>Java
and servlets support </ul>
<p>$valstr <br>Tested on PHP version
$version</p>
mylabel;
echo
"<hr><br>/n"; ?> |
Onto Page II: Java Support
TechMetrix Research is a technically focused analyst firm focused on e-business application development needs. Based in Boston, Mass., the firm publishes comparison reports and product reviews designed to aid enterprises with decision making and to keep pace with the fast-moving e-business market.
TechMetrix is a U.S.-based subsidiary of SQLI, a European company that offers on-site development services to international organizations. SQLI specializes in e-business project development.