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

Thread: Quick Question

  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quick Question

    Whats the difference with these two variable assignments in PHP

    PHP Code:
    $category=@$_GET[CATEGORY];

    $category=$_GET[CATEGORY]; 
    Mainly I'm wondering what the @ does.

    Thanks,

    Marf

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    It suppresses the output of error messages. In this case, if the get variable does not exist.

    There are very few cases where the @ should be used. It typically gets used to suppress a nuisance error message instead of having proper conditional logic.

    Inputs to programs always need to be tested and validated and an appropriate action needs to be taken depending on what was entered. Code that does not check its input data is the code you see posted in a Forum like this with a "why does this code not work?" question.

    For the case of a variable from a form/url parameter, you should do something like the following -

    PHP Code:
    if(isset($_GET['CATEGORY'])){
        
    // the get variable is set
        
    $category=$_GET['CATEGORY']; 
    } else {
        
    // the get variable is not set
        // set a default value
        
    $category0
        
    // and/or generate some error reporting and feed back as to what is wrong with the data -
        
    echo "You did not enter a value for category";

    Last edited by CFMaBiSmAd; 03-18-2007 at 07:13 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thankyou for your response

    Good to know that.



  •  

    Posting Permissions

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