Forum Moderators: coopster

Message Too Old, No Replies

Prevent Undefined Multi-Dimensional Variables

         

Borgscan

5:17 am on Sep 30, 2007 (gmt 0)

10+ Year Member



Hi, I'm trying to create a script that filters out undefined variables, to stop the notices from showing. I know they can be disabled by turning off notices with the INI file, but I'd prefer to prevent them within my script.


$output = getvar_multi("lang,system_message", "");

// Prevent underfined variables
function getvar_multi($var_name, $return)
{
global $lang;

$var_name = explode(",", $var_name);
$var_name1 = $var_name[0];
$var_name2 = $var_name[1];

if (isset($var_name1["$var_name2"]))
{
return $var_name1["$var_name2"];
}
else
{
return $return;
}
}

The problem is with this line:


return $var_name1["$var_name2"];

It just returns the number "1", bur it works fine if I change it to:

return $lang["$var_name2"];

PHP is just accessing the $lang global directly. Any ideas on how I can fix it would be much appreciated. I did try this with a class, but it just kept returning the first letter of the word for some reason. I'm not very familiar with classes, but I wouldn't mind using a class for this if it's easier.

[edited by: Borgscan at 5:21 am (utc) on Sep. 30, 2007]

Habtom

5:25 am on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$var_name = explode(",", $var_name);
$var_name1 = $var_name[0];
$var_name2 = $var_name[1];

if (isset($var_name1["$var_name2"]))
{
return $var_name1["$var_name2"];
}
else
{
return $return;
}

$var_name1 is no longer an array or just $var_name1['0'].

$var_name1 = $var_name[0];

What are you trying to achieve?

if (isset($var_name1["$var_name2"]))
{
return $var_name1["$var_name2"];

Did you mean to use $var_name instead of $var_name1?

Borgscan

5:53 am on Sep 30, 2007 (gmt 0)

10+ Year Member



Thanks for the quick response Habtom.

The function is in my functions.php file, and so I want to call this function on other pages so that I can varify if a variable is set. In this case I'm trying to varify that $lang["system_message"] (which should echo "System Message" is set and if so to echo it. If it isn't, then just output the return, which in this case, is nothing.

So I call getvar_multi("lang,system_message", ""); or I could use 3 args if it's easier... getvar_multi("lang", "system_message", "");

Of for now, if we ignore the function and just view it as this is what I'm trying to do...

$var_name1 = "lang";
$var_name2 = "system_message";

if (isset($var_name1["$var_name2"]))
{
return $var_name1["$var_name2"];
}
else
{
return $return;
}

The line in bold is where the problem is. PHP doesn't like that I'm using a variable ($var_name1) to call another variable ($lang) and it just returns 1 instead of "System Message" which is set elsewhere. I did some debugging (before posting this thread), and found that the isset() function is finding $lang["system_message"] as it should. It's the return that isn't working.

Hope that helps explain a bit. Sorry it wasn't more clear to start with.

[edited by: Borgscan at 6:03 am (utc) on Sep. 30, 2007]

Habtom

7:01 am on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you see where $var_name2 is coming from:

$var_name = explode(",", $var_name);
$var_name1 = $var_name[0];
$var_name2 = $var_name;

$var_name2 is an array, and you won't be able to put it the way you did it.

Borgscan

7:30 am on Sep 30, 2007 (gmt 0)

10+ Year Member



Okay, that part isn't working, but I've taken out that part and I'm just using:

$var_test1 = "lang";
$var_test2 = "system_message";
$return = "";

if (isset($var_test1["$var_test2"]))
{
return $var_test1["$var_test2"];
}
else
{
return $return;
}

The part in bold is the problem, because if I hard code $lang into the script it works perfectly fine.

$var_test1 = "lang";
$var_test2 = "system_message";
$return = "";

if (isset($var_test1["$var_test2"]))
{
return $lang["$var_test2"];
}
else
{
return $return;
}

The only other way around this that I can think to do is...

// Prevent underfined variables
function getvar($var_name, $var_type, $return)
{
global $lang, $config;

if ($var_type == "post")
{
if (isset($_POST["$var_name"]))
{
return $_POST["$var_name"];
}
else
{
return $return;
}
}
elseif ($var_type == "get")
{
if (isset($_GET["$var_name"]))
{
return $_GET["$var_name"];
}
else
{
return $return;
}
}
elseif ($var_type == "session")
{
if (isset($_SESSION["$var_name"]))
{
return $_SESSION["$var_name"];
}
else
{
return $return;
}
}
elseif ($var_type == "standard_var")
{
if (isset($var_name))
{
return $var_name;
}
else
{
return $return;
}
}
elseif ($var_type == "config")
{
if (isset($var_name1["$var_name"]))
{
return $config["$var_name"];
}
else
{
return $return;
}
}
elseif ($var_type == "lang")
{
if (isset($var_name1["$var_name"]))
{
return $lang["$var_name"];
}
else
{
return $return;
}
}
}

echo getvar("system_message", "lang", "");
echo getvar("some_variable", "config", ""); etc, etc

I don't really want to have so many elesif's, just have: Post, Get, Session, Standard_var, and mult_var. Make sense? If there is any other ways of accomplishing this (like with a class or something) I'd really appreciate it! :)

[edited by: Borgscan at 7:32 am (utc) on Sep. 30, 2007]

Habtom

7:48 am on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$var_test1 = "lang";
$var_test2 = "system_message";
$return = "";

if (isset($var_test1["$var_test2"]))
{
return $var_test1["$var_test2"];
}
else
{
return $return;
}

It seems you focused back again at the problem, when the solution lies at the lines above it.

What I can't understand, and you are not able to explain to me is, the way I see it, this

$var_test1["$var_test2"];
can't exist, the reason is $var_test1 is just containing the following value
$var_test1 = "lang";

Please explain this to me if there is a point I missed.

[edited by: Habtom at 7:49 am (utc) on Sep. 30, 2007]

Borgscan

7:58 am on Sep 30, 2007 (gmt 0)

10+ Year Member



Yeah, you are right. It can't exist. What I'm trying to ask is, is there a way to make PHP see that $lang["system_message"] is what I want to have returned without hard coding it? As you can see from the alternative, I can hard code it, and it works, but I'd like todo it with less elseif statements. Or is there any other way of dealing with multi-dimensional variables that are or are not set?

Habtom

8:09 am on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I understood what you are trying to do now.

Try something like the following:

if (isset(${$var_type}["$var_name"]))
{
return ${$var_type}["$var_name"];
}
else
{
return $return;
}

This will return $post instead of $_post, may be you can include that in the array as follows:
"_post"
"_session"
. . . .

Borgscan

9:25 am on Sep 30, 2007 (gmt 0)

10+ Year Member



Excellent! That's solved a big problem for my API. Many thanks!