Hello PHP!

A simple PHP tutorial for beginners.

 

The main difference between PHP and JScript is how it works. A PHP code is a "Server Side" Language. When a PHP file is requested thru the website, the Web Server process the instructions in the .php file and provides the result. The Jscript is not processed by the Web server, it is processed by the computer requesting the web page.

 

So, you can't run a PHP process at the user's computer. Even to get information from the user thru a website, you need to use a HTML form. If you have knowledge of any other language, PHP will be easy and practical to use.

 

Before starting to program in PHP, you need a Web Server with PHP parser and, if needed, a Database. You can obtain the software needed downloading the LAMP package that contains the typical configuration of Linux, Apache, MySQL and PHP. For windows users, there are the WAMP package.

 

One way to test PHP and determine if it is working properly, you can request the "php info" function:

 

phpinfo.php

<?php
phpinfo();
?>

 

First, the "phpinfo.php" file is the name for the file that contains the PHP code. The <?php line indicates to the parser at the Web Server to process the next information as PHP code. The line phpinfo(); is a function that displays the PHP information, like Status, variables, version, etc. The last line ?> indicates the PHP code is over.

 

The result will be something like this:

 

PHP info Function result

 

So, a PHP program looks like this:

 

<?php
echo ("Hello World, Hello PHP!");
?>

 

The result will be: Hello World, Hello PHP!

 

Now, we can write an small web page using some php on it:

 

hello.php

<html>
<head>
<title>Hello PHP!</title>
</head>
<body>
<?php
echo ("Hello PHP!<p>");
?>
</body>
</html>

 

When using PHP, the extension of the file must be .php and not .html. With the HTML extension, the web server is not going to process the PHP code.

 

Displaying messages.

 

The basic instruction to display a message (or variable) is echo.

 

There is no especific syntax for PHP on many instructions and functions. What I mean is, the basic PHP echo instruction can be used in many ways. Here is an example:

 

<?php
echo "Hello PHP! <br>";
echo 'Hello PHP! <br>';
echo ("Hello PHP! <br>");
echo ('Hello PHP! <br>');
?>

 

Result:

 

Hello PHP!
Hello PHP!
Hello PHP!
Hello PHP!

 

Comments and Remarks

 

Sometimes comments on the PHP code is needed to understand certain parts of the code. To add comments and remarks in the code, //, # and /*, */ are available.

 

Example:

 

<?php
// This is a single-line comment.
#  This is another single-line comment.
/* Here is a multiple-line comment for
   extensive explanations or some
   additional data */
?>
This code is not going to display anything or do anything as the PHP parser just ignores the content indicated by the comment's symbols.

 

The next articles will provide some useful PHP codes and example on this section. I hope you find it interesting.

 


 Homepage
php Index
Creating MySQL Users> 

 

If any information, data, picture or design infringes a copyrighted material, please send me an e-mail asking to remove it along with the supporting data.
Some features may not work with Google Chrome. © 2006-2010 Jose Pino - Powered by JPC Alpha