IntroductionThis tutorial was written to introduce you to PHP, it assumes you know nothing about programming at all, starts with the very basics and slowly guides you through some of the concepts of PHP programming. Hopefully this tutorial will clear up the basics and allow you upside-down people to follow, and I quote, some of my other “too technical” tutorials.
PHP?PHP is an ever-popular web scripting engine. It allows you to create dynamic web pages easily and quickly. It has a very rapid learning curve so once you get the basics down you’ll progress very quickly and start writing some nifty scripts. In newbie terms: it’s a thingy that runs in conjunction with a web server that easily allows you to create web pages that change—such as guestbooks, list users’ comments, discussion boards, top fifty lists, administration panels … and so forth.
Yeah ok, so how do I use it?Firstly you must ensure you have access to a web server that is compiled or set up to work in conjunction with PHP. Most good web hosts these days will offer PHP support, and if they don’t you can always bombard them with e-mails and whine until they do. PHP is getting very popular so the chances are you’ll have access to it. If you don’t you can always install it yourself on your own system, along with a web server. You can find instructions on how to do this, on a wide range of operating systems, in the PHP Manual.
So How Do I write It?PHP is very simple to use, the first thing to note is that PHP is embedded into HTML. You don’t have to have pure PHP code in a PHP document, you can switch between HTML and PHP, let’s take a look:
Simple, eh? Let’s have a closer look at it. The script starts off as any HTML document would, with a DOCTYPE definition followed by an open HTML tag, title, body tag, the only bit we’re interested in is the stuff between <?php and ?>. That is the PHP code, only when you open the PHP tag, <?php, can PHP code be used.
This is a great feature of PHP as you can just type out HTML if that’s all you need and jump into “PHP Mode” when and if you need it, remember: a PHP document doesn’t have to contain only PHP.
So What Happens?Remember how I said you need a web server that works in conjunction with PHP? You need this because the PHP code has to be executed, the PHP interpreter handles this. The web server is set up to recognize certain extensions as a PHP document, say .php. here’s what happens:-
That make sense? PHP code does not have to be compiled (converted to an executable program) before hand, when the web server passes PHP the document, it is then compiled on-the-fly (or interpreted), executed, checked for errors and then returned to the web server. So what is sent to the client (the person requesting the document) is not PHP code at all, it is the output of the PHP code. So … what would they receive from the above program?
All the PHP code did was to output
EchoThe only bit of the above script we’re interested in is this:-
We opened into PHP Mode, used the echo function (language construct, whatever) to output a sentence, or string. Echo is just for printing stuff out, for example:-
Would output the text “Hello World!”, just type <code>echo 'whatever'; </code>, the string is enclosed in single quotes and after the closing quote is a semi colon (;). We then closed PHP Mode and wrote out the end of the HTML document.
The PointWell in the above script there really is no point, many a time I have been asked something similar to:
“Why not just print out the
“Because this is an example showing you how you go into PHP Mode, how the web server handles the request and what goes on in the background, not a discussion forum, you silly person.”, I answer.
Points To Remember
Now that we have that cleared up and you (hopefully) know how PHP works, how to embed it into HTML, (hopefully) have access to a web host that uses it and didn’t fall asleep on your keyboard and are now ready to get coding some PHP. Click “Variables” and learn how to use variables in your PHP documents.
No Comments for this page.