pioneerx01 Posted August 18, 2012 Share Posted August 18, 2012 Let's say that I have this text stored in the database: My secret code is $secret_code When I echo this out plainly, I get: My secret code is $secret_code I already defied $secret_code = "123"; before I call for it from database. What function do I need to use to get it to echo out: My secret code is 123 Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted August 18, 2012 Share Posted August 18, 2012 I will personally lambaste anyone who mentions the "e" function. Strongly. Create an array of "variables" you want to replace, then replace them. str_replace() is the normal way of doing that but strtr() is actually more convenient. $vars = array( '$secret_code' => $secret_code ); $string = strtr($string, $vars); Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2012 Share Posted August 18, 2012 I will personally lambaste anyone who mentions the "e" function. Strongly. My . . . whatever do you mean? Quote Link to comment Share on other sites More sharing options...
pioneerx01 Posted August 18, 2012 Author Share Posted August 18, 2012 Thanks, I was hoping that there would be something simpler like the "e" function that shall not be named. Once more semi-related question. I got this running off the page by itself no problem. But now I am trying to make a function from this, but $secret_code always comes up blank. Being new to functions I am not sure if arrays need to be specially configured? Anyways is this correct? function fromdatabase ($variable) { $vars = array ( '$secret_code' => $secret_code ); $variable = strtr($variable, $vars); return $variable; } And $secret_code is defined before the function and before I call for function. Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted August 18, 2012 Share Posted August 18, 2012 But it's defined outside the function. Unlike many languages, in PHP variables defined outside functions are not available inside functions. I'm not sure if I like suggesting this, but function fromdatabase ($variable, array $vars) { $replace = array(); foreach ($vars as $var) { $replace['$' . $var] = (isset($GLOBALS[$var]) ? $GLOBALS[$var] : ""); } return strtr($variable, $replace); } $string = fromdatabase($string, array("secret_code")); I'm steering clear of anything that automatically pulls in variables: that kind of automated e***uation can backfire just like how register_globals can. This way you're importing variables from the global scope (eww) but only the ones you specifically allow in code. Quote Link to comment Share on other sites More sharing options...
MMDE Posted August 18, 2012 Share Posted August 18, 2012 Can't you just put them in an array? $array['secretcode'] = '123'; then you get from the database: 'secret code' and assign it to $result_from_db. to access secret code, simply use: $array[$result_from_db] or does this not make sense? o.O Did I not understand the problem? =P I at least think it needs a different approach. What the user sees doesn't need to be the same as what the server "really" is working with. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.