Join WhatsApp ChannelJoin Now

How To Use PHP Variables To JavaScript Example

Hi,

Today, i we will show you how to use PHP variables to javaScript example. This article will give you simple example of how to use PHP variables to javaScript example. you will learn how to use PHP variables to javaScript example. So let’s follow few step to create example of how to use PHP variables to javaScript example.

Example: 1

<?php
    $message = "Hello World!"; 
?>
<!DOCTYPE html>
<html>
<head>
    <title>How To Use PHP Variables To JavaScript Example</title>
</head>
<body>
    <h1>How To Use PHP Variables To JavaScript Example</h1>    
    <script type="text/javascript">
        var message = "<?php echo"$message"?>";
        alert(message);
    </script>
</body>
</html>

Example: 2

<?php
    $fruits = ['orange' => 'mango', 'yellow' => 'banana', 'red' => 'apple'];
    $json_fruits = json_encode($fruits);
?>
<!DOCTYPE html>
<html>
<head>
    <title>How To Use PHP Variables To JavaScript Example</title>
</head>
<body>
    <h1>How To Use PHP Variables To JavaScript Example</h1>    
    <script type="text/javascript">
        var fruits = <?php echo($json_fruits)?>;
        alert(fruits.yellow);
    </script>
</body>
</html>

I hope it will assist you…

Recommended Posts