Learn PHP Basics

When you decide to learn PHP, depending on how much of a beginner you are, it is important to learn the basics. I will tell you right away what this post is and what this is not. There will be no PHP history lesson. This is not a slight towards Rasmus Lerdorf, the creator of PHP. This, however, is the essentials you need to know to start writing PHP code correctly. Hopefully you have an editor and a local development environment. If you do not have an editor you can use notepad or something similar. I would like to encourage you to use an IDE such as NetBeans. You can Download NetBeans at the NetBean Website. Now we have all that out of the way let’s get to work.

PHP is an acronym for “PHP: Hypertext Preprocessor”. It is a server side scripting language that is evaluated on the server before its output is sent to the client’s browser unlike client side scripting such as JavaScript which is code that is sent to the client’s browser and executed there.

Here is an example of PHP code stored in a file called index.php on the web server:

This is embedding PHP into HTML.

The process of requesting a page is demonstrated below:

The process of how the web server handles php files.
The process of how the web server handles php files.

Since it has the .php extension it will get passed to the PHP interpreter. The interpreter skips over the HTML on the page until it reaches the <?php opening tag. It then evaluates the PHP code up to the closing tag – ?>. This particular code simply sends the results to the requesting browser. In this case we are simply writing “Welcome to My Index Page” to the page. If you look at the source code you will only see:

<h1>Welcome to My Index Page</h1>

To learn more about echo and other methods to output text to the screen, take a look at “Printing to the Screen with PHP.”

 

It is important to save your files that have PHP code with a .php extension. If you include PHP in a page that has an .html extension it will be ignored by the interpreter and pass all of your PHP code to the browser in plain text. You can set up PHP to evaluate .html files but that uses more resources unnecessarily and is beyond the scope of the post.

PHP Tags

As you see from the example above you can embed PHP into HTML. To do this you will need opening and closing PHP tags. There are a few different tags that I will go over with you. Some are deprecated in newer version of PHP and some are just a bad idea to use. However, you may need to read someone else’s code so it is important you know what they are.

Default Delimiter Syntax

The first one is the recommended tags. These are the tags that were used in the example above:

These tags are always available no matter what so these are the safest to use.

Short Tags

The sort tags are exactly what it sounds like and they look like this:

These tags are discouraged for a number of reasons.
1.    These tags must be enabled in the php.ini file using the short_open_tag configuration directive.  You will learn more about the php.ini later
2.    Short tags can clash with xml

Another example of short tags is:

To make your code as portable as possible (Can be used in almost any environment) you are strongly discouraged from using these!

ASP Tags

Another set of tags is the ASP tags. The ASP tags look like this:

These tags must also be enabled in the php.ini file. It is rarely used and is probably good that it is.

Script Tags

The script tag has been around a long time. It looks like this:

Again, your best bet is to use the <php ?> syntax. It is accepted no matter what.

It is important to note that the asp tags, short tags, and the script tags are all scheduled to be removed from PHP on version 7.

Commenting

When you write code you may need to go back and review what you have written some time ago. It is at that time when you will truly realize how important it is to comment your code. Or worse yet, you may need to back and look at someone else’s code. The importants of commenting cannot be over stated. Trust me when I say “Comment your code!”

Single Line Commenting

There are two types of commenting syntax, the single line and the multiple line comments.
The single line comments have two variants, the C++ style and the shell syntax style. If you do not know what C++ or Shell is, it is okay. Knowledge of these is not necessary. The C++ single line start with a double slash and looks like this.

The single line comment cannot continue to the next line. The next line will not be commented.

The shell syntax begins with the “#” and looks like this

Multiple Line Commenting

When more detailed commenting is necessary a single line might not be adequate. This is where the multiple line syntax comes in handy. The comment is opened and closed much in the same way PHP has open and closing tags. Multiple line commenting looks like this:

The “/*” tags tells PHP that everything after is to be ignored until it runs into “*/”.

These are the basics. Although they are not exciting, they are critical to starting PHP development on the right foot. The next post will be much more fun. I promise. Next week we will be learning how output information to the browser.

Related Posts