Home > Articles > Programming > PHP

  • Print
  • + Share This
From the author of

Step 5: Doing more complicated things with PHP

Clearly, PHP allows for far more complex tasks than printing "Hello World!" Because PHP is a programming language, you can use it to do things such as storing parameters and URL processing.

Storing parameters

Listing 3 illustrates a simple use of a variable in PHP to determine the type of browser employed by the user. This is achieved by checking the user agent string that the browser sends as part of an HTTP request. The required information is stored in a variable; the format for specifying variables is to use a dollar sign.

So for the browser data, we use the following variable specification: $_SERVER[’HTTP_USER_AGENT’].

Listing 2 Using a Variable

<html>
 <head>
 <title>PHP Test 2</title>
 </head>
 <body>
 <?php
 echo $_SERVER[’HTTP_USER_AGENT’];
?>
 </body>
</html>

Executing the code in Listing 2 is pretty easy. Just create an appropriately named file in the www folder, click the EasyPHP icon, select the Local Web option, and then select the file. You should see something like Figure 6.

Figure 6

Figure 6 The browser in use

I mentioned earlier that you can mix and match HTML and PHP. An example of this is illustrated in Listing 3.

Listing 3 Mixing and Matching HTML and PHP Code

<html>
 <head>
 <title>PHP Test</title>
 </head>
 <body>
<?php
if (strpos($_SERVER[’HTTP_USER_AGENT’], ’MSIE’) !== FALSE) {
?>
<h3>strpos() must have returned non-false</h3>
<p>You are using Internet Explorer</p>
<?php
} else {
?>
<h3>strpos() must have returned false</h3>
<p>You are not using Internet Explorer</p>
<?php
}
?>
 </body>
</html>

Executing the code in Listing 3 results in the output shown in Figure 7.

Figure 7

Figure 7 HTML and PHP code

The code in Listing 3 simply searches the returned string looking for the token ’MSIE’. If the token is found, we know that the browser is none other than Microsoft Internet Explorer.

That's the thing about scripting languages—they often look a lot more complex than they actually are. The key to understanding is to break them down.

URL processing

URL processing is a mechanism by which a client program communicates with a remote PHP-enabled Web server. Some organizations use PHP in this way as a means of enforcing security for Web-based software tools. Here's the way it works: user X opens a browser and logs onto a Web page. A number of details (such as username and password) are passed as part of the URL. The receiving server then verifies the data and returns an appropriate HTML page. Used in this way, the HTTP protocol becomes a client-server enabler.

This is obviously a more complex use of PHP. Programs that operate in this way typically have the structure illustrated in Listing 4.

Listing 4 Complex PHP Code Structure

<?php

//start session
session_start();
//more PHP code here

//Function call
function callgetInfo($f,$devId,$succUrl)
{
return $response;
}
?>

In Listing 4, notice the PHP start and end tags that we've seen earlier. These tags delimit the PHP code. Included inside them are straight PHP code as well as function calls. As you start to do more sophisticated things with PHP, it's likely you'll use function calls as shown here.

You now have enough information to help you delve more deeply into PHP. The EasyPHP tool makes code testing very straightforward. Of course, if you want to post some PHP code to a real website, you'll have to ensure that the Web server in question is PHP-enabled.

To learn more about PHP, there are online tutorials on the topic.

  • + Share This
  • 🔖 Save To Your Account