How to convert php variable to Javascript variable

There are many cases where there is a need to convert php variable to Javascript variable, ultimately passing data from the backend to the frontend.

Usually, PHP can generate the entire HTML page before sending it back to the browser for rendering, so you might ask why you would need to pass a PHP variable to Javascript.

Why pass a PHP variable to Javascript

One of the most common reasons a PHP variable needs to be passed to Javascript is because there is some Javascript logic that needs to process the data prior to rendering on the page.

For instance, a list of categories that have been pulled from the database may require to be passed to Javascript. One reason could be because there is some logic whereby only user interaction will show certain categories on the page.

The data being passed from PHP to Javascript might also require transformation. For example, what if the value of the PHP variable is an array, but the receiving Javascript variable is expecting a JSON object.

Examples of passing PHP variables to Javascript

Below are some examples of how you can pass PHP variables to Javascript. The examples also show the different types of values that also need to be transformed or converted.

Bear in mind, all the examples are HTML pages that have the PHP and Javascript embedded. When the page is rendered, the PHP values are then passed to JavaScript.

Passing a PHP string or integer to Javascript

This is the most straightforward and easiest example. Simply echo the PHP variable into a Javascript variable.

Notice how when echoing the string into a Javascript variable requires double quotes, whereas echoing a PHP integer into a Javascript variable requires no quotes.


  $php_string = "string"; 
  $php_integer = 6;

  const javascript_string = "<?=$php_string;?>"; 
  const javascript_integer = <?=$php_integer;?>; 


Passing a PHP array to Javascript object

In this example, its possible to utilise the PHP built in function json_encode(). This function will convert a PHP array into a JSON object.

Once converted the PHP variable can be simply be echoed back into the Javascript, where JavaScript can parse the JSON string into an object.


  $php_variable = json_encode(["item1", "item2", "item3"]);

  const javascript_variable = JSON.parse(<?=$php_variable;?>);


Using HTML to elements for JavaScript to read

Another more slightly complex way of passing values to JavaScript from PHP is to first pass the values to an HTML element.

Among other ways, PHP can utilise the HTML form. More specifically, echo the values into a hidden form input box.

Once the page is rendered, JavaScript can search for the HTML element and fetch the value.


<input id="php-value-to-javascript" type="hidden" value="<?=$php_value;?>">

 const fetchValue = document.querySelector("#php-value-to-javascript").value;


You can pass what ever string you like into the hidden input box. That could be a string, integer or JSON string that can be parsed using JavaScript.

Using this method is a good way of passing data such as a logged in user id or session id that is needed on the frontend.


There are a few ways you can convert PHP variable to a Javascript variable. All these different ways are quite easy with straight forward implementations.

The most complex and difficult task will be to send whatever data you need from PHP to Javascript in the correct format.

Data transformation might be necessary. For example, converting a PHP array into a JSON object or even encrypting and decrypting data from PHP to Javascript.

Whatever data you need to pass from PHP to Javascript, there is a very easy way of achieving the goal.

Utilising HTML forms is also a great way of passing data from the backend to the front end. Javascript can natively search and process form objects so using hidden fields are also a great way of passing data.

Try now – all new Smart Domain Generator

Use our ai-powered smart domain generator to search for a memorable name for your business idea.
The fast and easy to use smart domain name generator tool to generate innovative and unique ideas of names for your business or brand

Click here to open the Application

Leave a Comment