Hardware Reference
In-Depth Information
Figure 1-5
The results of your first PHP script,
in a browser.
the file you ask for is a PHP script, the web server applica-
tion will look for your file and execute it. It'll then send a
message back to you with the results.
!
If you see the PHP source code instead of what's
shown in Figure 1-5, you may have opened up the
PHP script as a local file (make sure your web browser's
location bar says http:// instead of file:// ).
For more on this, see Chapter 3. For now, let's get a
simple PHP program or two working. Here's your first
PHP program. Open your favorite text editor, type in the
following code, and save it on the server with the name hello.
php in your public_html directory (your web pages may be
stored in a different directory, such as www or web/public):
(replace example.com with your website and any addi-
tional path info needed to access your home files, such as
http://tigoe.net/~tigoe/hello.php ). You should get a web
page like the one shown in Figure 1-5.
<?php
echo "<html><head></head><body>\n";
echo "hello world!\n";
echo "</body></html>\n";
?>
If it still doesn't work, your web server may not be configured
for PHP. Another possibility is that your web server uses a
different extension for php scripts, such as .php4. Consult
with your web hosting provider for more information.
Now, back at the command line, type the following to see
the results:
You may have noticed that the program is actually printing
out HTML text. PHP was made to be combined with HTML.
In fact, you can even embed PHP in HTML pages, by using
the <? and ?> tags that start and end every PHP script. If
you get an error when you try to open your PHP script in a
browser, ask your system administrator whether there are
any requirements as to which directories PHP scripts need
to be in on your server, or on the file permissions for your
PHP scripts.
php hello.php
You should get the following response:
<html><head></head><body>
hello world!
</body></html>
Now, try opening this file in a browser. To see this program
in action, open a web browser and navigate to the file's
address on your website. Because you saved it in public_
html, the address is http://www.example.com/hello.php
 
Search WWH ::




Custom Search