.
Tutorial
Avatar

PHP basic lesson 1

By Remco on 2008-11-16
Views: 1006

Introduction

Thanks for reading this tutorial, we will explain the basics of PHP to you.
What is PHP? PHP means Hypertext Preprocessor and is mainly used by small companies and home users.
PHP is a very powerful language, you can do many things with it like creating a simple database table to an entire CMS ( content management system ).

In this lesson you will learn how PHP works and how to create a PHP file.

Step 1 - Information

Before we start you will have to know a few things about how PHP works.
PHP can not be opened from just any location.
PHP will need a webserver that runs PHP, you can get webservers like this by creating one yourself, buying one or using packages like WAMP ( for Windows ) and LAMP ( for Linux ).

PHP will only work when you run them from this server, you can run the script by browsing to it, a so called server sided scripting language.

Example: http://localhost/index.php

If your webserver does not run PHP correctly, it will just display your code.

Information

Most webservers you can get will run Apache, Mysql and PHP, this is also what the AMP in LAMP and WAMP stands for.

A tip for beginners: Try and use packages such as WAMP or LAMP before you purchase a webserver, it's always good to have practiced just incase you'd much something up.



Step 2 - Creating a PHP file

To create a PHP file you will need a good text editor, we recommend Notepad++.
It's a free editor and Personaly I find it amazingly usefull.

Once you've downloaded this program you create a new file and you should save this file as a php file, to make things easy call it index.php.

Note

It is very important that you save the file in the root of your webserver, often called your www folder or htdocs,if you don't your file will not be accessable through your browser.



Step 3 - What does a PHP file look like ?

We are now going to edit the PHP file you have just created.
A PHP file always starts and ends with a PHP tag.

The open tag: <?php
The closing tag: ?>

Everything in between these two tags will be your PHP script.
These two tags will make sure that the browser runs the PHP script.

Step 4 - Display text using PHP

As you understand you are able to run scripts with PHP, we will now explain to you how you can display text in your browser by using PHP.

To diplay text we will need the echo command.

Example:

Code:


<?php

echo "Hello World!";

?>



As you can see the text you want to display has been put between quotes " ",
and the command finishes with a ; .
It's a common rule in PHP to end all functions and commands with a ; , you do this so PHP knows that a specific function or command has finished.

If you forget the ; or the " " you will get an error.

Now save the file into your webserver and browse to your file.
Example: http://localhost/index.php

If everything works fine your browser should output: Hello world!




This was lesson 1, you now know more about the basics of what php does and how it works. You also have learned that you can display text using the echo command.
Next »




Comments

Flamer - 2008-11-17
Zomfg this really looks awesome!

Flamer - 2008-11-17
BTW! You are missing a code box, what is being displayed now is:


Code:

<?php

echo "Hello world";

?>



Remco - 2008-11-17
thnx, i know Hehe .. i still need to fix that Annoyed.
I've got a bug in my function which i need to sort out lol i've mailed you about it Hehe

Rabana - 2010-04-29
You are doing a great job. I used form builder http://phpforms.net/tutorial/html-basics/form-builder.html to create forms on my web site. Now with your cources I can do any customizations myself. Thanks

You have to be logged in to write a comment.

.