By Paul Hudson
Book Price: $29.95 USD
£20.95 GBP
PDF Price: $23.99
Cover | Table of Contents | Colophon
<?php and ?>. Here is an example of a simple page, shown in Perl first and then in PHP—don't worry about what the code means for now:
#!/usr/bin/perl
print <<"EOHTML"
<html>
<body>
<p>Welcome, $Name</p>
</body>
</html>
EOHTML
<html>
<body>
<p>Welcome, <?php print $Name; ?></p>
</body>
</html>
CGI.pm) help, but PHP continues to have a lead in terms of readability. If you wanted to, you could write your PHP script like the Perl script: switch to PHP mode and print everything out from there.http://www.php.net/manual. The manual contains documentation on all PHP functions, as well as various usage examples, and also user comments. Very often it's the user comments that are most helpful, because people recount problems they've experienced in the past and how they got around them. The PHP manual is an excellent resource that should help you deepen your understanding of all aspects of the language.http://www.php.net/mailing-lists.php to see a list of possibilities. You will most likely want the general mailing list, as it includes hundreds of questions and answers being sent each day.http://www.php.net/downloads.php. This contains the main PHP executables and DLLs, plus many extensions pre-compiled and ready to use.http://httpd.apache.org. This is packaged using the Microsoft Installer system (MSI), so you may be prompted to install the MSI software if you have an older release of Windows.http://www.php.net/downloads.php. This contains the main PHP executables and DLLs, plus many extensions pre-compiled and ready to use.http://httpd.apache.org. This is packaged using the Microsoft Installer system (MSI), so you may be prompted to install the MSI software if you have an older release of Windows.# symbol is a comment, and may provide further documentation to guide you in your edits. First, search for the string "LoadModule." There should be a collection of these LoadModule lines in there already, so scroll to the bottom and add this new one:apache2-2.0.53-9mdk. Select that, and you'll be prompted to include all the dependencies also (these are required). If you scroll down the list of search results, you should also see apache2-mod_php-2.0.53-4.3.10-7mdk, which is the package for PHP 4.3. Yes, that's quite out of date, but that's the result of installing through a package manager.
<?php
phpinfo()
;
?>
http://localhost/info.php in your web browser. All being well, you should see a lot of information printed out about your PHP configuration. This is actually a handy script to keep around for debugging purposes, as it tells you exactly what extensions you have installed and what their configuration options are. Of course, it also tells any hackers about your system configuration
, so don't advertise its existence!|
Option
|
Meaning
|
Default
|
|---|---|---|
assert.active
|
Enables the assert() function
|
On
|
display_errors
|
Sets whether PHP should output error messages to the screen
|
Off
|
error_reporting
|
Decides what types of errors PHP should notify you of
|
E_ALL
|
expose_php
|
Allows PHP to identify itself to clients through the web server
|
On
|
|
|
<?php
phpinfo();
?>
<?php
phpinfo();
?>
PEAR::DB, and provides an object-oriented, database-independent framework for reading from and writing to your database. PEAR::DB is covered in depth in Chapter 14.
<?php
$i = 10
$j = 5;
if (($i + 2) - ($j + 5) == 10 {
print "Success!";
}
?>
"== 10", causing another error.
<?php
// option 1
print "Hello, ";
print "world!";
// option 2
print "Hello, "; print "world!";
?>
print is technically not a function and, technically, they are correct. This is why print doesn't require brackets around the data you pass to it. Other language constructs that masquerade as functions (and are herein referred to as such for the sake of sanity) include array, echo, include, require, return, and exit.
<?php
print("Hello!");
?>
print and echo appear the same, they are not. The print construct behaves more like a function than echo
<?php
// option 1
print "Hello, ";
print "world!";
// option 2
print "Hello, "; print "world!";
?>
print is technically not a function and, technically, they are correct. This is why print doesn't require brackets around the data you pass to it. Other language constructs that masquerade as functions (and are herein referred to as such for the sake of sanity) include array, echo, include, require, return, and exit.
<?php
print("Hello!");
?>
print and echo appear the same, they are not. The print construct behaves more like a function than echo because it returns a value (1). However, echo is more useful because you can pass it several parameters, like this:
<?php
echo "This ", "is ", "a ", "test.";
?>
print, you would need to use the concatenation operation (.) to join the strings together, rather than a comma. If you have several things to print out, as in that example, then $ followed by a letter or an underscore, then any combination of letters, numbers, and the underscore character. This means you may not start a variable with a number. One notable exception to the general naming scheme for variables
are "variable variables," which are covered in the next chapter. A list of valid and invalid variable names is shown in Table 4-1.
$myvar
|
Correct
|
$Name
|
Correct
|
$_Age
|
Correct
|
$_ __AGE_ __
|
Correct
|
$91
|
Incorrect ; starts with a number
|
$1Name
|
Incorrect ; starts with a number
|
$Name91
|
Correct; numbers are fine at the end and after the first character
|
$_Name91
|
Correct
|
<?php
$name = "Paul"; print "Your name is $name\n";
$name2 = $name; $age = 20;
print "Your name is $name2, and your age is $age\n";
print 'Goodbye, $name!\n';
?>
EOT (end of text) for our delimiter, meaning that we can use double quotes and single quotes freely within the body of the text—the string only ends when we type EOT.
<?php
$mystring = <<<EOT
This is some PHP text.
It is completely free
I can use "double quotes"
and 'single quotes',
plus $variables too, which will
be properly converted to their values,
you can even type EOT, as long as it
is not alone on a line, like this:
EOT;
?>
EOT is just an example.<<< before the delimiter to tell PHP you want to enter heredoc mode.<?php to enter PHP mode, and ?> to leave PHP mode, but you can also use the short tags version, <? and ?>.<?=, like this:
<?="Hello, world!" ?>
<?php
print "Hello, world!";
?>
<? to open code blocks. This means that if you try to use XML and short-tagged PHP together, you will encounter problems—this is the primary reason people recommend using the normal open and close tags. Short tags are always dangerous because they can be disabled in the PHP configuration file, php.ini, which means your scripts may not be portable.<% %>, which opens and closes code blocks in the same way as Microsoft's ASP, and also <script language="php"></script>. These two often work better with visual editor programs such as Dreamweaver and FrontPage, but they are not recommended for general use because they need to be enabled to work.<?php and ?> whenever and as often as you want to.//, /* */, and #. // and # mean "Ignore the rest of this line," whereas /* means "Ignore everything until you see */." Some complications exist with /* and */ that make them less desirable to use.
<?php
print "This is printed\n";
// print "This is not printed\n";
# print "This is not printed\n";
print "This is printed\n";
/* print "This is not printed\n";
print "This is not printed\n"; */
?>
/* */ form of commenting. If you were to start a /* comment on line one, and end it on the line near the bottom where the other /* comment is started, you would find that the script would fail to work. The reason for this is that you cannot stack up, or "nest," /* */ comments, and attempting to do so will fail spectacularly.// for your commenting purposes, simply because it is easy to spot, easy to read, and easy to control.
<?php
$Age = 20;
if ($Age < 18) {
print "You're young - enjoy it!\n";
} else {
print "You're not under 18\n";
}
if ($Age >= 18 && $Age < 50) {
print "You're in the prime of your life\n";
} else {
print "You're not in the prime of your life\n";
}
if ($Age >= 50) {
print "You can retire soon - hurrah!\n";
} else {
print "You cannot retire soon :( ";
}
?>
if statements left to right, meaning that it first checks whether $Age is greater or equal to 18, then checks whether $Age is less than 50. The double ampersand, &&, means that both statements must be true if the print "You're in the prime of your life\n" code is to be executed—if either one of the statements is not true for some reason, "You're not in the prime of your life" is printed out instead. The order in which conditions are checked varies when operator precedence matters; this is covered in the next chapter.&&, there is also || (the pipe symbol printed twice) which means OR. In this situation, the entire statement is evaluated as true if any of the conditions being checked is true.1 == 1 is true, and 1 == 2 is false.if...elseif blocks can become unwieldy when you have a series of conditions that all test against the same variable, as here:
<?php
$Name = "Bob";
if ($Name = = "Jim") {
print "Your name is Jim\n";
} elseif ($Name = = "Linda") {
print "Your name is Linda\n";
} elseif ($Name = = "Bob") {
print "Your name is Bob\n";
} elseif ($Name = = "Sally") {
print "Your name is Sally\n";
} else {
print "I don't know your name!\n";
}
?>
switch/case. In a switch/case block, you specify what you are checking against, then give a list of possible values you want to handle. Using switch/case statements, we can rewrite the previous script like this:
<?php
$Name = 'Bob';
switch($Name) {
case "Jim":
print "Your name is Jim\n";
break;
case "Linda":
print "Your name is Linda\n";
break;
case "Bob":
print "Your name is Bob\n";
break;
case "Sally":
print "Your name is Sally\n";
break;
default:
print "I don't know your name!\n";
}
?>
Switch/case statements are frequently used to check all sorts of data, and they take up much less room than equivalent if statements.switch/case statement code. First, there is no word "case" before "default"—that is just how the language works. Second, each