How to call javascript function in PHP code

<html>
<body>
<pre id="msg"></pre>
<script>
        function a( i, str ) {
                alert("This is msg." + i + str);
                return 2;
        }
</script>
</body>
</html>
 
<?php
        $a = 1;
        if ($a == 2)
        {
        }
        else
        {
                $i = 100;
                $x = 'test';
                echo '<script>',
                        'var r = ', 'a(', json_encode($i), ',', json_encode($x),');',
                        'document.getElementById("msg").innerHTML=r',
                     '</script>';
        }
?>
Last modified: le 2018/10/08 12:54