Basic PHP Syntax

Published on April 6th, 2013 by | Category: Programming

I hope you had the chance to read the basic HTML syntax article carefully, and so, now you are familiar with HTML, XHTML and CSS basics.

Also, if you are serious to practice and learn PHP, you have to install the XAMPP software on your computer, to make your computer PHP and MySQL enabled. This helps you to test your PHP scripts on your own computer very easily.

We already created our first PHP script here, when we were talking about the requirements that have to be met when you want to start practising and learning PHP. It was a very simple script that displayed this sentence on your web browser: Hello World!

In this article, we are talking about the basic PHP syntax from the scratch.

You start writing a piece of PHP code (which is known as PHP script) exactly like starting to type an article. You can simply open your Notepad which is the simplest text editor everybody has on his computer, and start typing. You can type an article, some PHP codes, or some HTML codes. It means, start writing a PHP script is not different than writing an article or HTML code, and can be done with a very simple text editor. I recommend you to install the Notepad++ on your computer which has some very helpful features to make your life easier as a programmer.

The Differences of HTML and PHP Documents:

1. As it is explained here, when you use PHP code in a document, you have to save it with .php extension, whereas standard HTML documents have to have .html or .htm extension.

2. The second difference is that PHP codes have to be placed between the <?php and ?> tags. While writing HTML codes, if you need to insert some PHP codes, you have to open the <?php tag. For example:



Home Page




When there is only one line of PHP code, the <html> tags and also the PHP code can be in one single line. Therefore, the above script and the below one are the same and will return the same result:



Home Page




Why do PHP codes have to be placed between the <?php ?> tags?

The <?php and ?> tags indicate that the codes that are placed in between, have to be processed by a PHP-enabled server. All the other codes out of these two tags can be immediately sent to web browser because they are HTML and don’t have to be processed like the PHP codes. This is the third difference between PHP and HTML (below).

Also, please note that instead of <?php you can simply use <?. It will not make any problem, but you’d better to stick to the standard and formal tags.

3. PHP scripts have to be processed and launched by a PHP-enabled server, whereas HTML codes can be processed by any computer. PHP scripts work only through a URL (e.g., http://www.vahidchaychi.com/contact/index.php).

PHP Info Function

Before we talk more about the basic PHP syntax, lets create a small and simple PHP script that will help us a lot in future and we will have to refer to it frequently. Just copy and paste the below code to a text file and then save the file with a .php extension. You can give any name to the file, however, this script is usually named phpinfo.php

As you can see in the above script I am not using and HTML code and I merely showed the PHP code. However, if you like to have a standard and formal XHTML and PHP syntax, you have to have the script as below:





PHP Info




If you have saved the above code in a phpinfo.php file, then upload it to your website server. If you are using the XAMPP software, then save the phpinfo.php in the htdocs folder. Now, you can enter this URL to your browser address bar: http://localhost/phpinfo.php

I used the XAMPP software. This is what I got:

phpinfo

It is a long page that reports all the settings that our server has. Phpinfo() syntax calls the phpinfo function. We will talk in detail about this syntax later. If you see the above page, then you have done everything correctly. If not, then there is something wrong. For example, if you see the PHP code itself, then it means the PHP code is not processed and executed by the server. You have to check the file extension and make sure that the extension is .php. If you have used Notepad as the text editor, it is possible that the text editor has added a .txt to the end of the file name and the file is saved like this: phpinfo.php.txt

You have to remove the .txt from the file extension and have the file as phpinfo.php

Sending Plain Text to the Browser

We have already talked about the “print” language construct here:

printHello World!;

You just type the word print and you place the text that you want it to be displayed on the web page inside the quotes. You have to place semicolon (;) at the end of each of the PHP statements. You saw that we had semicolon at the end of phpinfo() too. However, we do not place semicolon at the end of PHP tags, conditionals, loops and… that will be discussed later.

The word print just displays the text which is inside the quotes, on the web browser. It doesn’t “print” anything on the paper.

The other thing you have to note is that print is a language construct and so you do not have to place parentheses after it. However, phpinfo is known as a function, and so, it needs parentheses: phpinfo();

This is something that you have to consider. However, it doesn’t mean that print can not be called a function.

There are some other functions that send text to the browser, including echo and printf().

Another thing that I remembered is that when a text you want to send to the browser is too long, you can break it to several lines. It will not make any problems:

print “Hello

World!”;

Sending HTML to the Browser

This is what we have already done here too. You can send HTML to the browser exactly as sending text that you did above. HTML was created to make the plain text beautiful and attractive. You can change the text colors, size, format and… using the HTML tags. To do that, you just need to embed the text with the related HTML tags. I give you some examples now. However, if you know nothing about HTML, I recommend you to read my basic HTML syntax article.

Let’s say you want to make a text look bold. Then you place the text between the <b> and </b> tags: <b>Hello, World!</b>. Then the text will look bold: Hello, World!

Now, if you want to send the bold text to the browser through PHP, you simply place the <b>Hello, World!</b> in the PHP code:

print "Hello, World!";

The only thing that you have to consider is that some of the HTML tags use the double quotation marks. For example:

Hello, World!

If you place the above HTML code that includes the double quotation marks, in the PHP code, it will make problems and the PHP will not work:

print "Hello, World!";

The reason is that the PHP code also uses the quotation marks, and it will mix up the quotation marks we have in the HTML code with its own quotation marks. On the above code, the first quotation mark tells the system that the print function argument is started. However, the second quotation mark which is related to the HTML code, makes the system assume that the print function argument is finished. After the second quotation mark, the system expects a semicolon (;) because we have to have a semicolon at the end of each PHP statement. But there is no semicolon after the second quotation mark and this makes the system not to work properly and you just see a blank page displayed on the web browser.

What Is the Solution?

One way is placing a backslash (\) before each quotation mark in the HTML code:

print "<font color=\"#FF0000\">Hello, World!</font>”;

Another way is that you avoid using the quotation mark if possible. Of course, this is against the XHTML rule that says all the HTML arguments have to be placed in quotation mark. However, it will not make any problems and the codes works properly:

print "Hello, World!";

The other ways is that you use semi quotation mark (‘) in the HTML code instead of quotation mark:

print "Hello, World!";

Although you have already created some PHP scripts that send HTML to the browser, it is a good time to create another one to make sure that you have learned it properly.

1. Save the below page in a .htm or .html file (e.g., test.htm). This will be an HTML document and has no PHP. On this page, you have used some HTML tags to make different styles for the text.





HTML Page

This is a
HTML
page.


2. The below scripts creates the same page we created above, with this difference that the HTML is sent to the browser through PHP. To create such a page, save the below codes in a .php file (e.g., text.php) and then test it in your PHP-enabled computer or server:





HTML Page


This is a
HTML
page.

";
?>

After visiting the web pages created by the above script, you can refer to the browser “View Source” and see the HTML codes of the web page. You will see that PHP has send the below codes to the browser:

This is a
HTML
page.

Adding Comments to Your PHP Scripts

The scripts we created so far, are so simple. However, when you start working as a programmer, you will create php files that include hundreds of lines of PHP codes. There is no doubt that if you code complicated scripts like that and then you refer to them after a while, you will not be able to remember why you have coded like that and what each part of the script is supposed to do. You will have to read the codes line by line and digest them in your mind to remember why you have done them. Adding comments to each and every part of the scripts you create and explaining each part will make your life much much easier. This is called documentation which is a very important thing in programming. If you do not add the comments properly and you do not do a proper documentation, in future you will not be able to modify, debug or upgrade your scripts easily. This is a very important rule in programming.

Documentation is done by adding comments. Comments are text lines that can be read by you, but they will not be known as part of the code and so the system ignores them. There are 3 ways of adding comments in PHP:

1. You can place two slashes at the beginning of the line. This will comment out the whole line. It means the system skips this line and goes to the next:

// Other objects, instances created here so we can set options on them
$this->sanitize =& new SimplePie_Sanitize;

The comment can be added after a PHP statement too:

$this->sanitize =& new SimplePie_Sanitize; // Other objects...

2. You can add a number sign (#) instead of two slashes. However, // is more common.

3. You can comment multiple lines with adding /* and */ tags:


/* Other objects, 
instances created 
here so we can set 
options on them. */
$this->sanitize =& new SimplePie_Sanitize;

Maybe you are already familiar with HTML commenting. In HTML we add comments as below:

We are learning
programming

here.

Resolving the PHP Problems and Bugs

It happens to you so many times as a programmer that your script doesn’t work at all or it works with problems. As I write more about PHP, I will tell you what to do to find and resolve the problems. However, here I list some of the initial things you have to do when you are faced with a problem.

1. PHP Has to Be Launched through a URL:

Some of the beginners try to test their PHP script through clicking on the .php file they create. PHP is not like the HTML files and PHP files can not be launched by double-clinking on them on your computer and opening them directly through your web browser. As I have already mentioned several times, PHP has to work on a PHP-enabled computer or sever. Therefore, if you make this mistake and double click on the PHP file to see how it works, it will either will not work, or it will be opened by your computer text editor. PHP always have to be ran through a URL.

2. PHP Version:

Make sure that you are using the right PHP version. Sometimes the PHP script you have written is not supported by the version of the PHP installed on the computer or server you are using to test your script. You can use your phpinfo.php file to see what version of the PHP is supported by the server. You might have to upgrade your server PHP version.

3. Turn on the Display_Errors:

Your PHP-enabled server must show the PHP error, so that you will know what the problem is. This is controled by a parameter named “Display_Errors”. To know if the Display_Errors is on, you have to check your phpinfo.php file. Run the file through entering this URL in your browser address bar: http://localhost/phpinfo.php

and then look for the Display_Errors. If it is “on”, then your web browser will display the PHP error. For example:

Parse error: syntax error, unexpected ‘>’ in C:\xampp\htdocs\test.php on line 12

Therefore you can easily fix the problem because it tells you on what line the error is.

You can turn on/off the Display_Errors through modifying the file, php.ini, which is placed inside the php folder on the server. Php.ini can be easily modified using a text editor like Notepad. You just open the file with Notepad and search for this line: display_errors = On

If you change it to display_errors = Off, then the system will not display the php errors. It is recommended to turn it off when you are done with your programming, just for the security purposes. However, if you are testing your PHP script on your computer and through running the XAMPP software, then display_errors is always on and you do not have to turn it off. If you are a XAMPP user, you can find the php.ini at C:\xampp\php

If you are faced with difficulties in finding the php.ini on your server, probably because you are using a Linux server and you are not familiar with it, you can still refer to your phpinfo.php page. Just create a phpinfo.php script and upload it to your server the way it was explained above, and then run the script by entering the phpinfo.php page URL in the web browser address bar: http://YourSite.com/phpinfo.php

The phpinfo() function will show you the php.ini location on the computer. Just look for “Loaded Configuration File” on the phpinfo.php page. It is the same if you use XAMPP on your computer. The phpinfo.php page also shows the php.ini location.

4. Double Check Your HTML Codes

Sometimes it is your HTML codes that don’t let the PHP codes work properly. We talked about one problem above. It was the problem that the quotation marks in the HTML codes can make for the PHP codes. We talked about the solutions too.

5. Check the Error Precisely:

Make sure to understand the error properly. Usually the errors you receive are exact and precise. They even give you the line number. You will have to use a text editor that show the lines numbers (Notepad++), so that, you can easily refer to the line that has the problem and fix it.

We will talk more about the debugging techniques later when we work on more complicated scripts.

How to Use the PHP Manual

I have already introduced the www.php.net. There is a PHP manual on this site at http://www.php.net/manual. In case you want to check a function you can easily do it by adding the function name to the end of the website URL. For example: http://www.php.net/print refers you to the page that explains about the print function. The below picture is the screenshot of what you will see if you enter http://www.php.net/print to your web browser address bar:

print fucntion

This is how you should read the page:

You see the name of the function (print) at the top. Below it, you see the version of PHP that the function is available in. It means not all the functions are available in all the versions. Therefore, if you see a function doesn’t work on a server, you have to make sure that it supports the last PHP version. Of course, I don’t think that you will be faced with such a problem because usually all servers are equipped with the most recent PHP function. Like all the other programming languages and computer softwares, PHP grows everyday, and so, new functions will be added and old and outdated functions will be removed.

In the blue box, it is explained how the function works and how it can be used. The white box inside the blue box is the most important part. It can be confusing too. You see the below line in the white box:

int print ( string $arg )

The first word (int) is the first 3 letters of the word integer. It means print always returns an integer value. Integer numbers are round whole numbers like 1, 2, 3, 4. Numbers like 1.03 are not integer. Specifically, the print function always returns 1.

String $arg which is inside the parenthesis means that the print function accepts only one argument which has to be a string. You have already seen it, haven’t you? We used the print function to display “Hello, World!” on the web page. “Hello, World!” which is a string, is the print argument.

To understand this better, you can compare the print function which only accepts on argument in the form of string, with another function that accepts two arguments:

Please enter this URL in your browser address bar to see the manual listing of the nl2br() function: http://www.php.net/nl2br

nl2br

The nl2br() function inserts the HTML line break tag (<br> or <br />) at the end of each string new line. You see the below line in the white box on the PHP manual page:

string nl2br ( string $string [, bool $is_xhtml = true ] )

The string at the beginning means that the nl2br() function only returns string. The string $string means that the function accepts string as the argument. The square brackets always includes the optional argument. “bool $is_xhtml = true” means the nl2br() function accepts an optional argument which has to be in the form of boolean (true/false) only. When it is optional, it means you do not have to write it. If you don’t write, the system considers it as “true”. If you want it to be false, you have to write the false argument:

nl2br(“This is the test one.”);

nl2br(“This is the test two.”, false); 

In the first line (above), the system considers the $is_xhtml as true, and so, it returns <br /> which is the XHTML version of the break tag. With the second one, the system considers the $is_xhtml as false, and so, it returns <br>.

How to Use the PHP Manual Briefly:

1. Enter the function name after the http://www.php.net/

2. If the function can not be found, double check the function name spell you added after the above URL. If still you can not find it, see the suggested function names.

3. If found, you will see the function name at the top. Below it, you will see the PHP version. You have to make sure that your server PHP version supports the function. If not, you have to upgrade your server PHP version. To know about the server PHP version, you have to check the phpinfo() page that you created above: http://localhost/phpinfo.php

4. Check the other data for the function. See what kind of data it will return. Is it string, integer or…?

5. Then check the arguments and make sure that you use the proper argument for the function.

6. If you scroll down, you will see several examples, and then the user comments after that. The examples are really helpful. Make sure to see them whenever you check a function on the function manual.

7. Sometimes you will see that some of the functions are deprecated for a special PHP version and after that. It means that function will not be used and will not work for the next version and it is replaced with some other functions.

This article was about the basic PHP syntax in general. You have learned a lot. I suggest you to read the article a few times carefully to make sure that you have understood everything. Also please make sure that you read the previous PHP training articles that I have already written. To do that, you can always refer to the below article and look for the list of my PHP training articles at the end of the article: How to Learn PHP Programming

Leave a Reply

Your email address will not be published. Required fields are marked *