Page 1 of 1
Help needed: insert variables inside predefined variables
Posted: Tue Aug 11, 2009 10:48 pm
by neuroxik
Hey everyone,
I'll try to be as brief and clear as possible.
I am very flexible with php, but something seems to have escaped me. So even if your answers are references of something like : "Lookup constants with..." or whatever, I'll be more than grateful for any solution, atleast I'll have some sort of direction cuz I haven't really found anything yet.
I'll use a random language file example:
Code: Select all
<?php
$lang['user_score'] = "Your score was ".$some_var_here." !!";
$lang['something_else'] = "You scored ".$some_var_here." on your ".$some_var_here." test.";
?>
So, how would I replace
$some_var_here from above (which was a fictional reference knowing this can't be done this way) and also, how would I call
$lang['something_else'] as to include the variable(s) ?
Thanks.
Re: Help needed: insert variables inside predefined variables
Posted: Wed Aug 12, 2009 1:05 am
by Christopher
neuroxik wrote:Code: Select all
<?php
$lang['user_score'] = "Your score was ".$some_var_here." !!";
$lang['something_else'] = "You scored ".$some_var_here." on your ".$some_var_here." test.";
?>
So, how would I replace
$some_var_here from above (which was a fictional reference knowing this can't be done this way) and also, how would I call
$lang['something_else'] as to include the variable(s) ?.
I am not clear what you mean by "replace
$some_var_here"? Do you mean change the value in that variable, or to change to a different variable, or something else? Also not clear what "how would I call
$lang['something_else'] as to include the variable(s)" means?
Re: Help needed: insert variables inside predefined variables
Posted: Wed Aug 12, 2009 1:16 am
by neuroxik
What I mean by "replace
$some_var_here" is: because I cannot store a variable that will be later defined, what do I need to write instead of $some_var_here so that example this could work:
Code: Select all
// this would be in a language file, say lang.php :
$lang['something'] = "You have [somthing_that_can_be_defined_later] apples left";
// main file:
require_once('lang.php');
$x = 4;
$num_apples = $x - 1; // some calculation here, just to show that $num_apples can change any time
echo $lang['something']; // so that it outputs, for example, the following: [b]You have [color=#CC0000]3[/color] apples left[/b]
Tell me if this is still confusing, I'm having trouble explaining this myself.
Re: Help needed: insert variables inside predefined variables
Posted: Wed Aug 12, 2009 2:25 am
by requinix
Define $some_var_here before you include the file.
Code: Select all
$some_var_here = 100;
include "lang.php";
echo $lang['user_score'];
Re: Help needed: insert variables inside predefined variables
Posted: Wed Aug 12, 2009 2:33 am
by neuroxik
Thanks tasairis.
But I still have to include the file (or set the variable) after, I was just wondering if there was some way to do it otherwise. I had seen somethings around like "bla bla % bla " where the % sign was replaced by a variable, do you have any clue what that might be?
Anyhow, thanks very much.
Re: Help needed: insert variables inside predefined variables
Posted: Wed Aug 12, 2009 2:49 am
by Mark Baker
have a look at printf()
Code: Select all
$lang['user_score'] = "Your score was %s !!";
printf($lang['user_score'],30);
Re: Help needed: insert variables inside predefined variables
Posted: Wed Aug 12, 2009 3:21 am
by neuroxik
Oh wow! This is exactly what I was looking for!
Thank you so much!