Undefined variable

Discussion in 'PHP' started by Bruny07, Sep 9, 2011.

  1. #1
    I'm trying to use this variable
    <?php echo $text2; ?>
    PHP:
    but I'm getting an error:
    Error
    Message: Undefined variable: text2
    Code (markup):
    This is the code:
    class Subpage
    {
    	static function Build()
    	{
    		if(User::$isLoggedin != TRUE)
    		{
    			redirect('login');
    		}
    
    		$details = User::$details;
    		if(isset($_POST['update']))
    		{
    			if(strlen($_POST['details']) >= 15)
    			{
    			$text2 = 'test';
    			}
    			elseif(strlen($_POST['details']) <= 15)
    			{
    				$id = User::$id;
    				$details = $_POST['details'];
    				$db_details = DB::safe($details);
    
    				DB::query("UPDATE top_topsites SET details = '$db_details' WHERE id = '$id' LIMIT 1");
    			}
    		}
    
    		Load::view('ucp/details', array('id' => User::$id, 'siteurl' => Config::item('siteurl'), 'details' => $details));
    	}
    }
    PHP:
    Thanks
     
    Bruny07, Sep 9, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    $text2 is being defined here:
    
    
    [COLOR=#b1b100]if[/COLOR][COLOR=#009900]([/COLOR][URL="http://www.php.net/strlen"][COLOR=#990000]strlen[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'details'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]>=[/COLOR] [COLOR=#cc66cc]15[/COLOR][COLOR=#009900])[/COLOR]
    [COLOR=#009900]{[/COLOR]
      [COLOR=#000088]$text2[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'test'[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#009900]}[/COLOR]
     
    Code (markup):
    If the condition is false, $text2 never gets defined. Try
    
    
    [COLOR=#b1b100]$text2 = '';
    if[/COLOR][COLOR=#009900]([/COLOR][URL="http://www.php.net/strlen"][COLOR=#990000]strlen[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'details'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]>=[/COLOR] [COLOR=#cc66cc]15[/COLOR][COLOR=#009900])[/COLOR]
    [COLOR=#009900]{[/COLOR]
      [COLOR=#000088]$text2[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'test'[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#009900]}[/COLOR]
     
    Code (markup):
     
    Rukbat, Sep 9, 2011 IP
  3. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    It's not working :(
     
    Bruny07, Sep 9, 2011 IP
  4. wiremind

    wiremind Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    self::$text2 = '';
    if(strlen($_POST['details']) >= 15)
    {
      self::$text2 = 'test';
    }
    Code (markup):
    Then do
    echo Subpage::$text2;
    Code (markup):
     
    wiremind, Sep 9, 2011 IP
  5. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Fatal error: Access to undeclared static property: Subpage::$text2 in D:\xampp\htdocs\top\templates\v1\pages\ucp\details.php on line 40
    Code (markup):
     
    Bruny07, Sep 9, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Where is

    <?php echo $text2; ?>
    PHP:
    appearing? It's undefined outside the function it's being defined in. You can declare it public in the class and access it from outside. But what you do depends on where you're trying to use it.
     
    Rukbat, Sep 9, 2011 IP
  7. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    I've solved the problem.
    Now I'm using JS to check the string length.

    Anyway thanks for the help.
     
    Bruny07, Sep 9, 2011 IP
  8. gauvion

    gauvion Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    undefined variables shouldn't be reported anyways. that way you can use the isset function if you choose.
     
    gauvion, Sep 9, 2011 IP