Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Question Undefined variable - stop logging?

    hi all, ive moved from easywamp to zendserver for php development but im now getting lots of really annoying messages.

    Code:
    <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 :

    Code:
    <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
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    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!
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    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:

    Code:
    "<td style='text-align:right'>{$row[stockrack]}</td>\n" .
    
    rather than
    
    "<td style='text-align:right'>{$row['stockrack']}</td>\n" .
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 Code:
    <?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 Code:
    <?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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    ok, but i get an error with:

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

    Code:
    <input type="text" class="text" id="stockname" name="stockname" value='<?php if(isset($stockname)) {echo $stockname;} ?>' />
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    so instead of:
    Code:
    	if($trading_name == $row['trading_name']) { 
    		$selcl = "selected"; 
    	} else {
    		$selcl = "selected";
    	}
    i have to do:
    Code:
    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!
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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:

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

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    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:

    Code:
    $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 : "");
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • Users who have thanked MattF for this post:

    babelfish (11-11-2010)

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    cheers matey
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •