php for beginners
skill level: easy
Welcome to my php tutorial for beginners. In this tutorial I will show you how to make a php enabled page. PHP is a Hypertext Preprocessor which is easily integrated into HTML. HTML knowledge is not required, but highly recommended. PHP is mainly used to create database driven pages, likely to ASP from Microsoft.
In this article I'm NOT going into databases, because it's a more advanced part of PHP and we are only going to see the basics.
To create your own pages you have to use a webserver which can parse PHP. Apache is the most well known webserver, but others will do fine. The installation of an Apache server together with PHP and SQL is up to you, check the internet for resources. At the end of my article are a few links listed if you want to start coding yourself, or if you need any help installing Apache, PHP or SQL.
:: Firsly I cover some basic HTML.
In the HTML (HyperText Markup Language) you can show text just to type it in as you read it. An example of a simple HTML page:
<title> HTML page </title>
This is the first text, shown on my page.
For the people who don't know HTML, I'll cover the used tags (words between '<' and '>').
|<html> and </html>||the beginning and ending of a html document, it's used to define that it is a html document|
|<head> and </head>||beginning and ending of a header, the place where you store information such as the creator, search words, title, etc.|
|<title> and </title>||guess... displays the title of the document.|
|<body> and </body>||between these tags you can display text, images, hyperlinks.|
Copy the above code and paste it into a .html or .htm file. Just create one in Notepad or so and run it in your browser and see the result. Try to change the words between the body tags.
:: PHP Coding
Shortly said anything that is PHP stands between the <? and ?> tags. <? is the opening tag and ?> is the closing tag, <? equals <[html command]> and ?> equals </[html command]>. Also <?php and ?> can be used, for writing PHP code. PHP has in some way the same syntax as Java or C. So If you're used to one of these languages in combination with HTML, PHP will be of almost "no difficulty" to you.
:: Displaying text
Of course, you want to display some text on the screen. Displaying text is done differently of the HTML way, where you just type some text between the "body" tags.
With the echo command, you can display text on the screen.
syntax: echo [String];
example: echo "Hello World!";
This can also be done with the print command:
In PHP variables are automatically assigned to the correct type, just as in Java. Variables are started with a $ (dollar) sign and a name. Followed by an equal (=) sign and the value. Closed by a semi-colon.
$name = "cenix"; // string
$nr = 8; // integer
$nr2 = 8.5; // float (real)
Variables can be used thoughout the PHP. Variables are either Global or Local. A Local Variable is only used into that function. A Global Variable is used all over the code.
PHP offers you some internal variables.
$GLOBALS: An array of global variables. You can access it anywhere without declaring it as a global variable first.
$PHP_SELF: This is the current script, your document name. Simply said: It links to itself.
I think you're are ready to begin coding PHP. If you created the html example I made above, load it into your favourite editor. Now we are going to rewrite the example into PHP. In this example the PHP is integrated into HTML.
<title> <? echo "PHP Page" ?> </title>
$text = "This is the first text";
echo "$text, shown on my page";
If you run this page, the screen will display something like: "This is the first text, shown on my page".
As you see, thus far PHP is not difficult at all. This is all quite nice, but not very useful.
<title> <? echo "A more useful PHP page??" ?> </title>
echo "Hello $name, How are you doing today?";
If you've created the document, save it under the name hello.php and run it with this command: hello.php?name=Bart where the name of your document is hello.php and your name is Bart. You can run it by starting your browser and type the location of the file with variables into the address-bar.
And with this I conclude my tutorial. Try to develop your own styled page, for example with tables and so, a little example is included with this article. If you want to know how to automate your site (connecting to a SQL database), read Sweeper's article.
And one last tip: download the PHP manual from www.php.net. It contains valuable information about PHP coding.
A few links for setting up your own Server (which can parse PHP pages), links for beginning with PHP coding, and more.
And check search engines such as Altavista, Yahoo, Webcrawler.
For questions about (beginning) PHP coding, unclear issues, and more: