Forum Moderators: coopster
$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"];
return $lang["$var_name2"];
[edited by: Borgscan at 5:21 am (utc) on Sep. 30, 2007]
$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?
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]
$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]
$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]
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"
. . . .