0

Sorry for a native question that maybe duplicated as I'm a newbie in Javascript & PHP. I want to know what if happened if I change a PHP global variable in Javascript? For example, in the following code, my expected result is 345. But now 335 is returned. I know that PHP is a server side program that will only run @ page load. But why doesn't the result be 333 then? Anyone can give me some comments? Thanks

PHP code:

$a = 3;

HTML:

<head>
    <meta charset="utf-8">
</head>
<body>
    <div>
        <button type="button" onclick="func_change_php()">Change</button>
    </div>
    <div id="text1">
        0
    </div>
    <div id="text2">
        1
    </div>
    <div id="text3">
        2
    </div>
    <script>
    function func_change_text1() {
        document.getElementById("text1").innerHTML = <?php echo $a;?>;
    }
    
    function func_change_text2() {
        document.getElementById("text2").innerHTML = <?php echo $a;?>;
    }
    
    function func_change_text3() {
        <?php $a = 5;?>
        document.getElementById("text3").innerHTML = <?php echo $a;?>;
    }
    
    function func_change_php() {
        func_change_text1();
        <?php $a = 4;?>
        func_change_text2();
        func_change_text3();
    }
    </script>
</body>

Edited: If I re-arrange Javascript function by delicate func_change_php() first, same result (335) is given. But if I put $a=4 out from func_change_php() and put it to the beginning of script, the result become 445

Sin Shu Kei
  • 71
  • 1
  • 6

0 Answers0