Categorized | PHP Tutorials

PHP Tutorial for Beginners Part 1

This  is the first part of  PHP Tutorial for beginners  series . In this article we take a look at a detailed introduction to PHP, which of course is the most if not the biggest programming language on the internet right now. Many types of sites utilize PHP in one way or another, for uses such as shopping carts, login forms and much more…

So what is PHP?

As a beginner to PHP you may like to know that this 3 letter word stands for Hypertext Preprocessor and that it is a scripting language that utilizes Apache servers to operate. PHP is of course an open source language and not only is it powerful, but the good news for PHP beginners is..that its pretty easy to learn. Now when i say “pretty” it all depends how far and deep you go depending on the tasks you wish to accomplish. However the usual elements of a web site that operates with PHP is a very straight forwarded learning process for PHP beginners.

Noticing PHP in action

When visiting a website you may notice at the address bar within your browser that you will see the URL (Uniform Resource Locator) ends with the extension “.php” this obviously means that the site is of course utilizing PHP. Other extensions such as “.php3″  or “.phtml” are also quite common. The PHP code within the page such as ‘<?php’ and ‘?>’ is parsed by a PHP engine on the Web server, which then dynamically generates the HTML that is sent to the user’s Web browser; commonly used in conjunction with a MYSQL database.

Using your first piece of PHP code

Let us begin with the first piece of PHP code that you will write to a web page, “Hello World”. Below is the code you will need to copy and paste into a text file, you can use notepad to make a new text file, insert the code below and save it as “hello.php”

<html>
<body>
<?php
echo 'Hello World';
?>
</body>
</html>

Next, upload your hello.php file to a server, and make sure this server supports PHP. Open your browser and load the page you have just uploaded, you should then see the text “Hello World”. If you don’t, then the server doesn’t support PHP (check with the host). If it has worked Congratulations you have now learned how to use the PHP scripting engine to write out  text within a simple HTML page.

Introduction to variables in PHP

Next up are variables in PHP, and these are very straight forward, anything that starts with a “$” symbol is a variable, when naming variables, it is a good idea to name them something you can refer to. For example a variable containing a Surname may be called $Surname. Now we will begin using a PHP variable within our Hello World code we have just created:

<html>
<body>
<?php $HelloWorld = 'Hello World, Again';
echo $HelloWorld;
?>
</body>
</html>

Notice that when you echo a variable you do not put it within quotation marks, otherwise it would display “$Hello World” as text. The echo command simply displays the result of the variable, in this case it will display the value the variable “$Hello World” as text on the web page.

Replace your current hello.php with the new code and you should see ” Hello World, Again” on the web page.

Now you have managed to complete two basic scripts, we are going to step up to something a little more challenging. We are going to create a variable and assign it with a value of the current hour of the day, and then display some text on the web page depending on the actual clock time.

Create a new text file and insert the code below, name you file time.php (or any name you want) and upload it to the server


<html>
<body>
<?php
$HourOfDay = date("G");
if ($HourOfDay < 12) {
echo 'Good Morning World!<br><br>The hour of the day is ' . $HourOfDay;
} else {
echo 'Good Afternoon World!<br><br>The hour of the day is ' . $HourOfDay;
}
?>
</body>
</html>

First we need to get the actual time to feed to the variable and that is done by using the “date()” function. The argument, “G” (an argument is a value you pass a function) tells the date function to give us the hour of the day.  $HourOfDay will now contain a number representing the hour of the current day. Next we need to check the actual value in order for the script to determine which piece of text to display. If the current value is lower than 12, then the script will activate the morning greeting message. If not the  (else)  command comes into play, and the script will place the afternoon greeting on the web page.

Once you have uploaded the script to the server you should then see one of the two greetings depending on the time, now the time is read from the server, so it may be different to your local time, depending on the location of the web server.

5 Responses to “PHP Tutorial for Beginners Part 1”

  1. Awesome tutorial! I am going to give this a shot for myself – probably I’m going to start working on it in a few days.

  2. Christine says:

    Just a short note to say thanks.You saved me a lot of time! Excellent tutorial.

  3. leons0133 says:

    Your php tutorial is really useful and excellent. Its useful for php beginners.

  4. g.t.alhadad says:

    this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a
    copy of this lesson on my site here /www.doannews.com

Trackbacks/Pingbacks

  1. [...] PHP Tutorial for Beginners Part 1 | PHP IDE Share and [...]