By Rasmus Lerdorf
Book Price: $9.95 USD
£6.95 GBP
PDF Price: $7.99
Cover | Table of Contents
tar zxvf foo.tar.gz
AddType application/x-httpd-php .php
tar zxvf foo.tar.gz
AddType application/x-httpd-php .php
LoadModule php4_module modules/libphp4.so
AddModule lines. These really
aren't necessary. They are only needed if you have a
ClearModuleList directive somewhere in your
httpd.conf file. I would suggest simply deleting
the ClearModuleList directive and deleting all
your AddModule lines. The idea behind
ClearModuleList/AddModule is to
make it possible to reorder already loaded modules in case module
order is an issue. With most modules, the order that they are
loaded -- which governs the order they are called -- is not
important. And further, most binary distributions of Apache ship with
most modules compiled as dynamically loadable modules, which means
that if order is an issue for some reason, you can simply change the
order of the <html><head><title><?echo $title?></title> </head>...
<?echo $title?> portion of the document
is replaced by the contents of the $title PHP
variable. echo is a basic language statement that
you can use to output data.
<? and
?> tags:
<? echo "Hello World"; ?>
short_open_tag directive. Another way to embed PHP
code is within <?php and
?> tags:
<?php echo "Hello World"; ?>
<script> tags is another style that is
always available:
<script language="php" > echo "Hello World"; </script>
<% and %> tags, is
disabled by default:
<% echo "Hello World"; %>
asp_tags
directive in your php.ini file. The style is
most useful when you are using Microsoft FrontPage or another HTML
authoring tool that prefers that tag style for HTML-embedded scripts.
<?php echo "Hello World"; echo "A second statement"; ?>
<br
/> tags for some reason, you can do it this way:
<?php for($i=0; $i<100; $i++) { ?>
<br />
<?php } ?>
str_repeat( ) function here
would make more sense.
$A and $a are two distinct
variables. However, function names in PHP are not case-sensitive.
This rule applies to both built-in functions and user-defined
functions.
/* C style comments */ // C++ style comments # Bourne shell style comments
$). The $ is followed by an
alphabetic character or an underscore, and optionally followed by a
sequence of alphanumeric characters and underscores. There is no
limit on the length of a variable name. Variable names in PHP are
case-sensitive. Here are some examples:
$i $counter $first_name $_TMP
$var = "hello";
$var variable. You can do that like
this:
$$var = "World";
$$var by first dereferencing the
innermost variable, meaning that $var becomes
"hello". The expression that's
left is $"hello", which is just
$hello. In other words, we have just created a new
variable named hello and assigned it the value
"World". You can nest dynamic variables to an
infinite level in PHP, although once you get beyond two levels, it
can be very confusing for someone who is trying to read your code.
echo "Hello ${$var}";
$$var[1] is
ambiguous because it is impossible for PHP to know which level to
apply the array index to. ${$var[1]} tells PHP to
dereference the inner level first and apply the array index to the
result before dereferencing the outer level.
${$var}[1], on the other hand, tells PHP to apply
the index to the outer level.
long data type in
C. On 32-bit platforms, integer values range from -2,147,483,648 to
+2,147,483,647. PHP automatically converts larger values to floating
point numbers if you happen to overflow the range. An integer can be
expressed in decimal (base-10), hexadecimal (base-16), or octal
(base-8). For example:
$decimal=16; $hex=0x10; $octal=020;
double type in C. On most platforms, a double can
be between 1.7E-308 to 1.7E+308. A double may be expressed either as
a regular number with a decimal point or in scientific notation. For
example:
$var=0.017; $var=17.0E-3
http://www.php.net/bc and http://www.php.net/gmp for more information.
'PHP is cool' "Hello, World!"
$a="World"; echo "Hello\t$a\n";
$a and the escape sequences are converted
to their corresponding characters. Contrast that with:
echo 'Hello\t$a\n';
$foo = <<<EOD This is a "multiline" string assigned using the 'heredoc' syntax. EOD;
5 5+5 $a $a==5 sqrt(9)
echo statement
we've used in numerous examples cannot be part of a
complex expression because it does not have a return value. The
print statement, on the other hand, can be used as
part of complex expression -- it does have a return value. In all
other respects, echo and print
are identical: they output data.
|
Operators
|
A
|
|---|---|
!, ~, ++,
--, @, (the casting operators)
|
Right
|
*, /, % |
Left
|
+, -, . |
Left
|
<<, >> |
Left
|
<, <=,
>=, >
|
Nonassociative
|
==, !=, ===,
!==
|
Nonassociative
|
& |
Left
|
^ |
Left
|
| |
Left
|
&& |
Left
|
|| |
Left
|
? : (conditional operator) |
Left
|
=, +=, -=,
*=, /=, %=,
^=, .=,
&=, |=,
<<=, >>=
|
Left
|
AND |
Left
|
XOR |
Left
|
OR |
Left
|
if statement is a standard conditional found
in most languages. Here are the two syntaxes for the
if statement:
if(expr) { if(expr): statements statements } elseif(expr) { elseif(expr): statements statements } else { else: statements statements } endif;
if statement causes particular code to be
executed if the expression it acts on is true.
With the first form, you can omit the braces if you only need to
execute a single statement.
switch statement can be used in place of a
lengthy if statement. Here are the two syntaxes
for switch:
switch(expr) { switch(expr): case expr: case expr: statements statements break; break; default: default: statements statements break; break; } endswitch;
case statement is compared
against the switch expression and, if they match,
the code following that particular case is executed. The
break keyword signals the end of a particular
case; it may be omitted, which causes control to flow into the next
case. If none of the case expressions match the
switch expression, the default
case is executed.
while statement is a looping construct that
repeatedly executes some code while a particular expression is
function keyword. For example:
function soundcheck($a, $b, $c) {
return "Testing, $a, $b, $c";
}
Fatal error: Can't redeclare already declared function in filename on line N
echo soundcheck(4, 5, 6);
soundcheck() function
optional:
function soundcheck($a=1, $b=2, $c=3) {
return "Testing, $a, $b, $c";
}
function triple($x) {
$x=$x*3;
return $x;
}
$var=10;
$triplevar=triple($var);
$var evaluates to 10 when
triple() is called, so $x is
set to 10 inside the function. When $x is tripled,
that change does not affect the value of $var
outside the function.
register_globals setting in your
php.ini file.
register_globals is off. With
register_globals off, all the various variables
that are usually available directly in the global symbol table are
now available via individual superglobal arrays. There is a limited
set of superglobals and they cannot be created from a user-level
script. The superglobal array to use depends on the source of the
variable. Here is the list:
$_GET$_GET['a'] and $_GET['b'] are
set to 1 and 2, respectively.
$_POST$_COOKIE$_REQUEST$_REQUEST is given by
the setting of the variables_order
php.ini directive. The default is
'GPC', which means GET-method variables are
imported first, then POST-method variables (overriding any GET-method
variables of the same name), and finally cookie variables (overriding
the other two).
$_SERVERDOCUMENT_ROOT, REMOTE_ADDR,
REMOTE_PORT, SERVER_NAME,
SERVER_PORT, and many others. To get a full list,
have a look at your phpinfo( ) output, or run a
script like the following to have a look:
<?php
foreach($_SERVER as $key=>$val) {
echo '$_SERVER['.$key."] = $val<br>\n";
}
?>
$_ENVsession.use_cookieson, PHP will try to use cookiessession.use_trans_sidtrans_sid code in PHP is rather interesting.
It actually parses the entire HTML file and modifies/mangles every
link and form to add the session ID. The
url_rewriter.tags php.ini
directive can change how the various elements are mangled.
session_start( ), then register the
variables you wish to associate with that session. For example:
<?php
session_start( );
session_register('foo');
session_register('bar');
$foo = "Hello";
$bar = "World";
?>
$foo and
$bar on the server. If you then load this
page2.php page:
<?php session_start( ); echo "foo = $_SESSION[foo]<br />"; echo "bar = $_SESSION[bar]<br />"; ?>
$foo and
$bar set in page1.php. Note
the use of the $_SESSION superglobal. If you have
register_globals on, you would be able to access
these as $foo and $bar
directly.
session_start( ).
<html><head><title>PHP Example</title></head>
<body>
You are using
<?php echo $_SERVER['HTTP_USER_AGENT'] ?>
<br />
and coming from
<?php echo $_SERVER['REMOTE_ADDR'] ?>
</body></html>
You are using Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1b) Gecko/20020722 and coming from 127.0.0.1
<html><head><title>Form Example</title></head>
<body>
<h1>Form Example</h1>
<?
function show_form($first="", $last="",
$interest="") {
$options = array("Sports", "Business", "Travel",
"Shopping", "Computers");
if(!is_array($interest)) $interest = array( );
?>
<form action="form.php" method="POST">
First Name:
<input type="text" name="first"
value="<?echo $first?>">
<br />
Last Name:
<input type="text" name="last"
value="<?echo $last?>">
<br />
Interests:
<select multiple name="interest[ ]">
<?php
foreach($options as $option) {
echo "<option";
if(in_array($option, $interest)) {
echo " selected ";
}
echo "> $option</option>\n";
}
?>
</select><br />
<input type=submit>
</form>
<?php } // end of show_form( ) function
if($_SERVER['REQUEST_METHOD']!='POST') {
show_form( );
} else {
if(empty($_POST['first']) ||
empty($_POST['last']) ||
empty($_POST['interest'])) {
echo "<p>You did not fill in all the fields,";
echo "please try again</p>\n";
show_form($_POST['first'],$_POST['last'],
$_POST['interest']);
}
else {
echo "<p>Thank you, $_POST[first] $_POST[last], you ";
echo 'selected '.
join(' and ', $_POST['interest']);
echo " as your interests.</p>\n";
}
}
?>
</body></html>int, double,
string, array,
void, and mixed.
mixed means that the argument or return type can
be of any type. Optional arguments are shown in square brackets. Note
that PHP didn't start tracking version numbers for
functions until PHP 3.0, so functions that are listed as 3.0 are
likely to have existed in Version 2.x.
Return to PHP Pocket Reference