Practical PHP Script Writing

Source: Internet
Author: User
Tags php book
Now write some more useful scripts, such as checking what browser the visitor is using on the Browse page. To do this, you need to check the user's agent string, which is part of the HTTP request sent by the browser. This information is stored in a variable. In PHP, a variable always starts with a dollar symbol. The variable we are interested in now is $_server[' http_user_agent '.

PS: $_server is a special PHP reserved variable that contains all the information provided by the Web server and is called a Hyper global variable. Please refer to the "Super Global Variables" section in this manual for more information. These special variables are introduced in the php»4.1.0 version. Before this, use a $HTTP _*_vars array, such as $HTTP _server_vars. Although they are not used now, they still exist in the new version (see the annotations in the "Old Code" section).

To display the variable, simply do the following:

Example #1 Print a variable (array element)

<?php echo  $_server [' http_user_agent '];?>

The output of the script might be:

mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

PHP has a number of different types of variables. In the example above, we have printed an array of cells. An array is a very useful type of variable.

$_server is just one of the variables that PHP automatically globals. You can view the list of these variables in the "Predefined variables" section, or you can see the output of the Phpinfo () function from the example in the previous section.

You can add more than one PHP statement to a PHP tag, or you can create a block of code to do more than a simple echo. For example, if you need to identify Internet Explorer, you can do the following:

Example #2 Process Control and function usage

<?phpif (Strpos ($_server [' http_user_agent '],  ' MSIE ')!==  FALSE) {    echo  ' is using Internet Explorer. <br/> ';}? >

The output of the script might be:

Internet Explorer is in use. <br/>

Here are some new principles to be introduced. The above uses an if statement. If you are familiar with the basic syntax of C, you should be familiar with it, or you may need to pick up any introductory PHP book and read the previous two or three chapters, or read the "Language Reference" chapter of this manual.

The second principle that needs to be introduced is the call to the Strpos () function. Strpos () is a built-in function of PHP that searches for another string in a string. For example we now need to look for ' MSIE ' in $_server[' http_user_agent ' (the so-called haystack) variable. If the string (the so-called needle) is found in this haystack, the function returns the position of the needle in haystack relative to the beginning and, if not, returns FALSE. If the function does not return FALSE, the IF evaluates the condition to TRUE and runs the code within its curly braces {}; otherwise, the code is not run. You can try to use if,else and other functions such as Strtoupper () and strlen () to create a similar script yourself. Examples are also included in the relevant pages in this manual. If you are not sure how to use the function, you can read the chapters in the Manual on "How to read the definition of a function" and "functions".

Below we further show how to get in and out of PHP mode, even in the middle of a PHP code block:

Example #3 mixed HTML and PHP patterns

<?phpif (Strpos ($_server [' http_user_agent '],  ' MSIE ')!==  false) {? >

The output of the script might be:


And above we use a PHP echo statement to output the difference is that we jump out of PHP mode to directly write HTML code. It is worth noting here that the logical efficiency of the script is the same for both cases. After judging whether the return value of the Strpos () function is TRUE or FALSE, that is, if the string ' MSIE ' is determined to be found, only one HTML block will eventually be sent to the browser.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.