Passing PHP JSON to Javascript: echo json_encode vs echo json declaration

Passing PHP JSON to Javascript: echo json_encode vs echo json declaration

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Passing PHP JSON to Javascript: echo json_encode vs echo json declaration

When building a shared constants file between PHP and JavaScript, it is tempting to treat JSON as mere text data that can be echoed directly into your HTML response. A common pattern involves storing the data in a string variable within PHP and then wrapping it in a JavaScript variable declaration using echo. For instance, if you have a PHP string like '{"const1": "val", "const2": "val2"}', you might write:

<?php $json_obj = '{"const1": "val", "const2": "val2"}'; echo 'var json = '.$json_obj; ?>

This approach works in isolation. However, relying on this method introduces significant fragility into your application architecture. The primary distinction between echoing raw JSON text versus using json_encode() lies in how the data is represented and handled when it crosses from PHP to JavaScript.

When you use echo 'var json = '.$json_obj;, you are effectively concatenating strings. This assumes that the values inside your PHP string are static, single-line strings, or simple numbers. If any value within your constants object contains a double quote character ("), a newline, or even a special regex character, the generated JavaScript becomes invalid immediately. For example, if const1 holds the value "Hello \"World\" ", the naive concatenation will break the syntax of the resulting script tag, causing a parse error in the browser.

Using json_encode() solves this by automatically escaping characters that have special meaning in JSON strings. It ensures that the output is valid JSON text regardless of the content of the values. When you wrap that output in echo, you are effectively telling the browser to treat the response as a script containing valid JavaScript object syntax. The resulting HTML looks like this:

<?php $json_obj = ['const1' => 'val', 'const2' => 'val2']; 
// Note: Using an array here, not a string, is best practice
$json_string = json_encode($json_obj); 
echo '<script>var json = '.$json_string.';</script>'; 
?>

The critical advantage of json_encode() is that it generates a JSON string literal. When you echo this inside a script tag, the browser parses it as a standard JavaScript object. You can then access properties using dot notation (json.const1) or bracket notation without worrying about quote escaping issues.

There is also a semantic consideration. If your PHP data source is an array (which is the native way to store constants in PHP), passing it through json_encode() transforms it into a JSON string. This is actually desirable because you can then pass that string directly to functions like JSON.parse() if you need to manipulate it later, or access it as a plain object if you echo it directly into a script block.

Attempting to bypass json_encode() by manually constructing the variable declaration (echo 'var json = ...') forces you to manage escaping logic yourself. This is error-prone and violates the principle of separation of concerns. PHP should handle its own data serialization, and JavaScript should consume that serialized data.

In modern frameworks like Laravel, this pattern is often handled more elegantly using JsonController or simply echoing the encoded JSON directly into a view without wrapping it in a script tag if you intend to use AJAX to fetch it. However, for inline constants, the safest path remains generating valid JSON text via json_encode() and injecting it into a <script> block. This guarantees that the resulting JavaScript is robust against edge cases involving special characters, ensuring your application doesn't crash due to a single quote in a constant name or value.

Our network