yyx748 Posted October 17, 2008 Share Posted October 17, 2008 hi, im juz doing a simple English to chinese function but it seem to be not working. need help!! thanks <?php $language = "english"; //// only want to change here function lang($eng,$chn){ if ($language == "english") echo $eng; else echo $chn; } lang(english,chinese); ///// ill be having alot of this all over my website and will be english and chinese text inside.?> Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/ Share on other sites More sharing options...
MadTechie Posted October 17, 2008 Share Posted October 17, 2008 it maybe better to use OOP or even sessions it really depends on what your doing but you can use global access it ie <?php $language = "english"; //// only want to change here function lang($eng,$chn){ global $language; if ($language == "english") echo $eng; else echo $chn; } lang(english,chinese); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/#findComment-668076 Share on other sites More sharing options...
yyx748 Posted October 17, 2008 Author Share Posted October 17, 2008 the variable $language will taken from my cookie but right now i cant seem to get my function to work. like the output is the same despite i change $language Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/#findComment-668081 Share on other sites More sharing options...
MadTechie Posted October 17, 2008 Share Posted October 17, 2008 Okay try this <?php $language = "english"; //// only want to change here function lang($eng,$chn) { global $language; if ($language == "english") { echo $eng; }else{ echo $chn; } } lang("english","chinese"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/#findComment-668087 Share on other sites More sharing options...
discomatt Posted October 17, 2008 Share Posted October 17, 2008 the variable $language will taken from my cookie but right now i cant seem to get my function to work. like the output is the same despite i change $language The reason is variable scope! Read up here http://php.net/variables.scope When you change it over to $_COOKIE, you won't have to worry about this, as $_COOKIE is a superglobal... read up here http://php.net/manual/en/language.variables.superglobals.php Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/#findComment-668090 Share on other sites More sharing options...
discomatt Posted October 17, 2008 Share Posted October 17, 2008 And on that note, rather than use the global keyword, I strongly prefer this method <?php $language = "english"; //// only want to change here function lang($eng,$chn) { $language = $GLOBALS['language']; if ($language == "english") { echo $eng; }else{ echo $chn; } } lang("english","chinese"); ?> That will create a local instance of the global variable $language. This way, your function can't 'accidentally' change values in the global scope Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/#findComment-668092 Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 you may want to consider another method of providing several languages (for scalability reasons). once the site is done, what if you want to add french? it would be silly to go back into all the pages and add a third argument. usually, sites will have a file for each language, and key/value pairs with all the translations. <?php $lang = array(); $lang['one'] = 'one'; $lang['welcome'] = 'Welcome to my site'; ?> then, just include that file at the top of your script, and use $lang (or write a lang() function): <?php $language = 'english'; require_once($language.'.php'); ?> <html> <body> <h1><?php echo $lang['welcome']; ?></h1> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/#findComment-668094 Share on other sites More sharing options...
yyx748 Posted October 17, 2008 Author Share Posted October 17, 2008 Thanks so much!!!!! it works now!!!!! thankssss!! Quote Link to comment https://forums.phpfreaks.com/topic/128866-php-function-help-needed/#findComment-668098 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.