Using PHP


PHP is the result of another lazy programmer's effort to simplify his life. Back in 1994, Rasmus Lerdorf wanted to eliminate some of the drudgery associated with updating his personal web page. Lerdorf wrote some Perl scripts to generate HTML tags based on some C code. In June 1995, he announced the existence of the Personal Home Page (PHP) tools, version 1.0, in a Usenet CGI newsgroup. Those tools have since evolved into a full-fledged scripting language with a powerful engine, Zend, and a large community of developers hacking the code. You can read more about the history of PHP at http://php.net/history.

The PHP Home Page at www.php.net defines PHP as "a widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in." It is open source and works on the server side to generate its content. PHP often works with databases such as MySQL and PostgreSQL to generate content.

PHP boasts a command-line interpreter as well as modules that work with just about any web server out there, but we will focus on PHP's mod_php4 module for Apache. Similarly, PHP works with just about any database management system, but we will focus on MySQL.

Note

PHP 5.0 was released in July 2004, but was not included in the standard SUSE Linux distribution until v9.3 in April 2005. The instructions in this book apply to PHP 4, but nothing should be substantially different in the new version. See the section "The Future with PHP 5" for information on PHP 5's new features.


PHP is extensible through the PHP Extension and Application Repository (PEAR) and the PHP Extension Community Library (PECL), both hosted at php.net.

Installing PHP

The core PHP package, mod_php, and many PHP extensions are installable through YaST, making it easy to use PHP on a local Apache server setup. The core PHP package installs the basics of the language, which work from a command line. If you plan on using PHP in a CGI capacity, this is fine but this isn't the recommended (nor most beneficial) way. The apache_mod_php package is an Apache module that will allow you to process and serve HTML pages with embedded PHP code. You'll want to install both the core and mod_php packages.

To confirm that PHP is properly installed on your system, open a text editor and write this line:

<?php phpinfo(); ?> 

Save the file as /srv/www/htdocs/test.php. Now open a web browser to http://localhost/test.php. Figure 31.1 should appear.

Figure 31.1. If PHP is properly configured on your system, this page should appear in your web browser after writing the test file.


If you don't get the PHP info page, make sure the script was typed correctly, and then see if Apache is running by going to http://localhost. If Apache is not running, check the Runlevel Editor in YaST.

Look carefully at the info page. You will see which PHP extensions are installed, where your php.ini configuration file is stored (by default in /etc), and a fairly detailed configuration report.

If you are using a web-hosting vendor, make sure that PHP is supported before uploading PHP pages to your site. Finding a host with PHP support should not be difficult, but many PHP-related websites, like the PHP Resource Index at http://php.resourceindex.com, include pointers to PHP-friendly hosts.

Before writing any PHP code, you should also check that your favorite text editor or IDE supports PHP code highlighting and the like. All the web-authoring tools included in SUSE Linux (Bluefish, Quanta Plus, and Nvu) support PHP (although Nvu has some quirks, see http://www.viapanda.com/nvuphp.html for details). Emacs needs a separate php-mode, downloadable from Sourceforge.net (http://sourceforge.net/projects/php-mode). There are also PHP helper scripts for VIM at vim.org.

Writing PHP Scripts

You've already written your first PHP script the one-line test.php script you used to test your installation in the earlier section. There's more to learn than that, of course, and this is what you'll do in this section.

The PHP interpreter basically treats any documents fed to it as text and echos back the contents, unless it finds code set between special opening and closing tags. The neat thing about this is that you can embed your code in an HTML page and PHP won't touch your HTML. It will only process your code and the end result is a final HTML document with the PHP code replaced with the resulting output. This is demonstrated in Figure 31.2.

Figure 31.2. PHP interprets only your code delineated with <?php and ?>. The end result is a complete HTML document.


PHP's include function is used to import the contents of another file. This allows you to include other PHP scripts or even other HTML files into your scripts and use PHP to template your website:

<html> <h1>Welcome!</h1> <?php include "menu.html"; ?> <img src="/books/2/811/1/html/2/img/me.jpg" alt="picture of me" /> <p>Welcome to my website.  Here you'll learn all sorts of neat stuff about me.  Here's some of the things that are going on in my life right now:</p> <?php include "events.html"; ?> <p>Thanks for stopping by!</p> <hr /> <?php include "footer.html"; ?> </html> 

Variables are used in PHP to store information in memory that the script may need. They are identified by a $ and are not strictly typed like variables in other languages. This means a variable can hold a number, a string, or anything else that you may want to alias.

PHP also offers a variety of mathematical and logical operators to compare variables. By using them with PHP's flow control structures, you can write scripts that follow different paths, depending on the value of the variables. Table 31.1 shows some of the operators available in PHP.

Table 31.1. Selected PHP Operators

Meaning

Operator

Example

Assignment

=

$x = $y

Add

+

$x + $y

Subtract

-

$x - $y

Multiply

*

$x * $y

Divide

/

$x / $y

Calculates the remainder

%

$x % $y

Negation

-

-$x

Increment by 1

++

$x++

Decrement by 1

--

$x--

Equal to

==

$x == $y

Exactly equal to

===

$x === $y

Less than

<

$x < $y

Greater than

>

$x > $y

Less than or equal to

<=

$x <= $y

Greater than or equal to

>=

$x >= $y

Not equal to

!=

$x != $y

Logical NOT

!

!$x

Logical AND

&&

$x && $y

Logical OR

||

$x || $y

Group

()

($x = 5) && ($y = $z)


<?php   $forecast = "rain";   $times = 17;   if ($weater == "rain") {       echo "I've already told you ";       echo $times;       echo " times... if it's going to rain you should bring your umbrella!";   }   if ($weather != "rain") {       echo "Have a nice day!";   } ?> 

PHP also has looping structures that can be used to cycle through a section of code any number of times. This saves you the time of having to explicitly write the same section of code over and over. Here's an example that will zip through and write out all the numbers between 1 and 10,000 each on a separate line:

<?php   $x = 1;   while (x >= 10000) {       echo $x;       echo "<br />";       $x++;   } ?> 

Many predefined functions are available in PHP to help manipulate data, explore strings of text, and even generate graphical images and PDF files. From its simple Perl days, PHP has grown to be a very useful and robust scripting language. I highly recommend exploring the documentation available at http://www.php.net to learn more about the power of PHP.

Adding More Functions to PHP

PHP can be extended in a similar manner as Apache, where a modular design permits adding new features and functionality by linking in new modules. The PECL library is a set of modules that can link into the language's core. The new functions they provide are then available as any of PHP's built-in functions.

PEAR is a collection of reusable, open-source code for use in your PHP applications. The code is organized as a selection of files that act as wrappers giving convenient access to various PHP functions or extending PHP in some way. The files are collectively known as a package.

Both PECL modules and PEAR packages can be installed using the pear utility. Calling pear without any arguments will display a list of commands pear understands. The install argument followed by the package or module name will install it; the uninstall argument similarly will uninstall the component.

./pear install Mail downloading Mail-1.1.8.tgz ... Starting to download Mail-1.1.8.tgz (16,196 bytes) ......done: 16,196 bytes install ok: Mail 1.1.8 

The Future with PHP 5

PHP version 5.0 was released in June 2004 and was included with SUSE Linux Professional beginning with version 9.3.

PHP 5 features enhanced support for Extensible Markup Language (XML), using the libxml2 toolkit. Strong support exists for all XML-related technologies, but many are especially excited about the potential for ordinary users to create fully rendered PHP objects using the SimpleXML parser. With SimpleXML, you can read, write, or iterate over your XML file with ease in accessing elements and attributes.

If you're interested in building web services applications with PHP, there is new support for Simple Object Access Protocol (SOAP).

For programmers, the chief difference between the two versions is the introduction of object-oriented programming tools in the Zend Engine. PHP has had some object modeling available since version 3, but some experienced object-oriented programmers had problems with unusual syntax. In PHP 5, among the changes welcomed by programmers used to working with objects are the following:

  • You can use common access modifiers to limit access to certain classes and properties.

  • Constructor and destructor methods of dealing with objects are more consistent and easier to use.

  • A new final class declaration limits overloading inheritance.

  • Objects can be cloned with the clone keyword.

  • Classes may have constant values.

Other new enhancements include extensions that add support for the SQLite portable database library and HTML Tidy, a marvelous tool that cleans up your HTML and other markup code. You can also download a new Perl extension to support writing Perl in your PHP documents.

Visit http://www.zend.com/php5/whats-new.php for a more complete listing of the new features in PHP 5.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net