Undefined variable error in php code

Discussion in 'PHP' started by digitalarts, Oct 19, 2009.

  1. #1
    This issue has mad me mad so I wi=oul b grateful if any one can through somelght abt it.

    While viewing a report I see the following error and the table does not show any data:

    Notice: Undefined variable: hasarow in C:\wamp\www\qa\coach\reporta.php on line 330

    In my code, I have used the hasarow in the below area:

    if($showthis){
    $hasarow = true;....Line 216	
    $ID = $row['ID'];
    Code (markup):
    And

    if(!$hasarow){.......Line 299
    echo '<tr>
    	          <td bgcolor="#FFFF99" colspan="36" style="padding:8px;"><strong> Sorry, No Report available for the selected criteria.</strong></td>
                </tr>';
    Code (markup):
    I do not know what is wrong. Please help me..
     
    Last edited: Oct 19, 2009
    digitalarts, Oct 19, 2009 IP
  2. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Try defining that variable before you actually use it. Somewhere toward the beginning of the file or before the first use of $hasarow add the code below.

    $hasarow = false;

    ... or add this code to the beginning of that file.

    error_reporting(E_ALL ^ E_NOTICE);

    or this code, but not both.

    error_reporting(0);

    Good luck.
     
    Last edited: Oct 19, 2009
    Webray, Oct 19, 2009 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    I would however strongly recommend to define that variable with initial value. It's always a good habit to do that way rather than suppressing errors and notices. This is my opinion, which may differ from others :)
     
    mastermunj, Oct 19, 2009 IP
  4. DollaradeConner

    DollaradeConner Active Member Affiliate Manager

    Messages:
    200
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #4
    I agree with mastermunj, if you use the variable $hasarow within different functions, you should make it global.

    The problem is $hasarow is initialized only when $showthis is true. If $showthis is false, the $hasarow variable won't even be created, hence your error when you try to check if it's true or false.

    If you don't want to initialize the variable first, you can always use this:

    if( !isset( $hasarow )) {
    Code (markup):
     
    DollaradeConner, Oct 21, 2009 IP
  5. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Both of these gentlemen have given good advice. :)

    Where are you, digitalarts!

    Ray
     
    Webray, Oct 21, 2009 IP
  6. LOD

    LOD Member

    Messages:
    319
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    35
    #6
    at least show us whats on the 330 line??
     
    LOD, Oct 23, 2009 IP
  7. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    :) this is good programming style. even in a production environment where the notices should be disabled, there should be no reason to suppress them!

    always give an initial value!
     
    heavydev, Oct 23, 2009 IP