A quick glimpse to PHP
Saturday, April 14, 2007 6:37:18 PM
Before you can start using PHP you have to do few steps:
- Download and install an HTTP server software (we are using Apache)
- Download and install PHP (I use PHP 5) or alternate XAMPP distribution for quick install and simpletons
- If you downloaded the normal PHP, spend some time to configure it (not easy for new people)
- Open httpd.conf file from the conf folder in the Apache installation directory.
- Locate and change DocumentRoot to a directory you want to use as the website root.
- In DirectoryIndex, add index.php.
- To include PHP you have to add next lines somewhere in the file and change them to match your directories:
LoadModule php5_module "C:/Program files/PHP/php5apache2.dll" AddType application/x-httpd-php .php PHPIniDir "C:/Program files/PHP"
<html> <head><title>Test</title></head> <body> <?php phpinfo(); ?> </body> </html>Then open your browser (God, I hope it's Opera) and open http://localhost/ to test whether the installation was successful. The page should show all info about your PHP installation and modules etc.
Some basic stuff about PHP:
PHP code is written in plain text and is always executed before the data is sent to the client. As you might noticed, the PHP code which you want to execute is always included in between special tags (<?php and ?>). PHP can also include external files. I do this for example to separate database functions, file parsing, texts and page drawing in different files, which are included only when needed.
PHP resembles some other programming languages, like C++ or Java. All variables are prefixed with dollar sign ($) like you'll see in the code below. You can use loops, functions and other familiar stuff. It helps quite a lot if you've done programming and/or web design earlier.
Example page:
Below you see three (3) different codes for php files. File index.php contains only two lines of code to include other files. Files head.php and body.php contain HTML and PHP mixed. Take some time to understand them because I couldn't be less arsed to explain in detail. Finally save all of the files in the same website directory.
index.php:
<?php include 'head.php'; include 'body.php'; ?>head.php:
<html> <head><title> <?php $title = "This is title text"; echo $title; ?> </title></head>body.php:
<body>
<h1>Header</h1>
<table>
<?php
// make ten table rows
for ($i = 1; $i <= 10; $i++)
{
echo "<tr><td>Row $i<td>Cell for row $i!";
}
?>
</table>
</body>
</html>
End words:
I hope you got some idea about PHP. I'm not going any deeper into the programming, but you can find a lot of tutorials in the net and perhaps read a book or two. Keep practicing and you'll gonna get friggin' good in this.
-J-




