Elements of PHP

What is PHP?

PHP is a scripting language used widely on the Web. With it, you can query other servers, create and process HTML pages and forms, open and write to files on your server, and perform many other essential server-side web development tasks.

Coding in PHP isn’t so different from coding in other procedural languages like C or Pascal, so whatever experience you have in that type of language will carry over to your PHP coding.

Some aspects of coding in PHP might require a slight change of how you think about programming, especially if you’ve never coded for server-side deployment before. For one thing, realize that you never interact with a PHP program while it’s running; instead, you only see the results of the program after it’s already run.

For example, you don’t use PHP to display a prompt with a message box that requests a user to enter her name. Client-side (i.e. JavaScript) apps take care of those tasks. Working through your first few PHP scripts will give you a better sense of the possible applications of this language.

Preparing to code

Your environment for coding PHP applications should ideally contain these tools: an IDE (integrated development environment) for writing and debugging PHP scripts; a simulated server for testing deployment of the scripts; and a reference source listing the detailed parameters needed for all PHP functions.

You can get the last of those features easily by surfing to PHP.net, the home page of the PHP development. All documentation for the PHP language is there. The docs include sample code, links to relevant resources, and contributed comments from PHP users, whose real-world experiences in PHP can make the difference between code that works and doesn’t work.

Integrated development environment

It’s possible to code PHP scripts in a plain text editor, but your development time will be much shorter and your development experience will be much happier, if you instead type your code within an integrated development environment (IDE).

A PHP IDE will point out syntax errors as soon as you make them, so you’re not wasting time hunting down such errors after you’ve uploaded a script to your server. An IDE will also clean (“tidy”) and format your code, so all the braces and parentheses line up nicely. If you’ve ever tried fixing unmatched parentheses errors using a plain text editor, you know how vital this feature is.

An IDE will also have a feature equivalent to Microsoft’s Intellisense, which displays pop-up menus that list the parameters and description of the PHP function you’re currently typing. This feature deletes the need for constant switching back and forth between your browser and development windows to consult the PHP API.

At least two organizations offer free IDEs for PHP: NetBeans.org and Eclipse.org.

The PHP runtime environment

Since PHP runs on a server, getting a simulated server for your development environment will help you see how your PHP script runs when you deploy it on the production server. You could install just the server software on your hard drive, but your workflow will be smoother if you integrate the installation with your IDE. The help pages for the NetBeans IDE have detailed instructions on installing an implementation of Apache server, and configuring it to work with NetBeans. Find the makers of this Apache implementation here.

Basic output

Create your first PHP program by pasting the following code into your IDE.

<?php
echo &quot;I like to go moshing with dolphins a-sloshing and swim between their fins.&quot;
?>

Save the file with any appropriate filename, though be sure it ends with the extension “.php.” Upload the saved file to your server, then request its URL in your Web browser. Unless you’ve made a syntax error, you’ll see the text in the quotes output to the screen.

Exploring the code

We’ll look at how the code in this script works. But first, keep in mind that PHP scripts must end with the .PHP, .PHP4 or .PHP5 file extension. If you request a page with PHP code from your browser, and that page has a different extension from the ones just mentioned, the server simply won’t execute your script.

The script itself begins and ends with tags that mark the text inside them as PHP code:

<?php

And

?>

The PHP interpreter on your server can’t “see” your code unless it’s in these tags. But, realize that these tags can show up in several places within your script. Try this script to see this:

<html>
	<body>
	<?php
		echo &quot;I like to go moshing with dolphins a-sloshing ...&quot;;
	?>
	and swim
	<?php
		echo &quot; between their fins.&quot;;
	?>
	</body>
</html>

Continue learning PHP’s elements in part 2 of this article.

3 Comments on "Elements of PHP"

  1. Here is a recent article reviewing 8 PHP IDEs, although they did not cover my preferred IDE, Visual Studio with the VS.PHP add-in (you don’t get a feature equivalent to Intellisense, you get the real thing!).

  2. Marc Fraser says:

    Christopher, where can I get the VS PHP add-in? Only if I’d known before… Microsoft’s Intellisence is brilliant.

  3. Marc, VS.PHP is available here. It’s not free, but if you’re accustomed to Visual Studio I personally think it’s definitely worth the price.

Got something to say? Go for it!