Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 12462

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Something Useful</title>

 </head>
 <body><div class="manualnavbar" 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"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['HTTP_USER_AGENT']</a></var></var>.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     <var class="varname"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var></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">&raquo;&nbsp;4.1.0</a>.  Before this time, we used
     the older <var class="varname"><var class="varname">$HTTP_*_VARS</var></var> arrays instead,
     such as <var class="varname"><var class="varname">$HTTP_SERVER_VARS</var></var>.  Although deprecated, 
     these older variables still exist.  (See also the note on
     <a href="tutorial.oldcode.html" class="link">old code</a>.)
    </p>
   </p></blockquote>
   <p class="para">
    To display this variable, you can simply do:
   </p>
   <p class="para">
    <div class="example" id="example-4">
     <div class="info"><p><strong>Example #1 Printing a variable (Array element)</strong></p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</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">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     A sample output of this script may be:
    </p></div>
    <div class="example-contents screen"><br />
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)<br />
    </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"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var></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  <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> 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" id="example-5">
     <div class="info"><p><strong>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></strong></p></div>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(</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">],&nbsp;</span><span style="color: #DD0000">'MSIE'</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'You&nbsp;are&nbsp;using&nbsp;Internet&nbsp;Explorer.&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>
      A sample output of this script may be:
     </p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
You are using Internet Explorer.&lt;br /&gt;
</pre></div>
     </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  <span class="function"><a href="function.strpos.html" class="function">strpos()</a></span>
    function call.  <span class="function"><a href="function.strpos.html" class="function">strpos()</a></span> is a function built into
    PHP which searches a string for another string. In this case we are
    looking for <em>&#039;MSIE&#039;</em> (so-called needle) inside
    <var class="varname"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['HTTP_USER_AGENT']</a></var></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 <strong><code>FALSE</code></strong>.  If it does not return <strong><code>FALSE</code></strong>, the <a href="control-structures.if.html" class="link">if</a> expression evaluates to <strong><code>TRUE</code></strong>
    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  <span class="function"><a href="function.strtoupper.html" class="function">strtoupper()</a></span> and 
     <span class="function"><a href="function.strlen.html" class="function">strlen()</a></span>.  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" id="example-6">
     <div class="info"><p><strong>Example #3 Mixing both HTML and PHP modes</strong></p></div>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(</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">],&nbsp;</span><span style="color: #DD0000">'MSIE'</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;h3&gt;strpos()&nbsp;must&nbsp;have&nbsp;returned&nbsp;non-false&lt;/h3&gt;<br />&lt;p&gt;You&nbsp;are&nbsp;using&nbsp;Internet&nbsp;Explorer&lt;/p&gt;<br /><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">}&nbsp;else&nbsp;{<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;h3&gt;strpos()&nbsp;must&nbsp;have&nbsp;returned&nbsp;false&lt;/h3&gt;<br />&lt;p&gt;You&nbsp;are&nbsp;not&nbsp;using&nbsp;Internet&nbsp;Explorer&lt;/p&gt;<br /><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>
      A sample output of this script may be:
     </p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
&lt;h3&gt;strpos() must have returned non-false&lt;/h3&gt;
&lt;p&gt;You are using Internet Explorer&lt;/p&gt;
</pre></div>
     </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  <span class="function"><a href="function.strpos.html" class="function">strpos()</a></span>.  In other words, it depends on
    whether the string <em>MSIE</em> was found or not.
   </p>
  </div><hr /><div class="manualnavbar" 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>