Welcome to the first chapter of the tutorial on PHP Web Programming.
In this tutorial we will cover the following:
- About PHP5 Scripting Language
- PHP Tags
- PHP Comments
- How to display data on the browser
- A note on line terminator
- Installing PHP & Apache on your machine for development
- Setting up Virtual Hosting on your machine for development
Fasten your seat belts and enjoy the lovely journey towards learning PHP 5.
About PHP5 Scripting Language
PHP5 is a scripting language for the web to render dynamic pages (See note below on dynamic and static pages). PHP is used for server side scripting and can also be used for developing CLI (Command Line Interface) programs. PHP is a recursive acronym for PHP: Hypertext Preprocessor.
Note on Dynamic and Static Pages
Initially when the World Wide Web was created all pages were static. This means that someone manually had to change the pages and upload it to the server again. This was a big issue in terms of creating a Web Application that would keep changing / updating a page depending on the data available in their database server.
Therefore, came the concept of CGI (Common Gateway Interface), through which you could ask for script pages instead of static HTML pages. Once a user hits the Script URL CGI would allow for the script engine to execute the page and throw HTML output to the browsers. This is called Dynamic Pages.
PHP Tags
With PHP it is possible to embed the presentation HTML along with your PHP Code. Therefore, for PHP to understand the area of your file where PHP code resides, you need to use PHP Tags.
Code Example:
<?php
echo “Hello World”;
? >
PHP allows 3 types of tags, as follows:
Standard Tags |
<?php
… code
?> |
Short Tags |
<?=$variable?> |
Script Tags |
<script language=”php”>
… code
</script> |
ASP Tags |
<%
… code
%> |
Standard tags are the de-facto opening and closing tags; they are the best solution for
portability and backwards compatibility, because they are guaranteed to be available
and cannot be disabled by changing PHP’s configuration file.
Short tags were, for a time, the standard in the PHP world; however, they do have
the major drawback of conflicting with XML headers and, therefore, have somewhat
fallen by the wayside. Their other advantage is the availability of the short form
<?=$variable ?> syntax, which allows you to print the result of an expression directly
to the script’s output.
Script tags were introduced so that HTML editors which were able to ignore
JavaScript but were unable to cope with the standard PHP tags could also ignore
the PHP code.
Nobody quite understands why ASP tags were introduced—however,
if you are so inclined you can turn on this optional configuration option, and you are
free to use them.
Note: Short tags, script tags and ASP tags are all considered deprecated and their use is
strongly discouraged.
PHP Comments
It is a good practice to comment your code. Comment is nothing but a textual explanation of what a piece of code does. This is necessary because over a period of time you as a developer would not remember how the logic works. Comments serve as inline documentation for the code and aids speedy maintenance activity.
PHP allows developers to comment as following:
Single line comment (//) |
<?php
//this logic does that
… code
?> |
Single line comment (#) |
<?php
#this logic does that
… code
?> |
Multi-line comment (/*…*/) |
<?php
/*this logic does that and requires
two lines of comments */
… code
?> |
How to display data in PHP
PHP provides two methods of displaying data in the browser:
a) echo
b) print
Either of these statements can be used to display a string output on the browser or command line. Look at the examples below:
Example 1 : How to output a String
<?php
echo “Hello World”;
print “Hello World”;
?>
In the example above, look at the way Hello World has been enclosed in double quotes. Note that in PHP a string should be enclosed in double quotes. Though you could also enclose it with single quotes (‘Hello World’) because PHP is not type strict. But as a general practice it is better if you follow the double quote principle. By following this way of using string; if you keep developing projects across various different programming languages then the standard still gets maintained.
Example 2 : How to output a variable
<?php
$name = “Sunil Bhatia”;
echo “My name is ” . $name;
print “My name is ” . $name;
?>
In the example above we first assign a string to a variable $name (we will cover more about variables in the next tutorial). Look at how we have used the (.) dot operator to concatenate (join) two strings i.e. First String “My name is” and second string variable $name.
Concatenation means to join two strings so that the result of this operation becomes a joined string. The dot operator (.) is used to concatenate two strings. Just like we would use a + operator in Java or & operator in ASP.
A note on line terminators
As a developer you will be writing lines of code. But how would PHP know when a line of code is over. You would say that PHP should identify end of line because of the new line character i.e. <Enter> key. But there are many statements that you could be written on multiple lines or the same line. So, how would PHP identify end of line.
Like C, Java and C#; PHP also used a semi-colon (;) as a statement terminator. This is mandatory and should not be ignored.
Look at the example below
<?php
$name = “Sunil Bhatia”;
echo $name;
?>
Do you see that both the line of codes are terminated by a semi-colon (;).
Installing PHP & Apache on your machine for development
Installing PHP & Apache is easier than you think. Read this tutorial on the site to have a complete understanding:
How to Install Apache on Windows
The following link will help you understand how to configure PHP for Apache on Windows:
How to configure PHP on Apache for Windows
Setting up Virtual Hosting on your machine for development
Read this post on how to setup virtual hosting on your machine with Apache
Setting up Virtual Hosting in Apache on Windows
In the next tutorial we will learn about PHP Variables.
Cisco is considered one of the most important certification providers. Cisco exams CCNA IIUC Voice 640-460 and CCIE 350-029 are one of the most important certification exams. Cisco introduced 642-654 WAASSE Wide Area Application Services and 650-059 LCSARS Lifecycle Services Advanced Routing and Switching to enhance the network security. Microsoft MCSE 70-298 certification exam is the most popular exam.