View Full Version : Undefined variable

02-09-2010, 07:15 AM

It must Be very Simple . but i do not know how to do it ..

i use this ..


$dec = $_SESSION['description'];
$com = $_SESSION['comment'];

echo "$dec";
echo "<br>";
echo "$com";


Now in my site i have the debug mode enabled and always shown Undefined variable error.. because i did not define $dec , $com ..

this variable defined when the browser returns after post data ..

So myquastion How to Prevent the error Undefined variable from shown up without turning the debug Mode Off..

02-09-2010, 07:22 AM
It's just as simple as checking to see whether or not the $_SESSION value exists prior to reassignment, e.g.


$dec = ( isset($_SESSION['description']) )? $_SESSION['description'] : '';
$com = ( isset($_SESSION['comment']) )? $_SESSION['comment'] : '';

Note this uses the ternary operator, which is a shortcut if..else statement. It essentially says "if {$var} is set, assign new var to its value. if not, assign empty string (or whatever you want as default)".

02-09-2010, 07:39 AM
Thank u .and thx for the tip