« LOOPing for Data - Part I | Main | A Little String FUN »
November 3, 2005
PHP 101
Today Dale and I worked on a beginning PHP tutorial. We believe that our applications will be more robust if we learn to code up some PHP. Ron has gotten PHP enabled on AERO, so we were all set to go.
The first thing I noticed was how easy it was to get a simple page up. I literally saved a notepad file with a .php extension, and that's all it took.
Next we learned a bit about super-global variables in PHP. The $_SERVER super-global returns quite a bit of information about the browser being used. This code:
_?php echo $_SERVER['HTTP_USER_AGENT']; ?>
returns this kind of information:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.0.3705; .NET CLR 1.1.4322)
You can then look through this output for the string 'MSIE' to determine whether a person is using Internet Explorer. The code looks like this:
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
echo 'You are using Internet Explorer.
';
}
?>
and outputs the phrase after the echo above if you are using IE.
The next concept we covered was that you can put HTML tags in with the PHP tags, and they will be interpreted correctly. You do not have to switch between PHP and HTML mode. This is not true of Javascript, HTML and PL/SQL. Very neat!
Next, we learned a bit about function calls in PHP. There are a number of built in functions (the strpos() call used above is a good example) and they are called pretty much like any other language.
The coolest thing we covered today was the implementation of a simple form like this (all <'s replaced with _'s to display):
_form action="action.php" method="post">
_p>Your name: _input type="text" name="name" />_/p>
_p>Your age: _input type="text" name="age" />_/p>
_p>_input type="submit" />_/p>
_/form>
The form looks like this:
We then created the action.php file, which looks like this:
Hi _?php echo $_POST['name']; ?>.
You are _?php echo $_POST['age']; ?> years old.
And gave us output like this:
Hi Mike. You are 34 years old.
Note that no variables are created at all. The $_POST built-in from PHP handles it. You just have $_POST display the value from the form text box. I thought the "submit query" button that is displayed is kind of like magic, where did it come from? It must also be built-in to PHP.
Unfortunately, we ran out of time here. My first imPHPressions are that PHP will simplify some aspects of our applications for us, and will be a powerful new tool to use to make our apps more robust. I thought we were able to cover a lot of territory pretty simply, and am anxious to expand my knowledge of PHP.
By the way, we were following a beginning tutorial from the official PHP site at: http://us2.php.net
Posted by rossm at November 3, 2005 11:14 AM
Comments
FYI: You can use entities for the less-than (<) and greater-than (>) symbols to display HTML tags in a block of preformatted text. This prevents the HTML tags from being interpreted as genuine tags but renders the symbols within the preformatted text block.
Posted by: Summer at November 7, 2005 3:00 PM
Post a comment
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)