Reputation: 12564
Sorry if it's an irritating question (i come from a ruby background and i am new to PHP).
I find it kind of weird that you can't do:
if($defined_array['undefined_index'] == 'a string') {
do_something();
}
without getting slammed in the face with a warning.
false
. Is it due to interpreter limitations ? What are the technical reasons that are behind this ? I'm just curious.Is there any function, construct, idiom or whatever that i don't know that would avoid me to do this every 4 lines or so:
if(isset($defined_array['undefined_index']) && $defined_array['undefined_index'] == 'a string') {
do_something();
}
I know that i could declare every single array keys i intend to use, but what's the point of not being forced into declaring variables before using them if PHP intends to slap you every time you do it ?
Upvotes: 1
Views: 266
Reputation: 1055
No, not with the same expression. However, you could make an own comparison function or use the (ABSOLUTELY HORRIBLE) error suppression operator @. Error suppression is ALWAYS a bad idea though, I recommend you to solve the problem instead.
1: Own function:
function setAndEq($a, $b){
if(isset($a, $b))
return $a === $b;
else
return false;
}
if(setAndEq($array['undefined_index'], "hello")) {
// hello
}
2: Error suppression:
if(@strcmp($array['undefined_index'], "hello") === 0) {
}
Upvotes: 2
Reputation: 782315
The reason for the warning is for code safety reasons. While it's possible that the reason the array key doesn't exist is because it's not supplied in this instance, it's also possible that there was a typo in either setting or retrieving the element. Without warnings, such coding errors would be harder to notice.
Upvotes: 3
Reputation: 1995
Regarding 1: When a variable doesn't exist you should always get a warning. It indicates that you didn't write your code correct. When an unset variable (or an array key) would evaluate to FALSE, then this would be just wrong. I guess the question isn't what's the technical reason - it#s just common sense.
Regarding 2:
No. Just use empty()
or isset()
and use strict type checking.. p.e. if ($x === FALSE)
instead of if ($x == NULL)
.
Upvotes: 1
Reputation: 5536
Edit: combining isset and the error control operator you may declare a function that always return the value of a variable or null
if it is not set:
$ php -a
Interactive shell
php > function getvalue($var) {
php { return isset($var) ? $var: null;
php { }
php > $testarray['key1'] = "hello";
php > if ($myvar = @getvalue($testarray['key1'])) echo "var: " . $myvar;
var: hello
php > if ($myvar = @getvalue($testarray['key2'])) echo "var: " . $myvar;
php >
Note that you cannot distinguish between a variable that doesn't exist or a variable that does but its value is null
.
Upvotes: 0
Reputation: 20753
You could silence the warnings with setting an appropriate error level but that is not a really good idea. Personally i use a small function to ease on typing:
/**
* nullsafe
*
* returns values from array like objects
*
* Usage:
* nullsafe($array, 'key'); - returns the $array['key'] if it is set
* nullsafe($array, 'key', 'foo'); - returns the $array['key'] if it is set, 'foo' otherwise
* nullsage($array, array('level0', 'level1')); returns $array['level0']['level1'] if it is set, null otherwise
*
* returns value indexed by $key from the $from, or the $default if its not present
* if the key is an array then it will descend it into the $from like $from[$key[0]][$key[1]][$key[2]]...
*
* @param mixed $from
* @param mixed $key
* @param mixed $default
* @return mixed
*/
function nullsafe($from, $key, $default = null) {
if (!is_array($key)) {
if (isset($from[$key])) {
return $from[$key];
}
} else {
foreach ($key as $k) {
if (!isset($from[$k])) {
return $default;
} else {
$from = $from[$k];
}
}
return $from;
}
return $default;
}
Upvotes: 1
Reputation: 2269
It depends, first of all you can turn of your error_reporting, but i wouldn't reccomend you that.
When you have multiple checks for the same array-key you also could try this:
if (isset($defined_array['undefined_index'])) :
switch ($defined_array['undefined_index']) :
case 'a string' :
do_something();
break;
case 'b string' :
do_anotherthing();
break;
endswitch;
endif;
In that case you don't have to worry about all the warnings on each row.
Upvotes: 1