...

View Full Version : Undefined variable



ajloun
02-09-2010, 07:15 AM
Hello

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

i use this ..


<?php
session_start();

$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..

bdl
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.


<?php
session_start();

$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)".

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum