Introduction to PHP Learning Guide

From Wikiversity
Jump to: navigation, search

PHP is a significant language, with millions of practical applications used for a variety of commercial and non-commercial purposes. It is installed on thousands of web servers worldwide, and web hosts consider using PHP on their servers for other applications e.g. hosting control panels. Due to the greatly increased availability over other web languages (Perl for instance), PHP has grown rapidly and knowledge of it can even lead to a high paying job. However, today we're going to start out small and teach you the basics, step by step, of the Wikiversity Introduction to PHP.

Introduction[edit]

As the course reference begins, PHP is a server side scripting language. This means that the scripts you write are handled by the server (generally the web host) rather than the client (the end user's browser), unlike other scripting languages like JavaScript. Its use requires a certain software known as a web server (not the hardware - any PC will do, in theory) which handles incoming connections from other computers and sends back web pages as requested.

For PHP to run, this web server has to be configured so that when certain requests come - requests for pages with the .php extension, for example - PHP is called. The php.exe file then jumps in, looks at the code, interprets it and sends the output back to the web server within a fraction of a second for the web server to send back to the client. This means that if your PHP code was the following:

<?php
echo "ABC";
?>

All that would be sent to the user's web browser is this:

ABC

This means that significant web applications can be developed by software firms without having to worry about competitors stealing their code - after all, you wouldn't want to spend millions only for your arch rivals to get it all for free by taking it from your servers.

Prerequisites[edit]

Just to clarify, some of the prerequisites listed at Introduction to PHP don't even exist yet (as of 6/10/06), so in lieu of them we suggest you run through the basics of w3schools.com's HTML tutorial and the first few lessons of its JavaScript tutorial.

Basic syntax[edit]

As this is your first PHP lesson, we'll cover this section in detail. How web servers handle programming languages is quite simple - they look for certain file extensions (the .whatever in the file name) and run specific commands depending on the file type. In this instance, web servers look for .php in the file name, and instead of simply sending the file across, they call a program called php.exe and pass the file to it. php.exe then parses (interprets) it before sending the output back to the web server.

However, there is a very useful feature of PHP that requires some further insight here. This feature is PHP's ability to effectively jump in and out of 'PHP mode', so that, at times, normal HTML can be used in scripts. This is a basic example:

<html>
<body>
<?php

?>
</body>
</html>

Notice that normal html tags are wrapped around the php. If this were a .html file, what would be sent to your web browser is exactly the above.