Приглашаем посетить
Хемницер (hemnitser.lit-info.ru)

Your First PHP Programs

Previous
Table of Contents
Next

Your First PHP Programs

PHP is an interpreted scripting language, meaning that the language engine simply runs the scripts you have written with no necessary intermediate steps to compile or convert them into binary formats. Most of the scripts that will make up your web applications go in the same place as your HTML content files. Files containing PHP script will typically have a .php extension, although many people still use the older extensions.php3 and .phtml. Where you place these files will depend on how you have configured your web site and what extension the web server will use to pass the file to the PHP interpreter. We will refer to this location as the document root.

When the web server receives a request for an appropriately named file, it passes this to the PHP engine, which then processes the file one line at a time. For lines that do not contain PHP code (typically, lines containing HTML markup), the server emits them untouched and executes any PHP script code as it is seen. With this in mind, we write our first program, which is a mixture of PHP and HTML.

<html>
<head>
  <title>My First PHP Program</title>
</head>
<body>

<?php
  echo "Hello Everybody!";
?>
</body>
</html>

If we type this into a file called firstprogram.php and put it in our document root directory, we can then type the URL for the path to this file (for example, http://localhost/gettingstarted/firstprogram.php). We should then see something similar to Figure 1-1.

Figure 1-1. Your first PHP program in action.

Your First PHP Programs


If you have problems getting the script to run correctly, here are a few things you should investigate:

  • Make sure you entered the URL to the script file correctly, making sure that the server name is valid.

    Do not forget that the path gettingstarted is relative to the document root directory for the web site. So, if our web site's root directory were c:\inetpub\wwwroot or /home/samplesite/www, then our directory in the preceding example would be c:\inetpub\wwwroot\gettingstarted or /home/httpd/www/gettingstarted.

  • If you see just the code of your script printed onscreen, chances are that your web server has not been configured to recognize .php files as needing to be sent to the PHP interpreter.

  • If you get an error saying that the host could not be found, then you should make sure that the web server is actually running. See Appendix A for more information.

Most of the file is simply HTML text. As we have said, the PHP language engine outputs any text it sees and looks for PHP code sections. In the preceding file, the PHP code is demarcated by the tags

<?php
   ...
?>

When the PHP language engine sees these two tags, it processes the script and takes any appropriate actions. When the script portion ends, it resumes output of HTML.

To learn more about our PHP installation, we will write a small script in our next program that uses a helpful little tool. This function, called phpinfo, is used as follows:

<?php
  phpinfo();
?>

This script produces output similar to that seen in Figure 1-2. (We did not have to use HTML headers or markup since the phpinfo function emits these for us.)

Figure 1-2. Learning about our PHP interpreter with the phpinfo() function.

Your First PHP Programs



Previous
Table of Contents
Next