Web Design & Development

[ Web Design & Development Topics ]

Writing PHP

PHP Tips and Tricks:

PHP Sample script:

Lets start out by looking at a sample PHP script.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Our first PHP3.0 script</title>
</head>
<body>
<CENTER>Our first PHP3.0 script</CENTER>

<?
/* the above "<?" signals that the PHP script has begun */
$today = date("Y-m-d");

PRINT "<CENTER>Today is: $today.</CENTER>";

/* the following "?>" signals that the PHP script has ended */
?>

</body>
</html>

However, note that if you look at the source code from a Web browser you will only see the results and not the PHP segment from above. You can try this sample PHP script here.

Lets try our hand and modifying the layout of date fields:

Conditional statements

Conditional statements, as mentioned in prior sections, is the performance of different actions based on specific conditions. PHP has two types of conditional statements:

  1. if..else statements (if true do one thing, if not do something else)
  2. switch statements (choose from many blocks of code what to execute)

Lets try a sample switch statement so you can see how it works. Here is the code:

<html xmlns="http://www.w3.org/1999/xhtml">
<body><?php
$x=2;
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>

Try this sample PHP script here.

Looping

As noted previously, looping is the ability to re-execute the same block of code multiple times until a stop condition is met. PHP has the following looping statements: while, do...while, for, and foreach. So lets try a very simple while loop that prints from 1 to 5 as we have done in Javascript before:

<html xmlns="http://www.w3.org/1999/xhtml">
<body><?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>

Try this sample PHP script here.

Functions

Remember, a function is a named piece of programming that performs a specific task (like a procedure or a routine). In PHP there are over 700 functions available for your use. Obviously we won't go over all of these. Lets try on though just for kicks:

<html xmlns="http://www.w3.org/1999/xhtml">
<body><?php
echo "Referer: " . $_SERVER["HTTP_REFERER"] . "<br />";
echo "Browser: " . $_SERVER["HTTP_USER_AGENT"] . "<br />";
echo "User's IP address: " . $_SERVER["REMOTE_ADDR"];
?></body>
</html>

Try this sample PHP script here.

Forms

First we need a form to work with. Lets make one like this:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<form method="post" name="f" action="testing.php">
First Name: <input type="text" name="firstname" />
Last Name: <input type="text" name="lastname" />
<input type="submit" value="Submit">
</form>
</body>
</html>

Call this something like testform.html.

Next we need to build the PHP on the back end, which you will notice I called testing.php above. This means we need to make another file using our text editor and call it testing.php. In your text editor cut and paste the following:

<?php
{
echo "thank you".$_POST["firstname"]."<P>";
echo $_POST["lastname"];
}
?>

Now call this file testing.php and put it in the same directory as where your form is. Now what happens when you try and submit the form? You should get a thank you page.

Just a note, we will be working with the "post" method of sending forms, but I wanted to let you know there are two methods for sending information via forms, get and post:

Non-Web References

Spainhour, S. & Eckstein, R.  (1999)  Webmaster in a Nutshell.  O'Reilly and Associates, Inc.  Sebastopol, CA.