Find PHP tutorials for Dummies on the Web, but PHP for smarties here

Find PHP tutorials for Dummies on the Web, but PHP for smarties here
Page content

PHP Tutorials for Dummies

PHP tutorials for dummies can be found all over the Web. But, if you’re interested in programming in PHP, you can’t be all that dumb. PHP is a scripting language used widely on the Web. With PHP, you can process web page forms, search your server’s directories, fetch content from other servers, execute queries on your MySQL database, and more.

One benefit of developing in PHP is fairly rapid development time–if you have a rich IDE (integrated development environment) with syntax highlighting and PHP library lookups, similar to Microsoft’s Intellisense. Also, PHP is an interpreted, rather than compiled, language, meaning that you don’t have to build the executable from the script. Instead, you hand your script off to the PHP interpreter for execution.

Code completion in a PHP IDE

Set up your development environment

Undertake one of the following two development options to begin coding in PHP.

Option one involves signing up with a hosting company that has PHP. You’ll write PHP code on your computer, upload it to your server, then request the PHP page in your browser to have it executed on the server. Option two involves simulating a server on your computer by using server software, such as Apache.

You can do either option for free. Find a listing and review of free PHP hosting companies here.

To do the server emulation option, first get a free PHP IDE from these sites: NetBeans.org, Eclise.org, or Aptana.org. Then, get a package that includes MySQL, PHP and server software bundled together. Here’s one resource for that approach.

The remainder of this article will assume you’ve set up one of the development environment options just mentioned.

Howdy, Planet

Using your IDE or windows Notepad, paste the following PHP program into your code window:

Save the file as “MyFirstPHPApp.php,” then upload it to your server, (or your simulated server on your hard drive). Open up a browser window on the file you just uploaded, to cause the server to execute the script. Notice the resulting message from the script.

How it works

Take a look at the script’s content, which starts and ends with the “” tags, as all PHP scripts must. We gave the PHP another hint that our script was PHP and not HTML or something else: the filename extension of the script: .php. Be sure each of your PHP scripts has that extension in its filename.

Use Echo for output

There’s little doubt about what the “Echo” statement is doing, but learn a few things about this statement besides its obvious function.

First, you can use other output statements besides echo. Replace that “echo” in the script with this statement: “print.” Re-run the script using the instructions given previously. The output will be the same.

Realize that you could have written “PRINT” or “ECHO,” in place of the lowercase text you typed–but that doesn’t mean PHP is case-insensitive. Try the following example to reveal PHP’s case sensitivity. Replace the code in your script with this code:

"; PRINT "Hail ".$t."! Well met."; ?>

Run the script as you ran its previous incarnation, and notice the output: two different statements, coming from two different variables. The variable names are both “T"s, but differ in case. The PHP interpreter would not have printed two different statements if it were case-insensitive. In short, pay attention to case when you code in PHP. Your IDE’s syntax highlighting will help you with this.

Punctuation

Notice the punctuation used in the foregoing script, the period and semicolon in particular. You can probably guess correctly that semicolons are needed to terminate each PHP statement.

Guessing the role of the period might be a little tougher: it’s a concatenator of strings; it joins them together. Compare this with JavaScript’s syntax:

alert (“hello " + “world”);

JavaScript uses the plus to join strings. Be careful if you’re a veteran JavaScript programmer, just learning PHP. Change your pluses to periods.

Variables

Take a look at the variable assignment statements in the foregoing script. If you sense there’s something missing, you’re right. The variable declarations aren’t there. In PHP, you don’t have to declare variables; you can just start using them. A variable comes into existence when you assign something to a token. That assignment looks like this:

$x = “stuff”; or $x = 2;

The PHP interpreter is smart enough to treat the variable as it should be based on its type–be it integer, floating-point number, string or some other type.

Read more about PHP variables here.

Creating HTML content

If you look back at the script with the PRINT statement, you’ll notice a tiny bit of HTML in one of those statements: “
.” This statement is a hint that you can use PHP to display any HTML content. Try this example:

"; echo ""; echo ""; echo ""; echo ""; echo ""; echo "

Here's some formatted text.

"; echo ""; echo ""; ?>

This snippet features PHP creating HTML, JavaScript and CSS. The result is web content that looks like a normal HTML page, once you download it to your browser.

You can start to get a sense of PHP’s ability to create dynamic HTML content by replacing the line “Here’s some formatted text” with this one:

echo “

Here’s the date: “.date(‘M-d-Y’)."

”;

See this page for detailed information about PHP’s date functions and how to format them.

These last few fragments point out another detail about how PHP handles strings. You can surround strings with either double or single quotation, as long as you end a double quote with a double quote and a single with a single.

Form processing

One common application of PHP is to validate and process the input from HMTL forms. The following script gives an example of that process.

Paste the following script into a new PHP file, then use the previous examples’ instructions to test it in your browser.

Enter a valid email address.

'; } function showSuccessPage() { echo '

Thank you for your information!

'; } ?>

Notice how the PHP displays complete HTML pages, and how it chooses which page to display based on what’s in the predefined $_POST variable.

The $_POST variable is an array that’s populated with the names and values of all the elements inside an HTML form element. This populating happens when the user presses the submit button on the form, which calls the PHP form-processing script.

In this example, the script first checks for an empty $_POST element, which would indicate that the form must first be displayed to the user. That element will contain user input text only after the user hits the HTML form’s submit button. Once the PHP script detects that input via a non-empty $_POST[“email”], it will validate the email and display a new HTML page appropriate to the results of the validation.

Learn more about PHP form submission here.

Search the Web for PHP tutorials for dummies, but come to Bright Hub to learn PHP for smarties.

References

Home page of the PHP Hypertext Processor.