Tuesday, 7 July 2015

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
TERM
A statement represents an instruction to the interpreter. Broadly, it
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>
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) usually will be available
to subsequent blocks.
Figure 3.4: The output of Listing 3.2 as viewed in a browser.
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 code as you write
can save you time later on and make it easier for other programmers to work with
your code.
NEW
TERM
A comment is text in a script that is ignored by the interpreter.
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
*/

No comments:

Post a Comment