PHP/Data Type/isset

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

bool isset ( mixed var [, mixed var [, ...]] ) returns true if its parameter has been set

   <source lang="html4strict">

<? isset($a); ?>

 </source>
   
  


Checking PHP"s global authentication variables

   <source lang="html4strict">

<? 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; ?>

 </source>
   
  


Checking Whether a Variable Is Set: isset( )

   <source lang="html4strict">

<?

   $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";
   }

?>

 </source>
   
  


Using the isset() function to see whether an array element is defined for a given index.

   <source lang="html4strict">

<?php $array = array("a" => "R", "b" => 2, c => "2");

print "

Using for:

\n

";

$limit = count($array); for($i = 0; $i < $limit; $i++)

   if( isset($array[$i]) ) 
       printf("· %s,", $array[$i]); 

?>

 </source>
   
  


Viewing the username cookie

   <source lang="html4strict">

<?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"] . ".");

} ?>

</source>