Home  >  Article  >  Backend Development  >  Is it appropriate to use php for scripting?

Is it appropriate to use php for scripting?

(*-*)浩
(*-*)浩Original
2019-09-09 15:14:223217browse

php comes with many functions commonly used in web applications. For example, escaping html, url, etc. It will be much more convenient to process this data.

Is it appropriate to use php for scripting?

Now let’s write some more practical scripts, such as checking what browser the visitor to the page is using. To achieve 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'].

Note:(Recommended learning: PHP programming from entry to proficiency)

$_SERVER is a special PHP reserved variable, which contains All information provided by the web server are called superglobal variables. See "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 no longer used, they still exist in newer versions.

To display the variable, simply do the following:

Print a variable (array element)

<?php 
echo $_SERVER[&#39;HTTP_USER_AGENT&#39;]; 
?>

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 for a list of these variables, or you can check 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:

Use of process control and functions

<?php
if (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;MSIE&#39;) !== FALSE) {
    echo &#39;正在使用 Internet Explorer。<br />&#39;;
}
?>

The output of this script may be:

正在使用 Internet Explorer。<br />

An if statement is used above. If the user is familiar with the basic syntax of C language, he should be familiar with this. Otherwise, he may need to pick up any introductory PHP book and read the first two or three chapters, or he may also read the "Language" of the official manual. Reference" chapter.

The above is the detailed content of Is it appropriate to use php for scripting?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn