Cover | Table of Contents
http://www.startribune.com is an example of a static site, except that you do have to register to view articles. http://www.amazon.com is an example of a dynamic web site, because your ordering data is logged, and Amazon offers recommendations based on your purchasing history when you access their page. In other words, dynamic means that the user interacts more with the web site, beyond just reading pages, and the web site responds accordingly.
<?php
// A function to delete a user from the site_user table based on
//the $user_name parameter.
// An open database connection is assumed
function remove_user($user_name){
// Remove a User
// This is the SQL command
$sql_delete = "DELETE FROM `site_user` WHERE `User`='$user'";
$success = mysql_query($sql_delete) or die(mysql_error());
// print the page header
print('
<html>
<head>
<title>Remove User</title>
<link rel="stylesheet" type="text/css" href="user_admin.css" />
</head>
<body>
<div class="user_admin">');
// Check to see if the deletion was sucessful
if ($success){
// Tell the user it was sucessful
print("The account for $user_name was deleted successfully.");
}
else {
// Tell the user it was not sucessful
print("User $user could not be deleted. Please try again later.");
}
// Print the page footer
print('</div></body></html>');
}
?>
http://www.phone.com/directory.html would request the page directory.html from www.phone.com.< >) used for?http://www.oreilly.com/ in your browser, this sends a request to the server whose domain name is oreilly.com. The server fetches the page named index.html and sends it to your browser.http://www.oreilly.com/ in your browser, this sends a request to the server whose domain name is oreilly.com. The server fetches the page named index.html and sends it to your browser.http://httpd.apache.or/download.cgi. The file that you save to your desktop is called apache2_0.55-win32-x86-no_ssl.msi.http://sourceforge.net/projects/filezilla/, is one FTP client you can use to upload files to your ISP. Your initial login screen looks similar to Figure 2-25. Fetch is a good FTP program for the Macintosh.
http://sourceforge.net/projects/devphp/).
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>I sure wish I had something to say.</p>
</body>
</html>
http://sourceforge.net/projects/devphp/).
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>I sure wish I had something to say.</p>
</body>
</html>
echo command to output some text in Example 3-2.
<html>
<head>
<title>Hello World</title>
</head>
<body>
echo("<p>Now I have something to say.</p>");
</body>
</html>
$variable_name = value;
$) must always fill the first space of your variable. The first character after the dollar sign must be a letter or underscore. It can't under any circumstances be a number; otherwise, your code will not execute, so watch those typos!a-z, A-Z, 0-9, and _.$variable_name and $Variable_Name are different.$test_variable.=).PHP_SELF?strcmp do?TRUE or FALSE. For example, the expression 10 > 5 (10 is greater than 5) is a Boolean expression because the result is TRUE. All expressions that contain relational operators, such as the less-than sign (<), are Boolean. The Boolean operators are AND, OR, XOR, NOR, and NOT.|
Example
|
|---|
TRUE or FALSE. For example, the expression 10 > 5 (10 is greater than 5) is a Boolean expression because the result is TRUE. All expressions that contain relational operators, such as the less-than sign (<), are Boolean. The Boolean operators are AND, OR, XOR, NOR, and NOT.|
Example
|
Type
|
|---|---|
|
1
|
A numeric value literal
|
|
"Becker Furniture"
|
A string literal
|
|
TRUE
|
http://www.zend.com/manual/language.operators.php. There are some operators we're not going to discuss in order for you to get up and running with PHP as quickly as possible. These include some of the casting operators that we'll just skim the surface of, for now. When working with operators, there are four aspects that are critical:-) that multiplies a numeric value by -1. The auto-increment and -decrement operators described in Chapter 3 are also unary operators.
if statement, which we'll talk about later when discussing conditionals, takes three operands.if
switch
? : : (shorthand for an if statement)switch statement. The switch statement is useful when you need to take different action based on the contents of a variable that may be set to one of a list of values.if statement offers the ability to execute a block of code, if the supplied condition is TRUE; otherwise, the code block doesn't execute. The type of condition can be any expression, including tests for nonzero, null, equality, variables, and returned values from functions.{}) is executed. If not, PHP ignores it and moves to the second condition and continues through as many clauses as you write until PHP hits an else, then it automatically executes that block.if statement works. The else block always needs to come last and be treated as if it's the default action. This is similar to the semicolon (;), which acts as the end of a sentence. Common true conditions are:$var, if $var has a value other than the empty set (0), an empty string, or NULL
isset ($var), if $var has any value other than NULL, including the empty set or an empty stringFALSE, you will need to use looping. Each time the code in the loop executes, it is called an iteration. It's useful for many common tasks such as displaying the results of a query by looping through the returned rows. PHP provides the while,
for, and while . . . do constructs to perform loops.
if statement. Second, the code to perform is also required and specified either on a single line or within curly braces. A logical error would be to omit the code in the loop, causing an infinite
loop.TRUE. To avoid an infinite loop, which would loop forever, your code should affect the expressions so that it becomes FALSE. When this happens, the loop stops, and execution continues with the next line of code, following the logical loop.while loop takes the expression followed by the code to execute. Figure 4-3 illustrates how a while loop processes.
while loop is:while (expression) { code to execute; }
<?php
$num = 1;
while ($num <= 10){
print "Number is $num<br />\n";
$num++;
}
print 'Done.';
?>
Number is 1 Number is 2 Number is 3 Number is 4 Number is 5 Number is 6 Number is 7 Number is 8 Number is 9 Number is 10 Done.
$num is set to 1. This is called initializing a counter variable. Each time the code block executes, it increases the value in +) sign?&&) instead of one (&), will you get an error?isset() do?switch statement that adds, subtracts, multiplies, or divides x using the action variable.break keyword do?print_r is a function that prints readable information about a variable in plain English rather than code.print_r function. If given an array, values are shown as keys and elements. A similar format is used for objects. With the advent of PHP 5.0, print_r and var_export show protected and private properties of objects.aggregate_info to imap_ping through pdf_open_image. Since there are so many, we can only cover some basics in this chapter, but we'll give you enough information that you'll be using functions like a pro in no time at all. You can search http://www.php.net for an exhaustive list of functions.phpinfo. It returns configuration and technical information about your PHP installation.
<?php
phpinfo();
?>
(, the parameters, and then a closing parenthesis ), followed by a semicolon (;). It would look like this: function_name
(
parameters
);. Function names aren't case sensitive, so calling
phpinfo is the same as calling PhpInfo. As shown in Example 5-3, this is what calling a function looks like: md5($mystring);.md5 function. md5 is a one-way hash function used to verify the integrity of a string, similar to a checksum. md5 converts a message into a fixed string of digits, called a message digest. You can then perform a hashcheck, comparing the calculated message digest against a message digest decrypted with a public key to verify that the message was not tampered with. Example 5-3 creates a 128-bit long md5 signature of the string "mystring".function statement:function some_function([arguments]) { code to execute }
[ ]) mean optional. The code could also be written with optional_arguments in place of [
arguments
]. The function keyword is followed by the name of the function. Function names abide by the same rules as other named objects such as variables in PHP. A pair of parentheses must come next. If your function has parameters, they're specified within the parentheses. Finally, the code to execute is listed between curly brackets, as seen in the code above.<?php function hi() { echo ("Hello from function-land!"); } //Call the function hi(); ?>
Hello from function-land!
hi function doesn't take any parameters, so you don't list anything between the parentheses. Now that you've defined a simple function, let's mix in some parameters.strtolower, which converts your string "Hello world!" to lowercase. It takes a parameter of the type string, which is a data type (described in a previous chapter). There's also another function
Cat that has three methods: meow, eat, and purr. The class construct defines a class. It takes the name of the class immediately after it. Class names follow the same naming rules as variables and functions. The code that makes up the class is placed between curly brackets. This example creates the Cat class without defining any methods or variables.
<?php
// define a function
function Response {
echo "Have a good day!<br /><br />";
}
// driving to work
echo "Are you going to merge? <br />";
Response;
// at the office
echo "I need a status report on all your projects in the next 10 minutes for
my management meeting.<br />";
Response;
// at the pub after work
echo "Did Bill get everything he needed today? He was sure crabby!<br />";
Response;
?>
toast that takes minutes as a parameter. The function prints "done."toast function with 5 as a parameter.include() and require()?