|
Something Useful
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 variable. Variables always start
with a dollar-sign in PHP. The variable we are interested in right now
is
Note:
$_SERVER is a
special reserved PHP variable that contains all web server information.
It is known as a superglobal. See the related manual page on
superglobals
for more information. These special variables were introduced in PHP
» 4.1.0. Before this time, we used
the older To display this variable, you can simply do: Example 2.3. Printing a variable (Array element)<?php A sample output of this script may be: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) There are many types of variables available in PHP. In the above example we printed an Array element. Arrays can be very useful.
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: Example 2.4. Example using control structures and functions<?php A sample output of this script may be: You are using Internet Explorer.<br /> Here we introduce a couple of new concepts. We have an if 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 Language Reference part of the manual.
The second concept we introduced was the strpos()
function call. strpos() is a function built into
PHP which searches a string for another string. In this case we are
looking for 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: Example 2.5. Mixing both HTML and PHP modes<?php A sample output of this script may be: <h3>strpos() must have returned non-false</h3>
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 strpos(). In other words, it depends on
whether the string Code Examples / Notes » tutorial.usefulrfantin
While it's easy to get carried away mixing your logic and presentation together since it's so easy to do, your better off using PHP within HTML only to fill in values, or include other source files. Keep your actual processing in separate libraries that are called before you send any headers to the page. Try to avoid calling a script that retrieves or sets information, or manipulates it in the middle of your HTML. You'll find it's much easier to maintain. dexxter
What "rfantin" described in his/her post is commonly known as the MVC pattern. Might want to check out the following link for details http://en.wikipedia.org/wiki/Model-view-controller
anonymous
tento text prekladal asi absolutny magor alebo automat, ved to je hrozne...
john dot t dot gold
<html> <head> <title>A List of All Those Vaiables Variables you get</title> </head> <body> <table border=1> <tr><td align=center>Array Name</td><td align=center>Array Contents</td></tr> <?php foreach ($GLOBALS as $k => $v) { if (is_array($v)) { echo "<tr><td>$".$k."</td><td><table>\n"; if (count($v)>0) { echo "<tr><td><b>Index Syntax</b></td><td> </td><td><b>Current Value</b></td></tr>"; } foreach ($v as $key => $value) { echo "<tr><td>['".$key."']</td><td>=</td><td>".$value."</td></tr>\n"; } echo "</table></td></tr>\n"; } ELSE { echo "<tr><td>$".$k."</td><td>".$v."</td></tr>\n"; } } ?> </table> </body> </html> |