Jump to content

Recommended Posts

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.?>

 

Link to comment
https://forums.phpfreaks.com/topic/128866-php-function-help-needed/
Share on other sites

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

 

 

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

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 :D

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>

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.