By Kevin Tatroe, Rasmus Lerdorf, Peter MacIntyre
Book Price: $39.99 USD
£28.50 GBP
PDF Price: $31.99
Cover | Table of Contents
http://gtk.php.net), you can write full-blown, cross-platform GUI applications in PHP.http://gtk.php.net), you can write full-blown, cross-platform GUI applications in PHP.comp.infosystems.www.authoring.cgi in June 1995:
From: rasmus@io.org (Rasmus Lerdorf)
Subject: Announce: Personal Home Page Tools (PHP Tools)
Date: 1995/06/08
Message-ID: <3r7pgp$aa1@ionews.io.org>#1/1
organization: none
newsgroups: comp.infosystems.www.authoring.cgi
Announcing the Personal Home Page Tools (PHP Tools) version 1.0.
These tools are a set of small tight cgi binaries written in C.
They perform a number of functions including:
. Logging accesses to your pages in your own private log files
. Real-time viewing of log information
. Providing a nice interface to this log information
. Displaying last access information right on your pages
. Full daily and total access counters
. Banning access to users based on their domain
. Password protecting pages based on users' domains
. Tracking accesses ** based on users' e-mail addresses **
. Tracking referring URL's - HTTP_REFERER support
. Performing server-side includes without needing server support for it
. Ability to not log accesses from certain domains (ie. your own)
. Easily create and display forms
. Ability to use form information in following documents
Here is what you don't need to use these tools:
. You do not need root access - install in your ~/public_html dir
. You do not need server-side includes enabled in your server
. You do not need access to Perl or Tcl or any other script interpreter
. You do not need access to the httpd log files
The only requirement for these tools to work is that you have
the ability to execute your own cgi programs. Ask your system
administrator if you are not sure what this means.
The tools also allow you to implement a guestbook or any other
form that needs to write information and display it to users
later in about 2 minutes.
The tools are in the public domain distributed under the GNU
Public License. Yes, that means they are free!
For a complete demonstration of these tools, point your browser
at: http://www.io.org/~rasmus
--
Rasmus Lerdorf
rasmus@io.org
http://www.io.org/~rasmus
http://ca3.php.net/manual/en/install.unix.php. You will have to replace the xxx signifier in the following steps with the version of the software that you choose to install.gunzip apache_
xxx
.tar.gz
tar -xvf apache_
xxx
.tar
gunzip php-
xxx
.tar.gz
tar -xvf php-
xxx
.tar
cd apache_
xxx
/configure --prefix=/www --enable-module=so
<html>
<head>
<title>Look Out World</title>
</head>
<body>
<?php echo 'Hello, world!' ?>
</body>
</html>
echo command produces output (the string "Hello, world!" in this case), which is inserted into the HTML file. In this example, the PHP code is placed between the <?php and ?> tags. There are other ways to tag your PHP code—see Chapter 2 for a full description.phpinfo( ) creates an HTML page full of information on how PHP was installed. You can use it to see whether you have particular extensions installed, or whether the php.ini file has been customized. Example 1-2 is a complete page that displays the phpinfo( ) page.<?php phpinfo( ); ?>
echo, while, class, etc., are case-insensitive. Thus, these three lines are equivalent:
echo("hello, world");
ECHO("hello, world");
EcHo("hello, world");
$name, $NAME, and $NaME are three different variables.if test:
echo "Hello, world";
myfunc(42, "O'Reilly");
$a = 1;
$name = "Elphaba";
$b = $a / 25.0;
if ($a == $b) { echo "Rhyme? And Reason?"; }
if ($needed) {
echo "We must have it!"; // semicolon required here
} // no semicolon required here after the brace
echo, while, class, etc., are case-insensitive. Thus, these three lines are equivalent:
echo("hello, world");
ECHO("hello, world");
EcHo("hello, world");
$name, $NAME, and $NaME are three different variables.if test:
echo "Hello, world";
myfunc(42, "O'Reilly");
$a = 1;
$name = "Elphaba";
$b = $a / 25.0;
if ($a == $b) { echo "Rhyme? And Reason?"; }
if ($needed) {
echo "We must have it!"; // semicolon required here
} // no semicolon required here after the brace
<?php
if ($a == $b) { echo "Rhyme? And Reason?"; }
echo "Hello, world" // no semicolon required before closing tag
?>
+) or minus (−) sign. If there is no sign, positive is assumed. Examples of decimal integers include the following:
1998
-641
+33
0755 // decimal 493
+010 // decimal 8
0xFF // decimal 255
0x10 // decimal 16
-0xDAD1 // decimal -56017
$). For example:
$name
$Age
$_debugging
$MAXIMUM_IMPACT
$what = "Fred";
$what = 35;
$what = array('Fred', '35', 'Wilma');
$day = 60 * 60 * 24;
echo "There are $day seconds in a day.\n";
There are 86400 seconds in a day.
NULL value:
if ($uninitialized_variable === NULL) {
echo "Yes!";
}
Yes!
$foo = 'bar';
$$foo = 'baz';
$bar has the value "baz".$black an alias for the variable $white, use:
$black =& $white;
$black is lost. Instead, $black is now another name for the value that is stored in $white:
$big_long_variable_name = "PHP";
$short =& $big_long_variable_name;
$big_long_variable_name .= " rocks!";
print "\$short is $short\n";
print "Long is $big_long_variable_name\n";
$short is PHP rocks!
Long is PHP rocks!
$short = "Programming $short";
print "\$short is $short\n";
print "Long is $big_long_variable_name\n";
$short is Programming PHP rocks!
Long is Programming PHP rocks!
+ and - familiar to us from math. Some operators modify their operands, while most do not.|
P
|
A
|
Operator
|
Operation
|
|---|---|---|---|
|
19
|
N
|
new
|
Create new object
|
|
18
|
[
|
Array subscript
| |
|
17
|
R
|
!
|
Logical NOT
|
if/else and switch, allow a program to execute different pieces of code, or none at all, depending on some condition. Loops, such as while and for, support the repeated execution of particular segments of code.if statement checks the truthfulness of an expression and, if the expression is true, evaluates a statement. An if statement looks like:
if (expression)
statement
else keyword:
if (expression)
statement
else
statement
if ($user_validated)
echo "Welcome!";
else
echo "Access Forbidden!";
if statement, use a block—a curly brace-enclosed set of statements:
if ($user_validated) {
echo 'Welcome!";
$greeted = 1;
} else {
echo "Access Forbidden!";
exit;
}
if line with a colon (:) and use a specific keyword to end the block (endif, in this case). For example:
if ($user_validated) :
echo "Welcome!";
$greeted = 1;
else :
echo "Access Forbidden!";
exit;
endif;
<?if($user_validated):?>
<table>
<tr>
<td>First Name:</td><td>Sophia</td>
</tr>
<tr>
<td>Last Name:</td><td>Lee</td>
</tr>
</table>
<?else:?>
Please log in.
<?endif?>
require and include
. They both load a file as the PHP script runs, work in conditionals and loops, and complain if the file being loaded cannot be found. The main difference is that attempting to require a nonexistent file is a fatal error, while attempting to include such a file produces a warning but does not stop script execution.include is to separate page-specific content from general site design. Common elements such as headers and footers go in separate HTML files, and each page then looks like:
<? include 'header.html'; ?>
content
<? include 'footer.html'; ?>
include because it allows PHP to continue to process the page even if there's an error in the site design file(s). The require construct is less forgiving and is more suited to loading code libraries, where the page cannot be displayed if the libraries do not load. For example:
require 'codelib.inc';
mysub( ); // defined in codelib.inc
<? require 'design.inc';
header( );
?>
content
<? footer( ); ?>
include or require, a warning is printed and execution continues. You can silence the warning by prepending the call with the silence operator (@) — for example, @include.allow_url_fopen option is enabled through PHP's configuration file, php.ini, you can include files from a remote site by providing a URL instead of a simple local path:
include 'http://www.example.com/codelib.inc';
include and require can be arbitrarily named. Common extensions are <script> tag; this makes it easy to edit pages with enabled PHP using a regular HTML editor.<?php and ?>. Everything between these markers is interpreted as PHP, and everything outside the markers is not. Although it is not necessary to include spaces between the markers and the enclosed text, doing so improves readability. For example, to get PHP to print "Hello, world", you can insert the following line in a web page:
<?php echo "Hello, world"; ?>
<!doctype html public "-//w3c//DTD XHTML 1.0 Transitional//EN"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>This is my first PHP program!</title>
</head>
<body>
<p>
Look, ma! It's my first PHP program:<br />
<?php echo "Hello, world"; ?><br />
How cool is that?
</p>
</body>
</html>
$some_value = function_name( [ parameter, ... ] );
// strlen( ) is a built-in function that returns the length of a string
$length = strlen("PHP"); // $length is now 3
// sin( ) and asin( ) are the sine and arcsine math functions
$result = sin(asin(1)); // $result is the sine of arcsin(1), or 1.0
// unlink( ) deletes a file
$result = unlink("functions.txt"); // false if unsuccessful
"PHP", to the function strlen( ), which gives us the number of characters in the string it's given. In this case, it returns
$some_value = function_name( [ parameter, ... ] );
// strlen( ) is a built-in function that returns the length of a string
$length = strlen("PHP"); // $length is now 3
// sin( ) and asin( ) are the sine and arcsine math functions
$result = sin(asin(1)); // $result is the sine of arcsin(1), or 1.0
// unlink( ) deletes a file
$result = unlink("functions.txt"); // false if unsuccessful
"PHP", to the function strlen( ), which gives us the number of characters in the string it's given. In this case, it returns 3, which is assigned to the variable $length. This is the simplest and most common way to use a function.asin(1) to the sin( ) function. Since the sine and arcsine functions are reflexive, taking the sine of the arcsine of any value will always return that same value. Here we see that a function can be called within another function and the returned value of the inner call is subsequently sent to the outer function before the overall result is returned and stored in the $result variable.unlink( ) function, which attempts to delete the file. Like many functions, it returns false when it fails. This allows you to use another built-in function, die( ), and the short-circuiting property of the logic operators. Thus, this example might be rewritten as:
function [&] function_name ( [ parameter [, ... ] ] )
{
statement list
}
column( ) function simply gives a convenient short name to HTML code that may be needed many times throughout the page:
<? function column( ) { ?>
</td><td>
<? } ?>
sin( ) function as sin(1), SIN(1), SiN(1), and so on, because all these names refer to the same function.return statement: put return
expr inside your function. When a return statement is encountered during execution, control reverts to the calling statement, and the evaluated results of expr will be returned as the value of the function. Although it can make for messy code, you can actually include multiple return statements in a function if it makes sense (for example, if you have a switch statement to determine which of several values to return).
function strcat($left, $right) {
$combined_string = $left . $right;
return $combined_string;
}
$left and $right. Using the concatenation operator, the function creates a combined string in the variable
$a = 3;
function foo( ) {
$a += 2;
}
foo( );
echo $a;
$a inside the function foo( ) is a different variable than the variable $a outside the variable; even though foo( ) uses the add-and-assign operator, the value of the outer $a remains 3 throughout the life of the page. Inside the function, $a has the value 2.$a will be 5 by the time the echo statement is reached, so keep that in mind when choosing names for your variables.global keyword. Its syntax is:
global var1, var2, ...
global keyword, we get:
$a = 3;
function foo( ) {
global $a;
$a += 2;
}
foo( );
echo $a;
$a with function-level scope, PHP uses the global &). Example 3-5 revisits our doubler( ) function with a slight change.
function doubler(&$value) {
$value = $value << 1;
}
$a = 3;
doubler($a);
echo $a;
$value parameter is passed by reference, the actual value of $a, rather than a copy of that value, is modified by the function. Before, we had to return the doubled value, but now we change the caller's variable to be the doubled value.$a into doubler( ) by reference, the value of $a is at the mercy of the function. In this case, doubler( ) assigns a new value to it.<?= doubler(7); ?> in the previous example, it would issue an error. However, you may assign a default
value to parameters passed by reference (in the same manner as you provide default values for parameters passed by value).
function return_one( ) {
return 42;
}
function return_two ( ) {
return array("Fred", 35);
}
& before its name returns a reference (alias) to its return value:
$names = array("Fred", "Barney", "Wilma", "Betty");
function & find_one($n) {
global $names;
return $names[$n];
}
$person =& find_one(1); // Barney
$person = "Barnetta"; // changes $names[1]
find_one( ) function returns an alias for $names[1], instead of a copy of its value. Because we assign by reference, $person is an alias for $names[1], and the second assignment changes the value in $names[1].
switch($which) {
case 'first':
first( );
break;
case 'second':
second( );
break;
case 'third':
third( );
break;
}
$which( ); // if $which is "first," the function first( ) is called, etc...
function_exists( ) to determine whether a function exists for the value of the variable before calling the function:
$yes_or_no = function_exists(function_name);
if(function_exists($which)) {
$which( ); // if $which is "first," the function first( ) is called, etc...
}
echo( ) and isset( ) cannot be called through variable functions:
$f = 'echo';
$f('hello, world'); // does not work
usort( ) function uses a function you create and pass to it as a parameter to determine the sort order of the items in an array.create_function( ). This function takes two parameters—the first describes the parameters the anonymous function takes in, and the second is the actual code. A randomly generated name for the function is returned:
$func_name = create_function(args_string, code_string);
usort( ).
$lambda = create_function('$a,$b', 'return(strlen($a) - strlen($b));');
$array = array('really long string here, boy', 'this', 'middling length', 'larger');
usort($array, $lambda);
print_r($array);
usort( ), using the anonymous function, in order of string length.
$who = 'Kilroy';
$where = 'here';
echo "$who was $where";
Kilroy was here
$who = 'Kilroy';
$where = 'here';
echo "$who was $where";
Kilroy was here
$n = 12;
echo "You are the {$n}th person";
You are the 12th person
$nth variable.
$bar = 'this is not printed';
$foo = '$bar'; // single quotes
print("$foo");
$bar
echo construct lets you print many values at once, while print( ) prints only one value. The printf( ) function builds a formatted string by inserting values into a template. The print_r( ) function is useful for debugging—it prints the contents of arrays, objects, and other things, in a more-or-less human-readable form.echo. While it looks—and for the most part behaves—like a function, echo is a language construct. This means that you can omit the parentheses, so the following are equivalent: