Introduction to PHP
Feb 12th, 2008 | By admin | Category: PHPIt is now ending of our lunar New Year eve and I start to write some first lessons in the learning PHP series. The fact is that Wordpress blog is not very good in content management and so the PHP lessons will be mixed up with other articles. However, I will try to create a Table of content in the Learn series page so you can track the flow easy.
What does PHP do ?
PHP can be used in three primary ways:
Server-side scripting
PHP was originally designed to create dynamic web content, and it is still best suited for that task. To generate HTML, you need the PHP parser and a web server to send the documents. PHP has also become popular for generating XML documents, graphics, Flash animations, PDF files, and more.
Command-line scripting
PHP can run scripts from the command line, much like Perl, awk, or the Unix shell. You might use the command-line scripts for system administration tasks, such as backup and log parsing.
Client-side GUI applications
Using PHP-GTK (http://gtk.php.net), you can write full-blown, cross-platform GUI applications in PHP.
PHP runs on all major operating systems, from Unix variants including Linux, FreeBSD, and Solaris to Windows and Mac OS X. It can be used with all leading web servers, including Apache, Microsoft IIS, etc.
The language is very flexible. For example, you aren’t limited to outputting just HTML or other text filesany document format can be generated. PHP has built-in support for generating PDF files, GIF, JPG, and PNG images, and Flash movies.
One of PHP’s most significant features is its wide-ranging support for databases. PHP supports all major databases (including MySQL, PostgreSQL, Oracle, Sybase, and ODBC-compliant databases), and even many obscure ones. With PHP, creating web pages with dynamic content from a database is remarkably simple.
Finally, PHP provides a library of PHP code to perform common tasks, such as database abstraction, error handling, and so on, with the PHP Extension and Application Repository (PEAR). PEAR is a framework and distribution system for reusable PHP components.
Your first PHP examples
A Hello World example:
<pre><html> <head> <title>Look Out World</title> </head> <body> < ?php echo 'Hello, world!' ?> </body> </html></pre>
The PHP echo command produces output (the string “Hello, world!” in this case), which is inserted into the HTML file. In this example, the PHP code is placed between the tags.
Checking your configuration
The PHP function phpinfo( ) creates an HTML page full of information on how PHP was installed. You can use it to see whether you have particular extensions installed, or whether the php.ini file has been customized.
<pre>< ?php phpinfo( ); ?></pre>
Processing a form
<pre><html>
<head>
<title>Personalized Hello World</title>
</head>
<body>
< ?php if(!empty($_POST['name'])) {
echo "Greetings, {$_POST['name']}, and welcome.";
} ?>
<form action="<?php $PHP_SELF; ?>" method="post">
Enter your name: <input type="text" name="name" />
<input type="submit" />
</form>
</body>
</html></pre>
PHP programs access form values through the $_POST and $_GET array variables. In this example, what you input in the textbox is submit to the page in the $_POST array and the key of the item is ‘name’. If you change the method of the form from ‘post’ to ‘get’ then the if condition should be changed to $_GET['name'].
Create a dynamic image button
<pre>< ?php
if (isset($_GET['message'])) {
// load font and image, calculate width of text
$font = 'times';
$size = 12;
$im = ImageCreateFromPNG('button.png');
$tsize = imagettfbbox($size,0,$font,$_GET['message']);
// center
$dx = abs($tsize[2]-$tsize[0]);
$dy = abs($tsize[5]-$tsize[3]);
$x = ( imagesx($im) - $dx ) / 2;
$y = ( imagesy($im) - $dy ) / 2 + $dy;
// draw text
$black = ImageColorAllocate($im,0,0,0);
ImageTTFText($im, $size, 0, $x, $y, $black, $font, $_GET['message']);
// return image
header('Content-type: image/png');
ImagePNG($im);
exit;
}
?>
<html>
<head><title>Button Form</title></head>
<body>
<form action="<?= $PHP_SELF ?>" method="GET">
Enter message to appear on button:
<input type="text" name="message" /><br />
<input type="submit" value="Create Button" />
</form>
</body>
</html></pre>
GD library is one of many PHP extensions available to you. In this example, we use GD library to create an image and draw a text on that image background. If you are not very familiar with the header command, it is MIME type of the output telling the web browser how to treat the returned data.
Now that you have first look of PHP and how easy it is to learn. If you have some basic programming knowledge and HTML skill you will find that building a fully functional dynamic website with PHP and database enable is just a piece of cake. If you have no programming skill, you may want to check out some basic PHP language syntax before moving on.

