📄 tutorial.useful.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Something Useful</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="tutorial.firstpage.html">Your first PHP-enabled page</a></div> <div class="next" style="text-align: right; float: right;"><a href="tutorial.forms.html">Dealing with Forms</a></div> <div class="up"><a href="tutorial.html">A simple tutorial</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="tutorial.useful" class="section"> <div class="info"><h1 class="title">Something Useful</h1></div> <p class="para"> Let us do something more useful now. We are going to check what sort of browser the visitor is using. For that, we check the user agent string the browser sends as part of the HTTP request. This information is stored in a <a href="language.variables.html" class="link">variable</a>. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['HTTP_USER_AGENT']</a></var>. </p> <blockquote><p><b class="note">Note</b>: <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var> is a special reserved PHP variable that contains all web server information. It is known as a superglobal. See the related manual page on <a href="language.variables.superglobals.html" class="link">superglobals</a> for more information. These special variables were introduced in PHP <a href="http://www.php.net/releases/4_1_0.php" class="link external">» 4.1.0</a>. Before this time, we used the older <var class="varname">$HTTP_*_VARS</var> arrays instead, such as <var class="varname">$HTTP_SERVER_VARS</var>. Although deprecated, these older variables still exist. (See also the note on <a href="tutorial.oldcode.html" class="link">old code</a>.) <br /> </p></blockquote> <p class="para"> To display this variable, you can simply do: </p> <p class="para"> <div class="example"> <div class="info"><p><b>Example #1 Printing a variable (Array element)</b></p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> A sample output of this script may be: </p></div> <div class="example-contents"><pre>Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) </pre></div> </div> </p> <p class="para"> There are many <a href="language.types.html" class="link">types</a> of variables available in PHP. In the above example we printed an <a href="language.types.array.html" class="link">Array</a> element. Arrays can be very useful. </p> <p class="para"> <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var> is just one variable that PHP automatically makes available to you. A list can be seen in the <a href="reserved.variables.html" class="link">Reserved Variables</a> section of the manual or you can get a complete list of them by looking at the output of the <a href="function.phpinfo.html" class="function">phpinfo()</a> function used in the example in the previous section. </p> <p class="para"> You can put multiple PHP statements inside a PHP tag and create little blocks of code that do more than just a single echo. For example, if you want to check for Internet Explorer you can do this: </p> <p class="para"> <div class="example"> <div class="info"><p><b>Example #2 Example using <a href="language.control-structures.html" class="link">control structures</a> and <a href="language.functions.html" class="link">functions</a></b></p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">], </span><span style="color: #DD0000">'MSIE'</span><span style="color: #007700">) !== </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'You are using Internet Explorer.<br />'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> A sample output of this script may be: </p></div> <div class="example-contents"><pre><div class="cdata"><pre>You are using Internet Explorer.<br /></pre></div> </pre></div> </div> </p> <p class="para"> Here we introduce a couple of new concepts. We have an <a href="control-structures.if.html" class="link">if</a> statement. If you are familiar with the basic syntax used by the C language, this should look logical to you. Otherwise, you should probably pick up an introductory PHP book and read the first couple of chapters, or read the <a href="langref.html" class="link">Language Reference</a> part of the manual. </p> <p class="para"> The second concept we introduced was the <a href="function.strpos.html" class="function">strpos()</a> function call. <a href="function.strpos.html" class="function">strpos()</a> is a function built into PHP which searches a string for another string. In this case we are looking for <i>'MSIE'</i> (so-called needle) inside <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['HTTP_USER_AGENT']</a></var> (so-called haystack). If the needle is found inside the haystack, the function returns the position of the needle relative to the start of the haystack. Otherwise, it returns <b><tt>FALSE</tt></b>. If it does not return <b><tt>FALSE</tt></b>, the <a href="control-structures.if.html" class="link">if</a> expression evaluates to <b><tt>TRUE</tt></b> and the code within its {braces} is executed. Otherwise, the code is not run. Feel free to create similar examples, with <a href="control-structures.if.html" class="link">if</a>, <a href="control-structures.else.html" class="link">else</a>, and other functions such as <a href="function.strtoupper.html" class="function">strtoupper()</a> and <a href="function.strlen.html" class="function">strlen()</a>. Each related manual page contains examples too. If you are unsure how to use functions, you will want to read both the manual page on <a href="about.prototypes.html" class="link">how to read a function definition</a> and the section about <a href="language.functions.html" class="link">PHP functions</a>. </p> <p class="para"> We can take this a step further and show how you can jump in and out of PHP mode even in the middle of a PHP block: </p> <p class="para"> <div class="example"> <div class="info"><p><b>Example #3 Mixing both HTML and PHP modes</b></p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">], </span><span style="color: #DD0000">'MSIE'</span><span style="color: #007700">) !== </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">) {<br /></span><span style="color: #0000BB">?><br /></span><h3>strpos() must have returned non-false</h3><br /><p>You are using Internet Explorer</p><br /><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">} else {<br /></span><span style="color: #0000BB">?><br /></span><h3>strpos() must have returned false</h3><br /><p>You are not using Internet Explorer</p><br /><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> A sample output of this script may be: </p></div> <div class="example-contents"><pre><div class="cdata"><pre><h3>strpos() must have returned non-false</h3><p>You are using Internet Explorer</p></pre></div> </pre></div> </div> </p> <p class="para"> Instead of using a PHP echo statement to output something, we jumped out of PHP mode and just sent straight HTML. The important and powerful point to note here is that the logical flow of the script remains intact. Only one of the HTML blocks will end up getting sent to the viewer depending on the result of <a href="function.strpos.html" class="function">strpos()</a>. In other words, it depends on whether the string <i>MSIE</i> was found or not. </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="tutorial.firstpage.html">Your first PHP-enabled page</a></div> <div class="next" style="text-align: right; float: right;"><a href="tutorial.forms.html">Dealing with Forms</a></div> <div class="up"><a href="tutorial.html">A simple tutorial</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -