HAL-PC Scheduled Classes TWiki > Classes > IntroPHP1 > IntroPHP2 > IntroPHP3 TWiki webs:
AIX| Classes | HALBOD | HLUG | Helpdesk | Library | Main | Samba | TWiki |
Classes . { TWikiGuest | Index | Changes | Search | Go }
Houston Area League of PC Users Education System <BODY BGCOLOR="#e2e3ed" TEXT="#07070a" LINK="#777bb4" ALINK="#777bb4" VLINK="#777bb4">
php logoPHP stands for PHP: Hypertext Preprocessor.
http://www.php.net
You might want to be able to see what was entered in the name field before you applied all of your string manipulations to the data.
Let's add the line: echo "<br>You entered: ".$name2."<br><br>";
<?php
$name2=$HTTP_POST_VARS["name2"];
?>

<html>
<body>


<?php
if($name2!="")
{
$newsplit="";
$name2=trim($name2);
$name2=strtolower($name2);
$newsplit = explode(" ", $name2);
$name2="";
$to_do=count($newsplit)+1;
for($y=0; $y < $to_do; $y++)
{
$newsplit[$y]=ucfirst($newsplit[$y]);
$name2=$name2." ".$newsplit[$y];
}
$name2=trim($name2);
$name2 = ereg_replace('[^._ 0-9a-zA-Z]', '', $name2);
echo "<br>You entered: ".$name2."<br><br>";
echo "<br>Our records show your name is: ".$name2."<br><br>";
} else {
echo "<br>Please enter your name.<br><br>";
}
?>

<form name=see_name action=name.php method=post>
Name:
<input type="text" name="name2">
</form>

</body>
</html>

This won't work because we apply all of our data manipulation on $name2 before we display it.

There are a few things we can do.

Let's add the line: $name_entered=$HTTP_POST_VARS["name2"];

and then change the line: echo "<br>You entered: ".$name2."<br><br>";

to read: echo "<br>You entered: ".$name_entered."<br><br>";

Your script now looks like this:

<?php
$name_entered=$HTTP_POST_VARS["name2"];
$name2=$HTTP_POST_VARS["name2"];
?>

<html>
<body>

<?php
if($name2!="")
{
$newsplit="";
$name2=trim($name2);
$name2=strtolower($name2);
$newsplit = explode(" ", $name2);
$name2="";
$to_do=count($newsplit)+1;
for($y=0; $y < $to_do; $y++)
{
$newsplit[$y]=ucfirst($newsplit[$y]);
$name2=$name2." ".$newsplit[$y];
}
$name2=trim($name2);
$name2 = ereg_replace('[^._ 0-9a-zA-Z]', '', $name2);
echo "<br>You entered: ".$name_entered."<br><br>";
echo "<br>Our records show your name is: ".$name2."<br><br>";
} else {
echo "<br>Please enter your name.<br><br>";
}
?>

<form name=see_name action=name.php method=post>
Name:
<input type="text" name="name2">
</form>

</body>
</html>

It is generally a good programming practice to initialize variables before you use them.

We check to see that $name2 is not equal to a blank string (if($name2!="")), but, we never set $name2 to a blank string.

How do we know what is in that variable?

We don't.

Let's add a third variable, $name3 and set all of the values to a blank string before they are used.

This way we always know that we started with a blank string.

Your code should now look like this:

<?php
$name_entered="";
$name_entered=$HTTP_POST_VARS["name2"];
$name3="";
$name3=$HTTP_POST_VARS["name2"];
?>

<html>
<body>

<?php
if($name3!="")
{
$newsplit="";
$name3=trim($name3);
$name3=strtolower($name3);
$newsplit = explode(" ", $name3);
$name3="";
$to_do=count($newsplit)+1;
for($y=0; $y < $to_do; $y++)
{
$newsplit[$y]=ucfirst($newsplit[$y]);
$name3=$name3." ".$newsplit[$y];
}
$name3=trim($name3);
$name3 = ereg_replace('[^._ 0-9a-zA-Z]', '', $name3);
echo "<br>You entered: ".$name_entered."<br><br>";
$name_entered="";
echo "<br>Our records show your name is: ".$name3."<br><br>";
} else {
echo "<br>Please enter your name.<br><br>";
}
?>

<form name=see_name action=name.php method=post>
Name:
<input type="text" name="name2">
</form>
</body>
</html>

PHP allows us to use c style comments.

We're getting quite a few lines of code here and this may turn into something useful.

How could another programmer know what we were thinking when we wrote this?

Better yet, will we remember what we were thinking?

The truth is, probably not. After you spend so many days writing code earlier days of code are forgotten. We need a way to remember what we were doing with this code.

Let's go through and add some comments that will help us in the future:


<?php

// Initialize variables to a known value:

$name_entered="";
$name3="";
$newsplit="";
$to_do=0;

/*
use the predefined PHP variables $HTTP_POST_VARS to acquire the data that the client entered into their browser:
*/

$name_entered=$HTTP_POST_VARS["name2"];
$name3=$HTTP_POST_VARS["name2"];

// Standard HTML tags are needed for the form:
?>

<html>
<body>

<?php

// Check to see if we got any data:

if($name3!="")
{

/*
Use the PHP predefined function trim() to Strip whitespace from the beginning and end of $name3:
*/

$name3=trim($name3);

// Use the PHP predefined function strtolower() to make $name3 all lowercase:

$name3=strtolower($name3);

/*
Use the PHP predefined function explode() to split $name3 where there are spaces and return each peice of $name3 in an element of an array named $newsplit:
*/

$newsplit = explode(" ", $name3);

/*
$name3 is now stored as elements in the array we named $newsplit. We can set $name3 to our know value, a blank string:
*/

$name3="";

/*
Use the PHP predefined function count to get the number of elements in $newsplit.
Use the addition operator (+) to increase the count by 1.
Assign that number to a new variable called $to_do:
*/

$to_do=count($newsplit)+1;

/*
Use a PHP control structure called a for loop to get each element of our array, $newsplit.

The first expression ($y=0) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, expr2 ($y < $to_do) is evaluated.

If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, expr3 ($y++) is evaluated (executed).

We use the Post-increment Operator $y++. The Post-increment Operator returns $y, then increments $y by one:

*/

for($y=0; $y < $to_do; $y++)
{

/*
Use the PHP predefined function ucfirst() to make the first character of our array element $newsplit[$y] uppercase:
*/

$newsplit[$y]=ucfirst($newsplit[$y]);

/*
Get whatever has been stored in $name3

Use the string concatenation operator (.) to add a blank space and then the contents of $newsplit[$y].

Use the assignment operator (=) to assign this new value to $name3.

Note: on the first time through the loop we add a blank space to the beginning of $newsplit[0]:
*/

$name3=$name3." ".$newsplit[$y];

/*
This is the end of our for loop. If $y is still less than $to_do we will increment $y and repeat the loop:
*/
}

/*
Use the PHP predefined function trim() to strip whitespace from the beginning of $name3:
*/

$name3=trim($name3);

/*
Use the PHP predefined function ereg_replace to replace most special characters with nothing:
*/

$name3 = ereg_replace('[^._ 0-9a-zA-Z]', '', $name3);

/*
Use the PHP language construct echo() to display the literal string "<br>You entered: " and (.) the value of $name_entered and (.) "<br><br>":
*/

echo "<br>You entered: ".$name_entered."<br><br>";

// Use the PHP assignment operator to reset the variable $name_entered to our known value "":

$name_entered="";

/*
Use the PHP language construct echo() to display the literal string "<br>Our records show your name is: " and (.) the value of $name3 and (.) "<br><br>":
*/

echo "<br>Our records show your name is: ".$name3."<br><br>";

/*
This is the ending of our if statement (if($name3!="")).

Nothing inside these curly brackets will execute if $name was equal to "" ($name=="").

We would execute the else part of the statement instead:
*/

} else {
echo "<br>Please enter your name.<br><br>";

// This is the end of the else part of our if statement:

}
// HTML form to get the data follows:
?>

<form name=see_name action=name.php method=post>
Name:
<input type="text" name="name2">
</form>
</body>
</html>

Next    Next!


-- Main.RickArchibald - 17 Sep 2004

Topic IntroPHP3 . { Edit | Attach | 5B%5EA-Za-z%5D">Ref-By | Printable | Diffs | r1.1 | More }
Revision r1.1 - 17 Sep 2004 - 04:03 GMT - RickArchibald
Parents: IntroPHP1 > IntroPHP2
Copyright © 2003-2007 by F. A. Archibald III & the contributing authors