Passing PHP JSON to [removed] echo json_encode vs echo json declaration

后端 未结 9 2250
猫巷女王i
猫巷女王i 2021-01-05 01:52

I\'m trying to create a common constants file to share between php and javascript, using JSON to store the constants. But I\'m wondering why pass the JSON from PHP to javasc

9条回答
  •  梦毁少年i
    2021-01-05 01:58

    json_encode is a function that converts a PHP array into a JSON string, and nothing more. Since your $json_obj variable is already a JSON string, no further conversion is needed and you can simply echo it out.

    To get to your $json_obj string from an array your code would have looked like this

    $json_array = array(
        "const1" => "val",
        "const2" => "val2"
    );
    
    $json_obj = json_encode($json_array);
    

提交回复
热议问题