Pregunta

Let's say I have a file effects.php with an array in it like

$techniqueDescriptions = array("damage" => "Deals ".$level." damage.");

And I have another file display.php that both gets the level and displays the attack like.

$level = $user->data["level"];
echo $techniqueDescriptions["damage"];

I tried the setup above and it gives "Deals damage", even if I declare it global in both files. How can I get it to work, if it's possible?

¿Fue útil?

Solución 2

Consider using level as a parameter.

effects.php:

function techniqueDescriptions($level) {
  return array("damage" => "Deals ".$level." damage.");
}

display.php:

require_once('effects.php')
$level = $user->data["level"];
echo techniqueDescriptions($level)["damage"];

Otros consejos

No. You're defining $level AFTER the array has been parsed/executed/constructed by PHP. PHP cannot "reach back in time" to retroactively insert a value for $level which didn't exist at the time you tried to insert $level into the array when it was being parsed.

You'd have to do something like

$level = 'foo';
include('array_gets_defined_here.php');
echo $techniqueDescriptions['damage'];

Doing it the other way around:

include('array_gets_defined_here.php');
$level = 'foo';
echo $techniqueDescriptions['damage'];

gets you into the time travel situation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top