How can I pass a PHP variable to javascript?

前端 未结 4 1087
失恋的感觉
失恋的感觉 2020-12-21 05:44

I want to use a PHP variable as a javascript variable - specifically I want to user the PHP variable session_id(); and use this as a javascript variable.

&l         


        
4条回答
  •  既然无缘
    2020-12-21 06:37

    The best method i can think of looks like this:

    
    
    

    PHP's json_encode-function is always producing valid JavaScript, which is not ensured if you are simply outputting random values. If you decide not to use json_encode(), you should at least enclose the php-value with quotes to prevent syntax errors. Be aware of escaping!

    
    
    

提交回复
热议问题