Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Change a php global variable in a javascript function

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!