The PHP Programming Language

Posted by: Rea Maor In: Programming - Thursday, May 10th, 2007

By nearly every statistic that measures web use, PHP is the current Golden Boy of server-side scripting languages. Perl runs a close second. The two of them balance out fairly well in pros and cons. Nearly every Open Source website tool you can get (blog systems, forum packages, shopping cart apps) is coded in PHP. A PHP developer can also make some good money, currently.

PHP is almost identical in syntax to C, with a few borrowed Perl traits wherever it would make sense to do it Perl’s way. PHP is mega-powerful, able to use the full capabilities of C, interact with SQL, PDF, Java, and Javascript, and generate any markup from HTML to XML. Oh, yes, and it can be embedded into a page, too. Accomplishing zany tricks like this:

if($x==1) {
<h3>Greetings, and welcome to my home page!</h3>
} else {
<h3>Go away!</h3>

Now, the “<?php” starts the scripting engine, and “?>” stops it. Yet this control structure performs correctly anyway, allowing one or the other HTML tagged content to print based on what the if…else block in PHP decides. So, you really don’t need the “echo” function if you find it too much of a hassle.

PHP is available for Windows and Unix, and the Mac. If you get the PHP binary for your home computer, you can also practice your scripting skills by simply writing up your script and saving it with the “.php” extension, then summoning php from the command line to interpret the file and save it as HTML. On Unix systems, it’s this simple:

php my_script.php > my_page.html

Then you can open the HTML file you’ve generated and see what the page will look like. What you see is what you will get on the server.

So, let’s thrash through the syntax:


/* Comments have 'wings' or... */
// Begin with double-slashes on every line. Just like C!

Data declarations:

$page_name="Welcome to my $page_name";

Note the middle one… This is one thing you can’t get away with in C, dereferencing a variable inside a sting. PHP is forgiving.

Output statement:

echo "<p>This is a <b>formatted</b> paragraph of output.</p>";

You can also simply leave PHP at any time and output in pure HTML, as in the first example.

Input statement:

Like with Javascript, there is no direct way to collect input from the page visitor. Instead, an HTML input can be set up (with “<form>” tags) to collect data from the visitor and call it by variable. For what it’s worth, input can also be had from the command line in a C-like manner:

$line = trim(fgets(STDIN)); // reads one string line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN

You’ll recognize the same function names from C, even.



File access:

$text_data = fopen("/site-data/file.txt", "r");
$image_handle = fopen("/my_images/file.gif", "wb");

As the second line suggests, PHP can generate images, too, here writing a .gif image. The “wb” is the access mode “binary write”. There are actually a whole library of input and output functions available.

Conditional statements:

if ($foo==$bar)

Fixed iteration:

$MyArray = array(1, 2, 3, 4, 5);
foreach ($MyArray as &$value) {
   echo $value * 2."<br />";

Conditional iteration:

while ($number <= 10)
echo $number++."<br />";


include 'essential_functions.php';

And everything in your essential functions file gets read in. For the basic functions you will need, PHP actually has a huge library built in. Only for special-purpose plug-ins would you be requiring files. But you can freely include what you want, when you want.


function foo($x, $y)
foo($x, $y);

After declaring a function, you call it without the dollar sign, unlike a variable.

That’s the basics of PHP.

Noteworthy is the highly helpful PHP Manual website, which itself uses PHP. In addition to being one of the most comprehensive manuals on any language posted to the Internet (it’s right up there with Python’s doc site.), the PHP manual site also functions as a bulletin board, allowing members to post comments appended to every manual page. The discussions are even more illuminating, as many coders share their tricks and tips. There is much to learn here.

That wraps up our tour of languages; we’ll summarize our findings in the next installment and call it a day. If you’ve read this whole series straight through, congratulations! Even if you never program, you at least have some understanding of the inner workings of software, which is much more than the majority of the public can say!

Related Posts:

2 Responses to “The PHP Programming Language”

  1. webjourneyman Says:

    I read that PHP has gotten a bad rep because so many newbies in programming have used it to create bad code. Unintentionally I guess that say’s something about it’s ease of use/powerfulness as well, but what about best practices, is there any guide or rule of thumb to what is good and what is bad practice in PHP?
    If you can sum it up in one paragraph; When programming using PHP don’t … ? But do … ?

  2. Rea Maor Says:

    Well, in my opinion php is just like ASP, its powerful yes. But because it’s so forgiving on the syntax it’s very hard to start from PHP and carry on learning other languages…

    There is no DO and DONT, it’s all up to you – it is best to start programming in php after you already have good C / C++ base, but don’t let it stop you…

Leave a Reply