我创建一个简单的语言功能失败了,我做错了什么?

I tried to create a simple language function, but I can't get it to work. The idea should be clear. I'm not getting any error messages but it won't show my text either. This is my code:

inc/text.php:

<?php

$show = array(

"welcome" => "Welkom @ ....", 
'test' => true 

);

function show($foo) {

echo $show[$foo];

}

?>

index.php:

<?php show("welcome"); ?>

It looks to me like you're declaring $show outside of the scope of function show($foo)

either declare $show inside the function or do this:

$show = array(); //blah blah

function show($foo)
{
    global $show;
    echo $show[$foo];
}

Make $show array global scope within the function, and return the value not echo it

<?php

$show = array(
"welcome" => "Welkom @ ....", 
'test' => true 
);

function show($foo) {
global $show;
return $show[$foo];
}
?>

<p><?php echo show("welcome"); ?></p>

You need to declare the SHOW array, so it is accesible from the function.

You have sevral methods to do this

  • Global variable
  • Pass it like a parameter
  • Declare the array in a function

Example of declaration inside a function

function show($foo) {
  $show = array(
    "welcome" => "Welkom @ ....", 
    'test' => true 
  );

  echo $show[$foo];
}

You don't need a function for that:

<?php echo $show["welcome"]; ?>

That should already do it. Let me know if that is not helpful and why.