How To Pass PHP Variables in JavaScript or jQuery Tutorial

Reading Time: 3 minutes
1,437 Views

Inside this article we will see the concept i.e How to pass PHP variables in JavaScript or jQuery. Article contains the classified information about passing PHP variables and it’s values to javascript variables.

In many cases you find yourself in such a case where you want to put PHP variable values to javascript or jquery variable. In this tutorial you will learn how to pass PHP variables to javascript variables.

Learn More –

Let’s get started.

Application Programming

Create index.php file in your localhost path. Open file and write this code into it.

<!DOCTYPE html>
<html>

<head>
    <title>How To Pass PHP Variables in JavaScript or jQuery Tutorial</title>
</head>

<body>
    <?php
    $simple = 'Welcome to Online Web Tutor';
    $complexArray = array('PHP', 'CodeIgniter 4', 'Laravel 9', array('WordPress', 'jQuery', 'Javascript'));
    ?>
    <script type="text/javascript">
        var simple = '<?php echo $simple; ?>';
        console.log(simple);
        var complexArray = <?php echo json_encode($complexArray); ?>;
        console.log(complexArray);
    </script>
</body>

</html>
      

Once above will compiles, you will see in source code of the page as –

You can see the PHP variable values put into javascript variables.

Above code will log message into console window.

We hope this article helped you to learn How To Pass PHP Variables in JavaScript or jQuery Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more