Friday, May 9, 2008
PHP Files, Syntax, and Coding Basics...




PHP and MySQL Tutorials
PHP Files, Syntax, and Coding Basics...

Introduction to PHP and Dynamic Websites...

Learn how to configure Apache, PHP, MySQL & PHPMyAdmin...

PHP Coding Basics:

In this lesson we are going to introduce you to PHP files, PHP syntax, and briefly cover how to send information to the browser.

Note: Some of the topics covered over the next few lessons may seem very basic, but don't be discouraged. Once you have a firm foundation in the basics of PHP we'll move on to the more advanced and fun stuff.

PHP Files:

PHP files all have a ".php" extension (for example, index.php or users.php). When you first create a PHP file it might display as an "unknown application". If it does, simply double click the file, "select a program from a list", and choose 'notepad' as the default program you wish to open the file in.

Note: Do NOT use a WYSIWYG editor (such as FrontPage or Dreamweaver) to create or edit your PHP code. Programs like this will mess up the code and cause the file to render an error.

In the previous lesson we explained that PHP is a sever-side scripting language. While this gives us an enormous amount of advantages, there is one downside: PHP scripts can only be executed by a server. This means that you can't run them locally on your PC unless you have a server and PHP installed. There are two solutions:
  1. Purchase a Hosting Account with a server that has PHP installed. You can then upload your PHP files to the web to test them.
  2. Install a server on your PC (to learn how to install the Apache server, read this article). This will allow you to develop your applications locally, without the hassle of having to purchase a hosting account or FTP files.

PHP Syntax:

Below are two examples of the PHP start and end tags. Anything placed within these tags will be treated as PHP by the server (meaning that the code will first be parsed by the server rather than immediately sent to the browser).

Example 2.1 (The Formal):
<?php

?>

Example 2.2 (The Informal):
<?

?>

Which you use is a matter of personal preference. I prefer the formal style, so for sake of consistency I'll be using that format for the rest of the lessons.

Note: There are actually two additional types of PHP tags. Besides the two mentioned above, there are the ASP (<% %>) and Script (<script language="php"> </script>) styles. However, these are far less common.

Sending Information to the Browser:

PHP has many built in functions to send data to the browser. The two that you'll be using the most are echo() and print():

Example 2.3:
<?php

echo 'Hello World!';
print 'How are you?';

// or

echo "How's it going?";
print "Isn't PHP great?";

?>

As our two examples show, you can use either single or double quotation marks (we'll discuss more about this in the next chapter). Also note that all PHP statements end with a semicolon.

Errors and Escaping:

Like any programming language if you don't follow the syntax correctly the script will die (fail). If you're using single quotation marks to print a statement, then the statement cannot contain a single quotation mark. The same rule applies to double quotation marks.

The following example will render an error:

Example 2.4:
<?php

echo 'This isn't going to work.';

?>

So how do we get around this? There are two solutions:
  • Use single quotations when printing a statement that contains a double quotation mark (and vice versa):

    echo "How're you?";
    echo 'And the king said, "PHP is the greatest".';

  • "Escape" the quotation mark by putting a backslash immediately before it:

    echo 'How\'re you?';
    echo "And the king said, \"PHP is the greatest\".";

If you don't take this precaution then PHP will render an error similiar to:

An example of a PHP Error


Now that you have a basic understanding of the PHP syntax and how to send information to the browser, let's create your first PHP page:

Example 2.5 (Your First PHP Page):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
   <title>My First PHP Page!</title>
</head>
<body>

<?php

echo '<h1>My First PHP Page</h1>';

echo '<p>Hello everyone! This is my first PHP Page.</p>';

?>

</body>
</html>

If everything goes well, it should display:

Your First PHP Page!

« Return to the Lessons Page | Continue to Lesson 3 »