Starting to make php's first Script. A First Script |
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 in clude 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. T o 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. |
|
|
41 |
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. |
|
42 |
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 |
<?php ?> |
tags |
Short tags <? ?> |
ASP tags <% %> |
Script tags <SCRIPT |
</SCRIPT> |
LANGUAGE="php" |
> |
43 |
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: |
44 |
<? 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. |
The print() Function |
print() is a function that outputs data. In most cases, anything output by print() |
ends up in the browser window. A |
function |
is a command that performs an action, |
usually modified in some way by data provided for it. Data sent to a function is |
almost always placed in parentheses after the function name. In this case, you sent |
the print() function a collection of characters, or string. Strings must always be |
enclosed by quotation marks, either single or double. |
Note |
Function calls generally require parentheses after their name whether or |
not they demand that data be passed to them. print() is an exception, |
and enclosing the data you want to print to the browser in parentheses is |
optional. This is the more common syntax, so we will usually omit the |
brackets in our examples. |
You ended your only line of code in Listing 3.1 with a semicolon. The semicolon |
informs the interpreter that you have completed a statement. |
NEW |
A |
statement |
represents an instruction to the interpreter. Broadly, it |
TERM |
is to PHP what a sentence is to written or spoken English. A |
statement should usually end with a semicolon; a sentence should |
end with a period. Exceptions to this include statements that |
enclose other statements, and statements that end a block of code. |
In most cases, however, failure to end a statement with a semicolon |
will confuse the interpreter and result in an error. |
Because the statement in Listing 3.1 is the final one in that block of code, the |
semicolon is optional. |
Combining HTML and PHP |
The script in Listing 3.1 is pure PHP. You can incorporate this into an HTML |
document simply by adding HTML outside the PHP start and end tags, as shown in |
Listing 3.2. |
Listing 3.2: A PHP Script Including HTML |
1: <html> |
2: <head> |
|
45 |
3: <title>Listing 3.2 A PHP script including HTML</title> |
4: </head> |
5: <body> |
6: <b> |
7: <?php |
8: print "hello world"; |
9: ?> |
10: </b> |
11: </body> |
12: </html> |
As you can see, incorporating HTML into a PHP document is simply a matter of |
typing in the code. The PHP interpreter ignores everything outside PHP open and |
close tags. If you were to view Listing 3.2 with a browser, as shown in Figure 3.4, |
you would see the string "hello world" in bold. If you were to view the document |
source, as shown in Figure 3.5, the listing would look exactly like a normal HTML |
document. |
You can include as many blocks of PHP code as you need in a single document, |
interspersing them with HTML as required. Although you can have multiple blocks of |
code in a single document, they combine to form a single script. Anything defined in |
the first block (variables, functions, or classes, for example) u sually will be available |
to subsequent blocks. |
Figure 3.4: |
The output of Listing 3.2 as viewed in a browser. |
|
46 |
Figure 3.5: |
The output of Listing 3.2 as HTML source code. |
Adding Comments to PHP Code |
Code that seems clear at the time of writing, can seem like a hopeless tangle when |
you come to amend it six months later. Adding comments to your cod e as you write |
can save you time later on and make it easier for other programmers to work with |
your code. |
NEW |
A |
comment |
is text in a script that is ignored by the interpreter. |
TERM |
Comments can be used to make code more readable, or to annotate a |
script. |
Single line comments begin with two forward slashes ( / /) or a single hash sign (#). |
All text from either of these marks until either the end of the line or the PHP close tag |
is ignored. |
// this is a comment |
# this is another comment |
* |
Multiline comments begin with a forward slash followed by an asterisk (/ |
) and end |
* |
with an asterisk followed by a forward slash ( |
/). |
* |
/ |
this is a comment |
none of this will |
be parsed by the |
interpreter |
/ |
* |
Summary |
You should now have the tools at your disposal to run a simp le PHP script on a |
properly configured server. |
In this hour, you created your first PHP script. You learned how to use a text editor |
to create and name a PHP document. You examined four sets of tags that you can |
use to begin and end blocks of PHP code. You learned how to use the print() function |
to send data to the browser, and you brought HTML and PHP together into the same |
script. Finally, you learned about comments and how to add them to PHP |
documents. |
Q&A |
Q Which are the best start and end tags to use? |
A |
It is largely a matter of preference. For the sake of portability the standard tags |
(<?php ?>) are probably the safest bet. Short tags are enabled by default and have |
the virtue of brevity. |
Q What editors should I avoid when creating PHP code? |
A |
Do not use word processors that format text for printing (such as Word, for |
example). Even if you save files created using this type of editor in plain text format, |
hidden characters are likely to creep into your code. |
Q When should I comment my code? |
A |
This is a matter of preference once again. Some short scripts will be |
self-explanatory to you, even after a long interval. For scripts of any length or |
complexity, you should comment your code. This often saves you time and |
frustration in the long run. |
Workshop |
The Workshop provides quiz questions to help you solidify your understanding of the |
material covered. Try to understand the quiz answers before continuing to the next |
hour's lesson. Quiz answers are provided in Appendix A. |
Quiz |
Can a user read the source code of PHP script you have successfully installed? |
What do the standard PHP delimiter tags look like? |
What do the ASP PHP delimiter tags look like? |
What do the script PHP delimiter tags look like? |
What function would you use to output a string to the browser? |
Activity |
Familiarize yourself with the process of creating, uploading, and running PHP |
scripts. |
0 comments:
Post a Comment