Sunday, 5 July 2015

A First Script php

Overview

Having installed and configured PHP, it is now time to put it to the test. In this hour,
you will create your first script and spend a little time analyzing its syntax. By the
end of the hour, you should be ready to create documents that include both HTML
and PHP.
In this hour, you will learn
How to create, upload, and run a PHP script
How to incorporate HTML and PHP in the same document
How to make your code clearer with comments

Our First Script

Let's jump straight in with a PHP script. To begin, open your favorite text editor. Like
HTML documents, PHP files are made up of plain text. You can create them with any
text editor, such as Notepad on Windows, Simple Text and BBEdit on MacOS, or VI
and Emacs on UNIX operating systems. Most popular HTML editors provide at least
some support for PHP.
Type in the example in Listing 3.1 and save the file, calling it something like
first.php.

Listing 3.1: A First PHP Script

1: <?php
2: print "Hello Web!";
3: ?>
Figure 3.1 shows the script created in Listing 3.1 as typed into the BBEdit text editor
for MacOS.
Figure 3.1: Your first script as created in the BBEdit text editor.
The extension to the PHP document is important because it tells the server to treat
the file as PHP code and invoke the interpreter. The default PHP extension for a PHP
4 document is .php. This can be changed, however, by altering the server's
configuration. You saw how to do this in Hour 2, "Installing PHP."
If you are not working directly on the machine that will be serving your PHP script,
you will probably need to use an FTP client, such as WS-FTP for Windows or Fetch for
MacOS to upload your saved document to the server.
After the document is in place, you should be able to access it via your browser. If
all has gone well, you should see the script's output. Figure 3.2 shows the output
from the first.php script.

Figure 3.2: Success: the output from Listing 3.1.

If PHP is not installed on your server or your file's extension is not recognized, you
may not see the output shown in Figure 3.2. In these cases, you probably will see
the source code created in Listing 3.1. Figure 3.3 shows what happens when an
unknown extension is encountered.
Figure 3.3: Failure: the extension is not recognized.
If this happens, first check the extension with which you saved your PHP script. In
Figure 3.3, the document was accidentally called first.nphp. If the file extension is
as it should be, you may need to check that PHP has been installed properly and that
your server is configured to work with the extension that you have used for your
script. You can read more about installing and configuring PHP in Hour 2.
Now that you have uploaded and tested your script, you can take a look at the code
in a little more detail.
Beginning and Ending a Block of PHP Statements
When writing PHP, you need to inform the interpreter that you want it to execute
your commands. If you don't do this, the code you write will be mistaken for HTML
and will be output to the browser. Table 3.1 shows the four ways of enclosing PHP
code.
Table 3.1: PHP Start and End Tags
Tag Style Start Tag End Tag
Standard
tags
<?php ?>
Short tags <? ?>
ASP tags <% %>
Script tags <SCRIPT
LANGUAGE="php"
>
</SCRIPT>
Of the tags in Table 3.1, only the standard and the script tags can be guaranteed to
work on any configuration. The short and ASP style tags must be explicitly enabled
in your php.ini. You examined the php.ini file in Hour 2.
To activate recognition for short tags, you must make sure that the short_open_tag
switch is set to "On" in php.ini:
short_open_tag = On;
Short tags are enabled by default, so you would only need to edit php.ini if you want
to disable these.
To activate recognition for the ASP style tags, you must enable the asp_tags
setting:
asp_tags = On;
After you have edited php.ini, you should be able to choose from any of the four
styles for use in your scripts. This is largely a matter of preference, although if you
intend to work with XML, you should disable the short tags (<? ?>) and work with
the standard tags (<?php ?>).
Let's run through some of the ways in which you can legally write the code in Listing
3.1. You could use any of the four PHP start and end tags that you have seen:
<?
print("Hello Web!");
?>
<?php
print("Hello Web!");
?>
<%
print("Hello Web!");
%>
<SCRIPT LANGUAGE="php">
print("Hello Web!");
</SCRIPT>
Single lines of code in PHP also can be presented on the same line as the PHP start
and end tags:
<? print("Hello Web!"); ?>
Now that you know how to define a block of PHP code, take a closer look at the code
in Listing 3.1 itself.

No comments:

Post a Comment