downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/tutorial.useful.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/tutorial.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'tutorial.useful.php',
   
1 => 'Something Useful',
  ),
 
'up' =>
  array (
   
0 => 'tutorial.php',
   
1 => 'A simple tutorial',
  ),
 
'prev' =>
  array (
   
0 => 'tutorial.firstpage.php',
   
1 => 'Your first PHP-enabled page',
  ),
 
'next' =>
  array (
   
0 => 'tutorial.forms.php',
   
1 => 'Dealing with Forms',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<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.php" 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.php" class="classname">$_SERVER['HTTP_USER_AGENT']</a></var>.
   </p>
   <blockquote><p><b class="note">Note</b>:
   
     <var class="varname"><a href="reserved.variables.server.php" 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.php" 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; 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.php" class="link">old code</a>.)
    <br />
   </p></blockquote>
   <p class="para">
    To display this variable, you can simply do:
   </p>
   <p class="para">
    </p><div class="example">
     <div class="info"><p><b>Example #1 Printing a variable (Array element)</b></p></div>
    <div class="example-contents programlisting">
<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 para"><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>
   <p class="para">
    There are many <a href="language.types.php" class="link">types</a> of
    variables available in PHP.  In the above example we printed
    an <a href="language.types.array.php" class="link">Array</a> element.
    Arrays can be very useful.
   </p>
   <p class="para">
    <var class="varname"><a href="reserved.variables.server.php" 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.php" 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.php" 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">
    </p><div class="example">
     <div class="info"><p><b>Example #2 Example using <a href="language.control-structures.php" class="link">control
     structures</a> and <a href="language.functions.php" class="link">functions</a></b></p></div>
     <div class="example-contents programlisting">
<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 para"><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>
   <p class="para">
    Here we introduce a couple of new concepts. We have an
    <a href="control-structures.if.php" 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.php" class="link">Language
    Reference</a> part of the manual.
   </p>
   <p class="para">
    The second concept we introduced was the <a href="function.strpos.php" class="function">strpos()</a>
    function call. <a href="function.strpos.php" 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>&#039;MSIE&#039;</i> (so-called needle) inside
    <var class="varname"><a href="reserved.variables.server.php" 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 class="constant">FALSE</tt></b>.  If it does not return <b><tt class="constant">FALSE</tt></b>, the <a href="control-structures.if.php" class="link">if</a> expression evaluates to <b><tt class="constant">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.php" class="link">if</a>,
    <a href="control-structures.else.php" class="link">else</a>, and other
    functions such as <a href="function.strtoupper.php" class="function">strtoupper()</a> and
    <a href="function.strlen.php" 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.php" class="link">how to read a
    function definition</a> and the section about 
    <a href="language.functions.php" 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">
    </p><div class="example">
     <div class="info"><p><b>Example #3 Mixing both HTML and PHP modes</b></p></div>
     <div class="example-contents programlisting">
<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 para"><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>
   <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.php" class="function">strpos()</a>.  In other words, it depends on
    whether the string <i>MSIE</i> was found or not.
   </p>
  </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites