PHP: Back to basics - Part 1
I had a friend contact me on msn yesterday. She's a web designer, but does the front end side of things, HTML/Javascript/CSS, that sort of thing. She had been asked to put in a captcha on one of the forums to stop spam. Now this usually has to be done in a language like PHP or ASP, but she didn't know PHP or ASP. She had been trying to implement a capctcha that she had found on the web for a couple days and eventually contacted me for some help.
She was saying that she would actually like to learn how to code in PHP and that she'll take some courses at some stage. I said she should just go through the php.net site and go through the documentation, as there is some good stuff on there to start off from. I found myself explaining alot of the very basics of PHP though, how it is translated, why it needs to run on a web server, etc. I decided then to make a small series of blog posts covering the very basics of the PHP web language, which is where we are now... Part 1.
Now first off, I just want to say, that I'm generally just going to be saying things the way I see them, or understand them. I'll make some references to the php.net site as we go along too, but if I get anything wrong, please make a comment, so we can get things correct.
What is PHP?
PHP.net says the following: "PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.". Now if you actually know next to nothing about PHP, what does this mean to you? Not much really, except that you can embedd this into your HTML code. On the php.net documentation preface, we get a little more info, "The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP.". Okay, so now we have a little more info. A web developer, or programmer, can use PHP to build dynamic web pages, rather than static ones. Awesome, but how does it do this?
Well, before we jump into how the language does anything, lets explain a little something about web pages and how they work generally. HTML is a rather simple language, text is surrounded by tags and saved in a file. Web browser looks at these HTML files and according to what tags there are, decides what to do with the text inbetween them. You do not need any sort of Web server for this. You could write a small html file on your computer, save it, and then open it with the web browser by simply opening a file with File->Open.
PHP is different. PHP is used to generate HTML, that HTML is then translated by your web browser into a readable format, as we just said. PHP is dependent on a web server to compile it. For instance, if you write a small PHP file and save it to your computer and then try to open it with your browser, then it will show up as you had typed it into your browser.
For instance, if the following code, was saved to HelloWorld.php, it would show up in your browser as you see it here.
<?php echo "Hello World!"; ?>
If however, you uploaded that file, to a web server which was configured to understand and compile PHP files, and then accessed that file via a URL, then it would output the following:
Hello World!
"I don't see any HTML?", I hear you asking. Well, thats because PHP doesn't generate HTML, it generates what we tell it to generate, which is most of the time, HTML, but it could be used to generate CSS or Javascript.
"Okay, so how do we get it to generate HTML for us?". Thats a good question, so lets take a look. You could get PHP to generate ALL the html for us, so the following code
<?php echo "<html>\n"; echo "<body>\n"; echo "Hello World!\n"; echo "</body>\n"; echo "<html>\n"; ?>
would generate the following html code
<html> <body> Hello World! </body> </html>
which would output the following in a web browser
Hello World!
"That looks exactly the same as static HTML, but with more effort". Again, you would be correct. You would usually never use PHP in such a way. So how do we go about using PHP to build these dynamic we pages? Well, for starters, PHP can be embedded into HTML, so we don't need to use to generate the <html> and <body> tags, but we can embed the code in the HTML. Then we can use built in functions to generate some content for us.
Lets create a small page that displays the date and time. Since the date and time is always changing, we be very busy if we kept updating the web page with static content, so instead we use PHP to do this for us.
Here is the following combination of HTML and PHP code to generate a dynamic web page that displays the date and time:
<html> <body> Date: <?php echo date("j f Y H:i:s"); ?> </body> </html>
That code will display the following in your web browser (not exactly the same, will be the actual date and time)
22 October 2009 20:42:11
If you refresh the page that you've just created and accessed, the time should change. Since the web page content is changing all the time, the page is now considered to be a dynamic web page.
This image gives your visual idea of how the translation flow goes, if you click on the image, you'll be taken to the page that gives a small description to go with the image.

"Okay, I've written some PHP inside my HTML file, but its not generating what I want, in fact the code is just showing up in browser". There are two likely suspects here, first off, your web server isn't configured to handle PHP code, so it just thinks it is normal text and hence, so does the browser. The other thing is, if you've saved PHP code, into a file with a .html extension, it's more than likely that the server won't try convert the PHP code. By default, you want to save your PHP pages into a file with a .php extension. Plain html can be saved in the .php file too. Web servers usually decide what to do with a page depending on it's extension, so unless you name your file with a .php extension, it's not going to know that there is php code in the file that it needs to translate.
PHP is a powerful language to create web pages with. Its not used to create fancy front end graphics and animations. PHP leaves that languages like Javascript and Flash. PHP is used to get data, manipulate that data into a form that you would like to present it and then do so, so that the user gets a nice web page. You would usualy do this by getting your data from a database, or a XML feed. You could provide a tool for news authors to type in content, which is then saved into a database, only to be displayed on a web page at a later stage. The php.net - What can PHP do page has a more detailed description.
If you would like to play around with PHP, but don't have access to a web server. If you're on windows, you can download a suite of programs called WAMP. WAMP is an acronym for Windows, Apache, mySQL and PHP contains a web server, database and PHP, so you can build web apps that run on your desktop or laptop.
I'm going to end off there, but I hope that gives you a slightly better understanding of how PHP is used to create web pages and the workings that go on behind it. In the next session we'll start going through some of the very basics of PHP code its self, a bit of a crash course in general programming. After which we'll see how PHP can be used to make HTML forms work as intended.
A quick side note, if you're looking to get into PHP and web development in general, check out the W3Schools website, I use this site alot when I'm looking for some small examples of code, or just to find out how to do some CSS or Javascript, but it has a section on PHP too.
Until then, keep learning.
Synack

Recent comments
42 weeks 3 days ago
42 weeks 3 days ago
42 weeks 3 days ago
42 weeks 3 days ago
42 weeks 3 days ago
42 weeks 3 days ago