PHP Syntax
What I like about PHP is that it is very easy to learn. You will find that out for yourself after this simple lesson on the basic syntax of PHP. At the end of this tutorial, you should be able to:
- Identify the PHP Tag.
- Write a simple script and save it to the server.
- Run a simple PHP Script from the browser.
- Learn about PHP Statements.
Just like most modern Web Scripting Languages, PHP is also a server-side, HTML-embeded script; that is, you can mix PHP code into your HTML tags. To demonstrate what this means in actuality, consider the program snippets shown. Listing 1: sample.php
-
<?php
-
?>
This is perhaps, the simplest PHP code around -- you don't even need to have a programming background to guess what it does. When you run the script, it only displays the message 'A simple display' on the browser.
Simple as it may be, please follow closely if you are a complete beginner. What we will do next it to actually save it on the server and run it on the browser. Please follow these steps:
- Open a Plain Text Editor like Notepad then copy-and-paste the code shown in Listing 1.
- Create a directory from the htdocs directory and name it phpsyntax. If you have installed XAMPP on Drive D, your directory structure should look something like this:d:\apachefriends\xampp\htdocs\phpsyntax\
- Save the file as sample.php
Now, it's time to run the script. With your XAMPP installed, make sure that Apache is active. Follow these steps when testing the script you have just created.
- Open your favorite Web Browser (e.g. Internet Explorer).
- On the Address Bar, type this: http://localhost/phpsyntax/
- Press Return
- You should be able to see the display message we have described earlier.
Syntax Explanation:
Now that you have successfully run the code, it's time to learn the syntax proper. A PHP Script is identified by the PHP Tag (starts with < ? php and ends with ?> ) in your code . You can have several PHP Tags in one file. In other words, PHP and HMTL Tags can be mixed together. Of course proper pairing and matching should be observed when mixing tags, otherwise a syntax error will be generated.
The next thing you must remember is that all PHP Statements must be terminated with semicolon. This is similar to Java or C. In fact the syntax is borrowed from C so that C programmers can easily adapt to the language. Indeed, if you have a background in any of the curly braces languages, PHP can be learned in no time.
So far, we have only discussed the basic syntax of writting a PHP Code. This is only the beginning. The topic on PHP Syntax is wide. There is a syntax for writing a function, a syntax for creating loops, and syntax for just about everything in PHP.
Syntax simply means the set of grammatical rules of a given language. The English Language says that a declarative statement must be concluded with a period. That is a syntax for writing declarative statements. In PHP, or in any programming language, same thing must be observed: one must follow a set of grammatical rules.
As you go along with the rest of the lessons, you will discover syntactical rules that apply to a specific topic. On the topic about PHP Functions, for example, you will learn how to construct a user-defined function, the different kinds of parameters and the ways of passing them. So, prepare yourself to discover some new things as you learn PHP from Carlos On Web.
This series of articles is intended as a
Self-Paced Introductory tutorial on PHP.
Assigment:
1. There are four different kind of tags supported by PHP. What we have shown is the most common and the one that should be practiced whenever possible. Visit the PHPBuilder website and learn other ways of writing PHP Code Blocks.
2. We mentioned that PHP can be embedded in HTML. Give an example showing PHP and HTML Tags written together in one file.
3. Try to change the filename of sample.php to sample.html and run the new file from the browser. Did you get the same display output? Why?