How To Pass PHP Variables in JavaScript or jQuery Tutorial

Table of Contents

Share this Article
Reading Time: 3 minutes
236 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5