Reputation: 19
I'm trying to check if a variable is existing, and if not - then define it.
$checkarray = array($demo1, $demo2, $demo3);
foreach ($checkarray as $checkkey) {
if (!isset($checkkey)) {
$checkkey = 'none';
}
}
But I'm just getting this error: *Notice: Undefined variable: demo1 (and so on...)
This is bascially what i'm trying to achive...
if (!isset($demo1)) {
$demo1 = 'none';
}
if (!isset($demo2)) {
$demo2 = 'none';
}
if (!isset($demo3)) {
$demo3 = 'none';
}
But it's not pretty.
Any ideas?
Cheers Kris
Upvotes: 1
Views: 1852
Reputation: 2999
You have to use the var name in your check array and not the var itself. This is called Variable variable
Then you can do something like this :
$checkarray = array('demo1', 'demo2', 'demo3');
foreach ($checkarray as $checkkey) {
if (!isset($$checkkey)) {
$$checkkey = 'none';
}
}
Upvotes: 1