PHP/Data Type/isset — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:03, 26 мая 2010
Содержание
- 1 bool isset ( mixed var [, mixed var [, ...]] ) returns true if its parameter has been set
- 2 Checking PHP"s global authentication variables
- 3 Checking Whether a Variable Is Set: isset( )
- 4 Using the isset() function to see whether an array element is defined for a given index.
- 5 Viewing the username cookie
bool isset ( mixed var [, mixed var [, ...]] ) returns true if its parameter has been set
<?
isset($a);
?>
Checking PHP"s global authentication variables
<?
if ( (! isset ($PHP_AUTH_USER)) || (! isset ($PHP_AUTH_PW)) ):
header("WWW-Authenticate: Basic realm="Secret Family"");
header("HTTP/1.0 401 Unauthorized");
print "Authorization is required.";
exit;
endif;
?>
Checking Whether a Variable Is Set: isset( )
<?
$foo = 1;
if (isset($foo)) {
echo "Foo is set\n";
} else {
echo "Foo is not set\n";
}
if (isset($bar)) {
echo "Bar is set\n";
} else {
echo "Bar is not set\n";
}
?>
Using the isset() function to see whether an array element is defined for a given index.
<?php
$array = array("a" => "R", "b" => 2, c => "2");
print "<p>Using for:</p>\n<p>";
$limit = count($array);
for($i = 0; $i < $limit; $i++)
if( isset($array[$i]) )
printf("· %s,", $array[$i]);
?>
Viewing the username cookie
<?php
setcookie("username","michele");
echo "Cookie created.";
if (!isset($_COOKIE["username"])){
echo ("Oops, the cookie isn"t set!");
}else{
echo ("The stored username is ". $_COOKIE["username"] . ".");
}
?>