...

View Full Version : Quick Question



marf
03-18-2007, 06:33 PM
Whats the difference with these two variable assignments in PHP



$category=@$_GET[CATEGORY];

$category=$_GET[CATEGORY];


Mainly I'm wondering what the @ does.

Thanks,

Marf

CFMaBiSmAd
03-18-2007, 07:10 PM
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 -


if(isset($_GET['CATEGORY'])){
// the get variable is set
$category=$_GET['CATEGORY'];
} else {
// the get variable is not set
// set a default value
$category= 0;
// 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";
}

marf
03-18-2007, 08:03 PM
Thankyou for your response

Good to know that.

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum