Checking set and unset variable
isset :
isset : is set return true or false for weather if a varible is defined or not
<?php
$x=10;
var_dump(isset($x));
?>
unset :
unset : same reverse of isset it destroys the defined varible or key.
<?php
echo "<hr/>";
$b='ravi';
echo "<hr/>";
echo $b;
echo "<hr/>";
var_dump(isset($b));
echo "<hr/>";
unset($b); // destroy
echo $b; // undefined Index $b : Notice Error
echo "<hr/>";
var_dump(isset($b));
?>
How to set default value to a undefined varible
we are going to use ternary Operator for such logic
General Syntax:
---------------
<expression>?[true]:[false];
<?php
$x=5;
($x%2==0)?print("even"):print("odd");
//it is short version of if-else
?>
if value is unset then undefined index error will raise.
we need to handle that
there are two possible ways :
1. suppress Operator @
2. using Ternary to handle unset varible
1. Suppress Operator : (@) is used to hide Notice or Warning Error
2. Ternary Operator: can be to assign a default value if they not defined
Special Care must be taken for Get Varible
0 Comments