How to access PHP variables in JQuery?

By Hardik Savani November 5, 2023 Category : PHP jQuery

Hi Friends,

This article will give you an example of how to access php variable in jquery. I would like to share with you how to use php variable in jquery. This tutorial will give you a simple example of how to get php variable in jquery. I’m going to show you about how to print php variable in jquery.

If you are working on php and you need to print your php variable inside the jquery then you can simple get value. you can use "" for get value of php variable. in following example you can see :

Example Code:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to access PHP variables in JQuery?</title>

</head>

<body>

<?php

$simple = 'demo text string';

$myArray = array("One", "Two", "Three", "Four");

?>

<script type="text/javascript">

var simple ='<?php echo $simple; ?>';

console.log(simple);

var myArray = <?php echo json_encode($myArray); ?>;

console.log(myArray);

</script>

</body>

</html>

I hope it can help you...

Tags :
Shares