问题
I've noticed on XAMPP that strict error reporting is on and I get undefined index errors now. I just have two small questions (I'm still learning here):
I know you don't have to declare variables in PHP but is there any advantage to declaring them anyway? If not, why do I get errors when strict error reporting is on when I don't define them?
When I use get variables for example, I check for their value before I run a function like
if($_GET['todo'] == 'adduser')
runFunctionAddUser();
This gives an error because I never check if the get variable exists first. Should I do
if(isset($_GET['todo']))
if($_GET['todo'] == 'adduser')
runFunctionAddUser();
instead? Would there be an advantage to this or would it be unnecessary and slow?
回答1:
This thread is abit old but I did some tests relating to the question so I might as well post it:
The testing code (PHP 5.3.3 - CentOS release 6.5 (Final)):
class NonExistant
{
protected $associativeArray = array(
'one' => 'one',
'two' => 'two',
'three' => 'three',
'four' => 'four',
'five' => 'five',
'six' => 'six',
);
protected $numIterations = 10000;
public function noCheckingTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
$this->associativeArray['none'];
}
}
public function emptyTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
empty($this->associativeArray['none']);
}
}
public function isnullTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
is_null($this->associativeArray['none']);
}
}
public function issetTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
isset($this->associativeArray['none']);
}
}
public function arrayKeyExistsTest()
{
for ($i = 0; $i < $this->numIterations; $i++) {
array_key_exists($this->associativeArray['none']);
}
}
}
The results are:
| Method Name | Run time | Difference
=========================================================================================
| NonExistant::noCheckingTest() | 0.86004090309143 | +18491.315775911%
| NonExistant::emptyTest() | 0.0046701431274414 | +0.95346080503016%
| NonExistant::isnullTest() | 0.88424181938171 | +19014.461681183%
| NonExistant::issetTest() | 0.0046260356903076 | Fastest
| NonExistant::arrayKeyExistsTest() | 1.9001779556274 | +209.73055713%
All the functions are called the same way with via call_user_func()
and timed with microtime(true)
Observations
empty()
and isset()
are the clear winners over the other 2 methods here, the two methods are pretty much tied on performance.
is_null()
performs bad because it needs to lookup the value first, pretty much the same as just accessing the non-existant value $this->associativeArray['none']
, which involves a full lookup of the array.
However, i'm surprised by the performance of array_key_exists()
. Where it is 2 times slower than empty()
and isset()
.
NOTE:
All the functions I've tested have different uses, this benchmark is only here for the most generic use case where you want to quickly check the value in an array. We can go into argument of whether null
should be considered a "value" or simply an indicator of non-existence, but that's another topic. o.o
UPDATE 2017-01-20
Use PHP 7.1
Fixed bug mentioned by @bstoney
$ php -v
PHP 7.1.0 (cli) (built: Dec 2 2016 03:30:24) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
$ php -a
php > $a = ['one' => 1, 'two' => 2, 'three' => 3];
php > $numIterations = 1000000;
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { $a['none']; }; echo microtime(true) - $start;
0.43768811225891
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { empty($a['none']); }; echo microtime(true) - $start;
0.033049821853638
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { is_null($a['none']); }; echo microtime(true) - $start;
0.43995404243469
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { isset($a['none']); }; echo microtime(true) - $start;
0.027907848358154
php > $start = microtime(true); for ($i = 0; $i < $numIterations; $i++) { array_key_exists('none', $a); }; echo microtime(true) - $start;
0.049405097961426
回答2:
I would suggest if(!empty($_GET['todo']) && $_GET['todo'] == 'adduser')
. This works because PHP short-circuits logic expressions.
It's best to develop using an error reporting level of E_ALL
(which includes E_NOTICE
) because it quickly helps identify mistyped variable / key names.
If you're asking whether or not testing for the existence of a variable is slow, you're asking the wrong question (the answer is that it's blazing fast. Your slowness comes in working with the filesystem / database / etc).
回答3:
It's not an error about a missing variable you're getting, it's an error about trying to compare variables to a non-existent variable.
If you try to compare nothingness
to 'adduser' it throws an error, because you can't compare nothingness
.
That's the answer to #1.
For #2, you SHOULD almost always (if not always) check for the existence of your variables, especially your GET variables, because they're so volatile.
回答4:
Thats not a direct answer because you already got that, but an information how to archive it:
I always use a function like this in all my programs to initialize the incoming variables (with a default value if needed).
So you could do your initializing like this:
$todo = initGet('todo');
And then just check normally:
if($todo=='adduser') {
And the function:
function initGet($var, $default=''){
if(!isset($_GET[$var])) return $default;
return $_GET[$var];
}
来源:https://stackoverflow.com/questions/8409249/should-i-declare-and-check-if-variables-exist-in-php