Home > Article > Backend Development > Practical php scripting
Now let’s write some more practical scripts, such as checking what browser the visitor browsing the page is using. 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, variables always start with a dollar sign. The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT'] .
PS:$_SERVER is a special PHP reserved variable, which contains all the information provided by the web server and is called super global variable. Please consult "Superglobal Variables" in this manual for more information. These special variables were introduced in PHP » 4.1.0 version. Before this use the $HTTP_*_VARS array, such as $HTTP_SERVER_VARS. Although now deprecated, they still exist in newer versions (see the note 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 this script may be:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
PHP has many different types of variables. In the above example we printed the cells of an array. Arrays are a very useful type of variable.
$_SERVER is just one of the variables automatically globalized by PHP. You can check the "Predefined Variables" section to view a list of these variables, or you can view it through the output of the phpinfo() function in the example in the previous section.
You can add multiple PHP statements to a PHP identifier, and you can also create a code block to do more than a simple echo. For example, if you need to identify Internet Explorer, you can do the following:
Example #2 Process controlUsage of functions
<?php if ( strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE' ) !== FALSE ) { echo '正在使用 Internet Explorer。<br />' ; } ?>
This script The output may be:
正在使用 Internet Explorer。<br />
Some new principles are introduced here. An if statement is used above. If the user is familiar with the basic syntax of C language, you should be familiar with this, otherwise, you may need to pick up any introductory PHP book and read the first two or three chapters, or you can also read the "Language" section of this manual Reference" chapter.
The second principle that needs to be introduced is the call to the strpos() function. strpos() is a built-in function of PHP. Its function is to search for another string in a string. For example, we now need to find 'MSIE' in the $_SERVER['HTTP_USER_AGENT'] (the so-called haystack) variable. If the string (the so-called needle) is found in this haystack ("needle hunting"), the function returns the position of the needle in the haystack relative to the beginning; if not, it returns FALSE. If the function does not return FALSE , then if evaluates the condition to TRUE and runs the code within its curly braces {}; otherwise, the code is not run. You can try to create similar scripts by yourself using if, else and other functions such as strtoupper() and strlen(). The relevant pages in this manual also contain examples. If you are not sure how to use a function, you can read the relevant chapters in the manual about "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 Mixing HTML and PHP mode
<?php if ( strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE' ) !== FALSE ) { ?> <h3>strpos() 肯定没有返回假 (FALSE)</h3> <p>正在使用 Internet Explorer</p> <?php } else { ?> <h3>strpos() 肯定返回假 (FALSE)</h3> <center><b>没有使用 Internet Explorer</b></center> <?php } ?>
Output of this script It may be:
<h3>strpos() 肯定没有返回假 (FALSE)</h3> <p>正在使用 Internet Explorer</p>
The difference from the above where we use a PHP echo statement to output is that we jump out of PHP mode and write HTML code directly. It's important to note here that the logical efficiency of the script is the same for both cases. After determining whether the return value of the strpos() function is TRUE or FALSE, that is, determining whether the string 'MSIE' is found, only one HTML block is finally sent to the viewer.
The above is the detailed content of Practical php scripting. For more information, please follow other related articles on the PHP Chinese website!