PHP Tutorial – Scripting Language Syntax

As we explained in the PHP introduction tutorial the PHP code is executed on the server-side. The result of a PHP program is send in plain HTML to the browser.

The PHP Basic Syntax

The PHP statements or scripting blocks always start with <?php and end with ?>. You can start a PHP block anywhere in a .php file.

Note: on servers that have shorthand support enabled you can start with <? instead of <?php. But in our opinion you should always use <?php, because this works in both cases. This way you stay compatible and in a program with hundreds lines of code the gains of not typing those three letters are negligible. It’s up to you!

A PHP file doesn’t have to include HTML tags, but in most cases it does. Of course you need a starting point so in most cases you’ll have a normal html file and from this file you’ll call some PHP statement or include another PHP file.

Let’s start with a simple example (copy the following code, open your favorite text-editor (we like to use Notepad++), paste the code and save it as index.php):


<html>
<head>
</head>
<body>
     <?php echo “Hello World, this is my first PHP program!” ?>
</body>
</html>

The index.php file that you just have created should be placed in the document root of your web-server (the directory that is called WWW.) After you’ve placed the file in the document root you can start a browser and type http://localhost (or whatever domain you use.) The text ‘Hello World, this is my first PHP program!’ should appear (without the ‘ ’ of course.) You have just created your first PHP program.

If you look at the code than you’ll see that you have the normal layout of an HTML file. Inside the body the PHP scripting block is placed beginning with <?php and ending with ?>. The only new thing is the ‘echo ” “‘ statement. The echo function will print everything on the screen that is placed between “”. In this case our “hello world” sentence.

Using Comments in PHP

Before we end this PHP scripting language syntax tutorial we want to explain comments in PHP, because you should always use comments in your programs. Even if you don’t give the programs you create to someone else, making use of comments is very important!

Why? The following scenario will explain it: you have worked on a PHP program for many weeks and you have used a lot of difficult code in your program without placing any comment. Your program seems to work perfectly, so you start using it on your web-site. After six months a web-site user sends you an e-mail, which tells you that in some scenarios the program doesn’t work and if you can fix it. And now the problem: no one can remember complex structures of code after six months. Because you didn’t use any comment lines, so you have to go through each line and again figure out what the program is doing at that point. If you had placed a comment field above a complex block of code (explaining in words what you are doing in that piece of code) you probably debug the program much more quickly.

OK, that’s enough on the importance of comment-lines, let’s see with an example how to use comment-line in PHP:


<html>
<head>
</head>
<body>

<?php 
     /* This is a
     comment block over
     several lines */

    //This is a one-line comment

    //Print a sentence onto the screen
    echo “Hello World, this is my first PHP program!” 
?>

</body>
</html>

As you can see in PHP you can use /* and */ if you want to make a large comment block. Or you can use // if you want a one-line sentence. Again, it’s up to you!

Note: choose one comment style in all your PHP files. This will make it much easier to read.

That’s all for this PHP language tutorial.

This entry was posted in PHP Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There is currently one response to “PHP Tutorial – Scripting Language Syntax”

Why not let us know what you think by adding your own comment!

  1. PHP is the best language for web programming? | Web Resource and Educations on January 10th, 2010:

    […] PHP Tutorial – Scripting Language Syntax | NextDawn Programming … […]