PHP: Hypertext Preprocessor, a server-side scripting language used to create dynamically generated Web pages
The PHP parser recognizes a few different types of PHP start and end tags and will attempt to execute anything between these tags, returning a highly visible error if it cant. The three main sets of start and end tags recognized by the parser are as follows.
Opening Tag | Closing Tag |
---|---|
<?php | ?> |
<? | ?> |
<script language="php"> | </script> |
The instruction terminator is the semicolon ( ; ) and is absolutely required at the end of commands or the PHP parser will return error messages for browsers to display, so dont forget it.
End all statements with ; (a semicolon, the instruction terminator).
The parse error created by forgetting a semicolon will often be reported as being on the line after the line that should have a semicolon.
The escape character is a backslash ( \ ) and it tells the PHP parser to just print and not read the character that follows the escape character.
It allows you to include quotation marks within the quotation marks that tell the PHP parser to use that which is contained within the quotation marks as a string. For example:
echo "This technique gets a check \"mark\" for including quotation marks.
";
Outputs as the HTML code:
This technique gets a check "mark" for including quotation marks.
PHP uses three kinds of comments that are ignored by the PHP parser:
(You can also make comments in HTML code
which you might want to do to announce or describe PHP code before it starts.)
The basic function of an operator is to do something with the value of a variable: assign a value, change a value, or compare two or more values.
Operator | Example | Action |
---|---|---|
= | $a = 1; | Assigns the value to the variable. |
+= | $a += 3; | Changes the value of a variable to the current value plus the value on the right side. |
-= | $a -= 3; | Changes the value of a variable to the current value minus the value on the right side. |
.= | $a .= "string"; | Concatenates (adds on to) the value on the right side with the current value. |
Operator | Example | Action |
---|---|---|
+ | $b = $a + 3; | Adds values |
- | $b = $a - 3; | Subtracts values |
* | $b = $a * 3; | Multiplies values |
/ | $b = $a / 3; | Divides values |
% | $b = $a % 3; | Returns the remainder (modulus) |
Operator | Example | Action |
---|---|---|
== | if/while $a == $b { } | Equal to |
!= | if/while $a != $b { } | Not equal to |
> | if/while $a > $b { } | Greater than |
< | if/while $a < $b { } | Less than |
>= | if/while $a >= $b { } | Greater than or equal to |
<= | if/while $a <= $b { } | Less than or equal to |
Expressions are enclosed in parentheses ( ).
Blocks of statements are grouped into separate elements within a control structure by curly braces { }.
When a Web browser makes a request of a Web server, it sends along with the request a list of extra variables called environment variables. The phpinfo()
function displays information about your Web server, version of PHP, and basic HTTP environment.
The REMOTE_ADDR
environment variable (&REMOTE_ADDR
) contains the IP address of the machine making the request. You can also assign it through the getenv()
function, that is &example = getenv("REMOTE_ADDR");
.
HTTP_USER_AGENT
returns the browser type, version, language encoding, and platform. For example: Mozilla/4.61 - (Win98; I)
or Lynx/2.8rel.3 libwww-FM/2.14
.
Check for the value of $func
before any other action occurs. This must be the first block of PHP code. HTTP headers of any kind must be sent to the browser before anything else, including white space, line breaks, and any characters. If a rogue user directly accesses the script without using the form, he will be redirected to the form, and no errors will occur.
<? if ($func == "") { header ("Location: http://localhost/generic_form.html"); exit; } $result = $funct($text1); ?>
Mode | Usage |
---|---|
r | Opens an existing file and reads data from it. The file pointer is placed at the beginning of the file. |
r+ | Opens an existing file for reading or writing. The file pointer is placed at the beginning of the file. |
w | Opens a file for writing. If a file with that name does not exist, the function creates a new file. If the file exists, the function deletes all existing contents and places the file pointer at the beginning of the file. |
w+ | Opens a file for reading and writing. If a file with that name does not exist, the function creates a new file. If the file exists, the function deletes all existing contents and places the file pointer at the beginning of the file. |
a | Opens a file for writing. If a file with that name does not exist, the function creates a new file. If the file exists, the function places the file pointer at the end of the file. |
a+ | Opens a file for reading and writing. If a file with that name does not exist, the function attempts to create a new file. If the file exists, the function places the file pointer at the end of the file. |
Example of use:
$newfile = fopen($filename, "w+")
To check whether a file already exists, use file_exists(). For example:
if (file_exists($filename)) { . . . } else { . . . }
Call to undefined function means you are trying to use a function that does not exist (not that the variable is incorrect).
Adapted from PHP fast&easy web development by Julie C. Meloni. All right, I hardly adapted at all. I stole the information here. Buy the book or an earlier or later one by the author, its worth it. There is a related web site: thickbook.com.
Meloni, Julie C. PHP fast&easy web development. Roseville, California: Prima Publishing, 2000.
Put functions you will use repeatedly in include files. Sensitive information is also better kept outside the document tree, in an include file. Tell PHP where to look by modifying the include_path =
statement in the PHP initialization file.
switch
/case
and define
define (INITIAL_PAGE, 0);
define (SOLICIT_EVENT, 1);
define (ADD_EVENT, 2);
define (DISPLAY_SCORES, 3);
[ . . . . ]
if (empty ($action))
$action = INITIAL_PAGE;
switch ($action) # what are we supposed to do?
{
case INITIAL_PAGE: # present initial page with user-defined function
display_events ();
break;
case SOLICIT_EVENT: # ask for new event information
solicit_event_info ();
break;
case ADD_EVENT: # add new event to database
add_ne_event ();
display_events ();
break;
[. . .]
default:
die ("Unknown action code ($action)");
}
function display_entry()
{
global $PHP_SELF;
global $member_id, $password;
$member_id = trim($member_id);
if (empty($member_id))
die("No member ID specified");
if (!ereg("^[0-9]+$", $member_id))
die("Invalid meber ID specified (must be number)");
[ . . . . ]
mysql_pconnect() establishes a persistent connection which stays open even when a script terminates, and is reused if the same function with the same arguments is called again, much more efficient than establishing each connection from scratch, as mysql_connect() does.
All the information in this section is stolen from a great book I kept out of the library long enough for the library to threaten legal action:
Paul DuBois, MySQL (Indianapolis, Indiana: New Riders, 2000).
Created on ... September 09, 2001
Last added to on ... September 20, 2001