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

search for in the

Práce s formuláři> <Vaše první stránka v PHP
Last updated: Sat, 24 Mar 2007

view this page in

Něco užitečného

Nyní udělejme něco ještě užitečnějšího. Budeme zjišťovat, jaký druh prohlížeče návštěvník používá. K tomuto účelu použijeme řetězec identifikace uživatelského agenta (User-Agent), který prohlížeč posílá jako součást HTTP požadavku. Tato informace je uložena v proměnné. Proměnné v PHP vždy začínají znakem dolaru. Proměnná, která nás právě teď zajímá, je $_SERVER['HTTP_USER_AGENT'].

Poznámka: $_SERVER je speciální vyhrazená proměnná PHP, která obsahuje všechny informace z webového serveru. Je známá též jako autoglobální (nebo superglobální) proměnná. Pro více informací viz související manuálovou stránku o superglobálních proměnných. Tyto speciální proměnné byly přidány v PHP » 4.1.0. Předtím jsme namísto nich používali starší pole $HTTP_*_VARS, jako např. $HTTP_SERVER_VARS. Přestože jsou již zavržené, tyto starší proměnné stále existují. (Viz také poznámku o starém kódu.)

Pro zobrazení této proměnné jednoduše napište:

Příklad 2.3. Tisk proměnné (elementu pole)

<?php echo $_SERVER['HTTP_USER_AGENT']; ?>

Ukázkový výstup z tohoto skriptu může vypadat takto:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    

V PHP existuje mnoho typů proměnných. Ve výše uvedeném příkladu jsme vytiskli element pole. Pole jsou velice užitečná.

$_SERVER je jen jedna z proměnných, které vám PHP automaticky zpřístupňuje. Seznam proměnných najdete v manuálové sekci Vyhrazené proměnné, úplný seznam pak ve výstupu funkce phpinfo() použité v příkladu v předchozí sekci.

Mezi PHP značky můžete vložit více PHP příkazů a vytvářet tak malé bloky kódu, které dělají víc než jen jednoduché echo. Pokud chcete například zjistit, zda uživatel používá Internet Explorer, můžete udělat toto:

Příklad 2.4. Example using control structures and functions

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
    echo
'You are using Internet Explorer.<br />';
}
?>

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. You can find a list of PHP books at » http://www.php.net/books.php.

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 'MSIE' (so-called needle) inside $_SERVER['HTTP_USER_AGENT'] (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 FALSE. If it does not return FALSE, the if expression evaluates to TRUE and the code within its {braces} is executed. Otherwise, the code is not run. Feel free to create similar examples, with if, else, and other functions such as strtoupper() and strlen(). 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 how to read a function definition and the section about PHP functions.

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říklad 2.5. Mixing both HTML and PHP modes

<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
<h3>strpos() must have returned non-false</h3>
<p>You are using Internet Explorer</p>
<?php
} else {
?>
<h3>strpos() must have returned false</h3>
<p>You are not using Internet Explorer</p>
<?php
}
?>

A sample output of this script may be:


<h3>strpos() must have returned non-false</h3>
<p>You are using Internet Explorer</p>

     

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 MSIE was found or not.



Práce s formuláři> <Vaše první stránka v PHP
Last updated: Sat, 24 Mar 2007
 
add a note add a note User Contributed Notes
Něco užitečného
rok
17-Jun-2008 01:25
Seperating logic and presentation should be introduced at start. I'm just learning PHP but have background in Perl, C and C++ and find mixed html/code extremly complex to maintain. It would be very nice if this beginners tutorial already presented on how to seperate code/html. Now to read up on that smarty ;).
sako
22-Apr-2008 02:36
I don't think there is a bad time to start separating logic from presentation. It is actually a good idea to start doing this from the get-go so it becomes a habit. This is a great habit.. Everything now a days is going away from mixing logic and presentation for simplicity sake among other things (look at html and css). I think a good thing to read up on is smarty. It can do wonders separating your logic from presentation.
ducky
03-Apr-2008 03:54
To the above poster:

That's probably too much to think about when you're starting out... You should probably at first just concentrate on getting the stuff running and learning syntax before you consider best practices and the like.
rfantin at coralwood dot com
20-Dec-2006 10:00
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.

Práce s formuláři> <Vaše první stránka v PHP
Last updated: Sat, 24 Mar 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites