Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda