NEW ANSWER FOR ADDED CODE
<?= include_once("language.php"); ?>
— this is wrong. You can use <?=
only when you outputting variable (<?=$welcome;?>
== <?php echo $welcome; ?>
). You have to remove =
sign from this php tag. <? include_once("language.php"); ?>
is correct.
Use console.log(welcome)
or alert(welcome)
to be sure, that welcome is assigned.
OLD ANSWER You output this js and html throw php, right?
You can use this:
// some php code
?>
<script type="text/javascript">
var welcome = <?=json_encode($welcome); ?>
// alert(welcome); // to make sure value is assigned in js
</script>
<?
//more php code or html output
You have to use <?=json_encode($welcome); ?>
instead of simple <?=$welcome;?>
. Explanation by Marc B:
Never dump arbitrary text from PHP into javascsript code. You are HIGHLY likely to introduce a JS syntax error and kill the entire script. ALWAYS use json_encode:
<?= json_encode($welcome) ?>
Additional example:
// more php
include_once("language.php");
// var_dump($welcome); // to debug what value assigned to $welcome
?>
<script type="text/javascript">
window.onload = function() {
function _(x){
return document.getElementById(x);
}
var welcome = <?=json_encode($welcome);?>
_("test").innerHTML = '<h2>Mesajul este: ' + welcome + '</h2>';
}
</script>
<?
// more php