<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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',
    2 => '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',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'chapters/tutorial.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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 class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     <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.
    </p>
   </p></blockquote>
   <p class="para">
    To display this variable, you can simply do:
   </p>
   <p class="para">
    <div class="example" id="example-1">
     <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 /><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 /><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>
Mozilla/5.0 (Linux) Firefox/112.0
</pre></div>
    </div>
   </div>
   </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 element
    from an <a href="language.types.array.php" class="link">Array</a> variable.
    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 <span class="function"><a href="function.phpinfo.php" 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 Firefox you
    can do this:
   </p>
   <p class="para">
    <div class="example" id="example-2">
     <div class="info"><p><strong>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></strong></p></div>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">str_contains</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">'Firefox'</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #DD0000">'You are using Firefox.'</span><span style="color: #007700">;<br />}<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 Firefox.
</pre></div>
     </div>
    </div>
   </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 <span class="function"><a href="function.str-contains.php" class="function">str_contains()</a></span>
    function call. <span class="function"><a href="function.str-contains.php" class="function">str_contains()</a></span> is a function built into
    PHP which determines if a given string contains another string. In this case we are
    looking for <code class="literal">&#039;Firefox&#039;</code> (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 true. Otherwise, it
    returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. If it returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, the <a href="control-structures.if.php" class="link">if</a> expression evaluates to <strong><code><a href="reserved.constants.php#constant.true">true</a></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.php" class="link">if</a>,
    <a href="control-structures.else.php" class="link">else</a>, and other
    functions such as <span class="function"><a href="function.strtoupper.php" class="function">strtoupper()</a></span> and
    <span class="function"><a href="function.strlen.php" 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.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">
    <div class="example" id="example-3">
     <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 (</span><span style="color: #0000BB">str_contains</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">'Firefox'</span><span style="color: #007700">)) {<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;h3&gt;str_contains() returned true&lt;/h3&gt;<br />&lt;p&gt;You are using Firefox&lt;/p&gt;<br /><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">} else {<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;h3&gt;str_contains() returned false&lt;/h3&gt;<br />&lt;p&gt;You are not using Firefox&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;str_contains() returned true&lt;/h3&gt;
&lt;p&gt;You are using Firefox&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.str-contains.php" class="function">str_contains()</a></span>.  In other words, it depends on
    whether the string <code class="literal">Firefox</code> was found or not.
   </p>
  </div><?php manual_footer($setup); ?>