problem with strings

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
jim_php
Forum Newbie
Posts: 16
Joined: Sat Mar 22, 2008 6:16 am

problem with strings

Post by jim_php »

hello i developed one site with multilanguage support

so i have one lang file
and i have one example word like
$lang_click_here = "Please click here";

in my php script i use $lang_click_here and shown the word "Please click here"
but i want something different
i have one record in mysql and i load

example

$res->message // shown the word "click_here"

the best way i know to show rigth is

if($res->message = "click_here")
{
echo "$lang_click_here"; /// show Please click here
}


but i want something direct like
echo "".$lang_."$res->message"; //direct show please click here

but return "$lang_click_here" exactly not "please click here"



any idea to add the $lang_ in to mysql result as string and tranlsate from file ?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Re: problem with strings

Post by dhrosti »

from what i understand, you need to use "variable variables". so the value of one variable is used as the name of another variable, like...

Code: Select all

echo $lang_{$res->message};
Last edited by dhrosti on Fri Jul 18, 2008 6:35 am, edited 1 time in total.
jim_php
Forum Newbie
Posts: 16
Joined: Sat Mar 22, 2008 6:16 am

Re: problem with strings

Post by jim_php »

your choice is good but i cant fix it
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Re: problem with strings

Post by dhrosti »

sorry, i missed off one of the dollar signs, should work now.
jim_php
Forum Newbie
Posts: 16
Joined: Sat Mar 22, 2008 6:16 am

Re: problem with strings

Post by jim_php »

you mean something like taht ?

Code: Select all

 
$lang_man = "this is a man";
 
$man = "man"; // 
 
echo $lang_{$man};
 
 

i not return something :(
jim_php
Forum Newbie
Posts: 16
Joined: Sat Mar 22, 2008 6:16 am

Re: problem with strings

Post by jim_php »

i think something

Code: Select all

 
$lang_man = "this is a man";
 
$$man = "man"; // 
 
echo "$lang_${$man}";
 
 
return man ... but i dont want set the line " $$man = "man"; // "

any idea ?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Re: problem with strings

Post by dhrosti »

this is a bad way to do it, but it's the only way i can get it to work...

Code: Select all

$lang_man = "this is a man";
  
$man = "man"; //
  
eval('echo $lang_'.$man.';');
Last edited by dhrosti on Fri Jul 18, 2008 7:58 am, edited 1 time in total.
jim_php
Forum Newbie
Posts: 16
Joined: Sat Mar 22, 2008 6:16 am

Re: problem with strings

Post by jim_php »

ow this work well

on note
i find the easyest way to do this

and try
this

Code: Select all

 
$lang_man = "this is a man";
 
$man = "man"; // 
 
echo "$lang_${$$man}";
 


and work perfect

but when try to insert in my page like this

Code: Select all

 
$lang_man = "this is a man";
 
echo "$lang_${$$res->gender}";    /// $res->gender return value "man"
 
 
i return this error
Catchable fatal error: Object of class stdClass could not be converted to string in /home/website/public_html/profile.php on line 99
jim_php
Forum Newbie
Posts: 16
Joined: Sat Mar 22, 2008 6:16 am

Re: problem with strings

Post by jim_php »

any idea ?
Post Reply