Web Development Blog
Intro to PHP Programming
  • Posted:
  • July 28, 2012
  • Categories:
  • PHP
  • Comments:


PHP is a recursive acronym that stands for Hypertext Pre-Processor. It works things out behind the scenes by running functions and delivering appropriate code to a web-page before the page is displayed. PHP “Pre-Processes” information with the ability to calculate results, retrieve database information and pre-populate content and form fields before a website is delivered to the client! What would otherwise be a static, unchanging web page.  It gives life to  and is responsible for the dynamic nature of millions of websites. According to these PHP Usage Statistics, there are 24,840,038 sites using PHP.

Arguably the biggest reason why PHP works so well for Web Development (aside from it being FREE to use..) is the fact that it can be embedded directly into HTML documents.  Anything from variables to functions can be place in HTML within the open & close tags for the language which are “<?php  and   ?>  “.

<?php   php code here    ?>

PHP is able to “pre-process” code and information because it runs of the web server. This allows it to perform functions before the website requested by the user is displayed. However, this also means that in order to use PHP, the web server must be properly configured & equipped.

PHP is commonly used in conjunction with the Apache web server. There are two widely used acronyms that are important to understand.

WAMP – Windows Apache MySQL PHP

LAMP –    Linux   Apache  MySQL PHP

These are common acronyms that refer to particular web server configurations. MySQL is the query language that is used to work with database tables of information. PHP can fully execute MySQL commands and queries on a database.

Basic Usage 

All PHP statements must end with a semicolon; PHP uses semicolons to separate simple statements. A compound statement that uses curly braces to mark a block of code, such as a conditional test or loop, does not need a semicolon after a closing brace. Unlike in other languages, in PHP the semicolon before the closing brace is not optional.

<?php  echo “Hello, world!”;   ?>         outputs Hello, world!

Technically, the last semicolon”;” before the closing PHP tag “?>” is optional. However it is considered good practice to include it, as it will avoid errors if more code is added in later.

Whitespace and Line Breaks

In general, whitespace doesn’t matter in a PHP program. You can spread a statement across any number of lines, or lump a bunch of statements together on a single line. For example, this statement:

raise_prices($inventory, $inflation, $cost_of_living, $greed);

could just as well be written with more whitespace:

raise_prices ( $inventory , $inflation , $cost_of_living , $greed ) ;

or with less whitespace:


You can take advantage of this flexible formatting to make your code more readable (by lining up assignments, indenting, etc.). Some lazy programmers take advantage of this free-form formatting and create completely unreadable code—this isn’t recommended.

Commenting within the code

Comments within the code are incredibly useful, even if it may not seem very important when first coding. You may know what you are writing a certain line of code now, but weeks or months later after the code piles up, you very well may forget or be unsure as to what exactly is going on there. You don’t need to comment the obvious, but it is highly reccomended to comment on the more intricate workings of the code. Comments keep things organized and greatly improve the readability of code. It also helps other programmers pick up where an earlier one left off.

There are 3 different ways to comment in PHP code.

Shell-style comments using  “#”

C++ comments using   “//”

C-style comments using ” /*   ….   */”

The following explaining is taken from PHP.net Shell-style comments

When PHP encounters a hash mark (#) within the code, everything from the hash mark to the end of the line or the end of the section of PHP code (whichever comes first) is considered a comment. This method of commenting is found in Unix shell scripting languages and is useful for annotating single lines of code or making short notes.

Because the hash mark is visible on the page, shell-style comments are sometimes used to mark off blocks of code:

####################### ## Cookie functions #######################

Sometimes they’re used before a line of code to identify what that code does, in which case they’re usually indented to the same level as the code:

if ($double_check) { # create an HTML form requesting that the user confirm the action echo confirmation_form( ); }

Short comments on a single line of code are often put on the same line as the code:

$value = $p * exp($r * $t); # calculate compounded interest

When you’re tightly mixing HTML and PHP code, it can be useful to have the closing PHP tag terminate the comment:

<?php $d = 4 # Set $d to 4. ?> Then another <?php echo $d ?> Then another 4 C++ comments

When PHP encounters two slash characters (//) within the code, everything from the slashes to the end of the line or the end of the section of code, whichever comes first, is considered a comment. This method of commenting is derived from C++. The result is the same as the shell comment style.

Here are the shell-style comment examples, rewritten to use C++ comments:

//////////////////////// // Cookie functions ////////////////////////

if ($double_check) { // create an HTML form requesting that the user confirm the action echo confirmation_form( ); } $value = $p * exp($r * $t); // calculate compounded interest <?php $d = 4 // Set $d to 4. ?> Then another <?php echo $d ?> Then another 4 C comments

While shell- and C++-style comments are useful for annotating code or making short notes, longer comments require a different style. As such, PHP supports block comments, whose syntax comes from the C programming language. When PHP encounters a slash followed by an asterisk (/*), everything after that until it encounters an asterisk followed by a slash (*/) is considered a comment. This kind of comment, unlike those shown earlier, can span multiple lines.

Here’s an example of a C-style multiline comment:

/* In this section, we take a bunch of variables and assign numbers to them. There is no real reason to do this, we’re just having fun. */ $a = 1; $b = 2; $c = 3; $d = 4;

Because C-style comments have specific start and end markers, you can tightly integrate them with code. This tends to make your code harder to read, though, so it is frowned upon:

/* These comments can be mixed with code too, see? */ $e = 5; /* This works just fine. */

C-style comments, unlike the other types, continue past end markers. For example:

<?php $l = 12; $m = 13; /* A comment begins here ?> <p>Some stuff you want to be HTML.</p> <?= $n = 14; ?> */ echo(“l=$l m=$m n=$n\n”); ?> <p>Now <b>this</b> is regular HTML…</p> l=12 m=13 n= <p>Now <b>this</b> is regular HTML…</p>

You can indent, or not indent, comments as you like:

/* There are no special indenting or spacing rules that have to be followed, either. */

C-style comments can be useful for disabling sections of code. In the following example, we’ve disabled the second and third statements by including them in a block comment. To enable the code, all we have to do is remove the comment markers:

$f = 6; /* $g = 7; # This is a different style of comment $h = 8; */

However, you have to be careful not to attempt to nest block comments:

$i = 9; /* $j = 10; /* This is a comment */ $k = 11; Here is some comment text. */

In this case, PHP tries (and fails) to execute the (non-)statement Here is some comment text and returns an error.


 Next time we will start looking at how to work with variables and simple calculations, along with simple functions!