PHP/Data Type/isset

Материал из Web эксперт
Перейти к: навигация, поиск

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("&middot; %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"] . ".");
}
?>