...

View Full Version : Undefined variable - stop logging?



babelfish
11-10-2010, 03:42 PM
hi all, ive moved from easywamp to zendserver for php development but im now getting lots of really annoying messages.



<b>Notice</b>: Undefined variable: uniquecreationid in <b>C:\Zendserver\Apache2\htdocs\projman\timesheet.php</b> on line <b>173</b><br />


having to go through every value on a forum and put an if(isset($variable)) { is a total pain - surely the language is intelligent enough to know if a variable isnt set then i havent post/get etc.. and just display nothing. can i stop logging for this? ive been through all my views/list and tidied them up but some forms have loads of fields and i generally just display as :



<input type="text" class="text" id="hours_worked" name="hours_worked" value="<?php echo"$hours_worked"; ?>" />


any way i can sort this out without adding possibly hundreds of lines of code to my apps?

i know i can stop errors displaying but its handy for real errors and otherwise its still logging constantly

babelfish
11-10-2010, 04:04 PM
btw - im happy to have Notice: Undefined index: being displayed as i think that is me not not coding too well, but Notice: Undefined variable: seems to be a bit pointless really. for me, if i havent defined it it is blank!

abduraooft
11-10-2010, 04:07 PM
any way i can sort this out without adding possibly hundreds of lines of code to my apps? Yes, change the error_reporting parameters to hide such notices.

babelfish
11-10-2010, 04:42 PM
ive changed it now to E_ALL & ~E_NOTICE but that now doesnt display 'Notice: Undefined index:', which is quite useful for when ive made a boo boo and done something like:



"<td style='text-align:right'>{$row[stockrack]}</td>\n" .

rather than

"<td style='text-align:right'>{$row['stockrack']}</td>\n" .

Fou-Lu
11-10-2010, 07:31 PM
Undefined variables are worse than undefined offsets and indexes. The latter can be recovered from and cannot be overridden. Variables on the other-hand can be extracted in the event that register_globals is enabled.

In production, yes you shouldn't have error_reporting enabled. But in development, your goal is to get rid of the errors completely. You don't need to check against isset unless its a superglobal offset (which wouldn't trigger an undefined error). You simply initialize them first:


<?php

if ($allowed)
{
echo 'you are allowed';
}
?>

Register globals allow the client to override $allowed and set it to true through get, post and cookie data. You can fix without an isset check by simply:


<?php

$allowed = false;
if ($allowed)
{
echo 'Allowed';
}
?>

I know this isn't a feasible example, but the idea is to simply ensure your variables are predeclared (excluding superglobals, you don't want to overwrite those).

babelfish
11-11-2010, 02:42 PM
ok, but i get an error with:



<input type="text" class="text" id="stockname" name="stockname" value='<?php if($stockname) {echo $stockname;} ?>' />


but not with:



<input type="text" class="text" id="stockname" name="stockname" value='<?php if(isset($stockname)) {echo $stockname;} ?>' />

babelfish
11-11-2010, 02:52 PM
so instead of:


if($trading_name == $row['trading_name']) {
$selcl = "selected";
} else {
$selcl = "selected";
}


i have to do:


if(isset($trading_name)) {
if($trading_name == $row['trading_name']) {
$selcl = "selected";
} else {
$selcl = "selected";
}
} else {
$selcl = "";
}


which seems very longwinded to me.

to be honest this app is all on an intranet and is part of a project management system ive designed, whilst i learnt PHP. maybe i got into bad habits initially lol.

new stuff will all be done using this method but there are thousands of lines of code i would have to go through now to correct everything lol, not sure i can be bothered!

MattF
11-11-2010, 02:54 PM
Use the isset if processing is liable to be stopped if the var isn't set. Set the var using some default value if processing is to continue, and that var is necessary further on in the processing. The simplest method to collar both is:



$stockname = ((isset($stockname)) ? $stockname : false);

babelfish
11-11-2010, 03:16 PM
Use the isset if processing is liable to be stopped if the var isn't set. Set the var using some default value if processing is to continue, and that var is necessary further on in the processing. The simplest method to collar both is:



$stockname = ((isset($stockname)) ? $stockname : false);


thats nice and concise

for me this is better as it returns an empty string if its not set:
$trading_name = ((isset($trading_name)) ? $trading_name : "");

MattF
11-11-2010, 03:47 PM
Aye, with that final part just set it to whatever your preferred default is for the var in question. It stops the script choking, erring or being compromised by unexpected, (or nonexistent), input then. If you're wanting to set a default that involves a bit more processing, the if/else is still the better option, but for the simple ones the above is simpler and less to type.

babelfish
11-11-2010, 03:49 PM
cheers matey :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum