Jump to content

Recommended Posts

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

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);

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.