Search:
All Any

Part of mariaus.info network Subscribe via RSS
Follow on twitter Request a tutorial
Quick tip: Inset shapes, lines and text
Wallpaper: Above the clouds
Wallpaper: wall of pictures
Non destructive deletion with layer masks
Business card # 1
Photography and art inspiration # 2
Photography and art inspiration # 3
Photography and art inspiration # 4
Photography and art inspiration # 1
Go to first pageAdd to favorites (this)Contact page ownerRequest a tutorialSubscribe to the Tip Kit news
Request a tutorial
Introduction to PHP, Part 1 - Understanding, installing and using

A first part in a series of learning material about PHP for the ones who are just getting started.
If you're planning on learning PHP and don't know where to start, well ... you can buy a book or follow some online free learning material like the one you're currently reading. This is the first part of introductional tutorial series to PHP. We will be covering not only the basics of PHP, but also the basics of programming itself. This way even if you're a total beginner in programming and trying to start with PHP, you won't get lost in a bunch of unknown words and phrases. Although i'll try to teach you as much as possible, PHP is very closely related to HTML: it practically cannot live without HTML, it's PHP's purpose to serve HTML. And i'm not going to be covering HTML so if you don't know what that is - find yourself a tutorial and learn it first. Anyway i'll try to use as little HTML as possible and will work with simple text. This part of the tutorial series will be a short and easy one, but if you're a beginner - read it nevertheless.

So what is PHP and what is it used for? PHP is a server side programming language that helps create dynamic webpages. In old days if you wanted to view a website, you typed in a URL and the corresponding HTML document was downloaded from the server and viewed by your browser. That means if you wanted to change something in your website, you had to edit some HTML files and upload them to the server via FTP or something like that. But websites nowadays do not even require any programmer's interferance to perform complex tasks. For example you load a site and you see a banner, then you refresh the site and the banner changes. Do you think some programmer is sitting by his computer and changing the banners manually every minute or so ?:) Not really. That's where server side programming languages like PHP come in. When you request a document from the server, that document is at first run through a server side script interpreter (like PHP) and the result is then sent to the user. So the document on the web server is now not only a HTML file, but also contains instructions to the script engine. For example consider the following HTML file:

Hello world!


A very difficult one, isn't it ?:) I told you i'll try to avoid HTML as much as possible so that you could get the PHP things more easily. So the above HTML document, when viewed in the browser, shows the text Hello world!. If you don't believe me, create a file index.html on your computer and write that text into it. Then save the document and double-click it. But now change the contents of index.php to these:

Hello world! <?php echo 'And again!'; ?>


What do you see when you double-click the file? You should see it exactly as you see it in the box above. But this file has actually a PHP instruction defined. And it should execute that instruction. So why doesn't it? It's because when your browser reads this document, it sees everything as it is. The browser doesn't execute PHP or any other server side language. So you can't just double-click a PHP script and expect it to do something. But if you uploaded this file to a server with PHP support, here's what would happen:

* your browser would request a file index.html
* the server would call a PHP parser and pass index.html to it
* PHP parser would see and execute its instructions (the ones between <?php and ?>)
* PHP would output the results to your browser

Here's how PHP would read this file:

* first it would see the Hello World! part and since it's not a PHP instruction, the parser would simply send it to your browser as it is. So your browser now has received text Hello world!.
* next, PHP would find an instruction between <?php and ?>. It knows that echo instruction is used to print something to your browser. In this case it prints the text And Again!. So PHP sends this text to your browser and the final result your browser sees is Hello world! And again!.

As you can see, your browser does not receive anything that's between <?php and ?>, because PHP keeps that to itself. So you could write your sensitive passwords or something like that between <?php and ?> tags and no user would be able to see them, unless you use echo istruction deliberately to print them to the browser. You can use php scripts to create complex website designs dynamically and they would be generated every time a user requests the page. For example see the script below:

<b>I can count from 1 to 3:</b><br /><br />

<?php
    for ($i = 1; $i <= 3; $i++)
        echo '<u>Number ' . $i . '</u><br />';
?>


If you requested the following script from the server, here's what you would see with your browser:

I can count from 1 to 3:
Number 1
Number 2
Number 3


As you can see, PHP has written 3 lines of text itself. You could write hundreds or thousands of lines like that and you'd only need to write the two lines of script (a little tweaked) between <?php and ?> tags, as seen in the example above.

Now that you know what PHP is for, i'd recommend to get started by installing yourself a local webserver with PHP support. What that does is it launches a webserver on your computer and you can create websites with PHP without even having a hosting account somewhere. Unfortunately, you will need the hosting account eventually, because your computer is not a server and real websites shouldn't run on it. But it's good for testing and developing purposes to have a local webserver on your computer. If you do, you're assigned a special website address (http://localhost/) that you can type into your browser's address bar and a website from your computer will open. Every local webserver has a special directory where you can put your scripts in. So the same index.php file that didn't work by double-clicking it would now work by putting it in this special directory and launching http://localhost/ in your browser. A popular local web server is called WAMP (Windows Apache MySQL and PHP). You can download it from this site. But your hosting account will most likely run on Linux operating system and these two systems have minor differences that effect programming in PHP (for example file paths). So if you want create conditions as close to a real hosting account, install a LAMP server (Linux Apache MySQL and PHP). And yes, you need to install Linux for that. Fortunately there are things like virtual computers that enable you to run both systems at the same time. If you're a complete beginner, it will be challenging enough for you to understand and use the WAMP server so don't get concerned with Linux for now. But after you learn something, you can have a look at my tutorial on how to install a linux virtual computer with a PHP enabled server on your Windows machine.

So i'll leave you to digest this new information and we'll meet again when a second part of the tutorial series is released. In this part, we'll start the actual programming.

Article written by: Marius S.
This article is an intellectual property of its respective author. All images, used here are property of tip-kit.com if not stated otherwise.
Share this article
Digg del.icio.us Facebook Furl Google Reddit Slashdot StumbleUpon Technorati
How easy was it to understand? Was it useful?










Leave a reply
Your name:

Message:

Confirmation code
Please enter the above code:
There are no comments yet. You can use the above form to leave a reply.
Copyright © 2009 Tip-Kit.Com | Valid XHTML 1.0 | Powered By Isis | RSS