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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts

    Undefined variable

    Hello

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

    i use this ..

    PHP Code:
    <?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..

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It's just as simple as checking to see whether or not the $_SESSION value exists prior to reassignment, e.g.
    PHP Code:
    <?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)".

  • Users who have thanked bdl for this post:

    ajloun (02-09-2010)

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    Thank u .and thx for the tip


  •  

    Posting Permissions

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