Help needed: insert variables inside predefined variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Help needed: insert variables inside predefined variables

Post 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.
Last edited by neuroxik on Wed Aug 12, 2009 8:31 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help needed: insert variables inside predefined variables

Post 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?
(#10850)
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Re: Help needed: insert variables inside predefined variables

Post 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.
Last edited by neuroxik on Wed Aug 12, 2009 8:32 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help needed: insert variables inside predefined variables

Post 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'];
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Re: Help needed: insert variables inside predefined variables

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Help needed: insert variables inside predefined variables

Post by Mark Baker »

have a look at printf()

Code: Select all

 
$lang['user_score'] = "Your score was %s !!";
printf($lang['user_score'],30);
 
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Re: Help needed: insert variables inside predefined variables

Post by neuroxik »

Oh wow! This is exactly what I was looking for!

Thank you so much!
Post Reply