[Solved] Passing PHP-variable to Javascript doesn’t work via PHP-function (not about global variables) [duplicate]


Don’t use global variables, it’s a poor practice to get in the habit of.

You should pass the value into the function as an argument:

function test23( $var_external ){
  // do stuff to modify it
  return $var_external;
}

Then print the result:

<?php print test23($var_external); ?>

It’s hard to give better advice because I don’t know what the values are really for, and why they’re being modified.

A few other tips:

  • The variable has to created outside the function before global would work.
  • Be sure a value is always printed, if it prints an empty/null val, the javascript will break. Or, use quotes and then convert to a number or whatever you need

3

solved Passing PHP-variable to Javascript doesn’t work via PHP-function (not about global variables) [duplicate]