Is it okay to use PHP inside a jQuery script?

前端 未结 7 1502
误落风尘
误落风尘 2021-02-20 03:56

For example:

$(document).ready(function(){
    $(\'.selector\').click(function(){
        
    });
});
         


        
7条回答
  •  情深已故
    2021-02-20 04:18

    PHP is a "backend" language and javascript is a "frontend" language. In short, as long as the PHP code is loaded through a web server that understands PHP - the downside is that you have to inline the JS, losing caching ability (there are workarounds to parse php in .js files but you shouldn't really do this). To the user it will just look like javascript and HTML. Here's the server order:

    1. User requests page.
    2. Apache (or equivalent) notices this is a php file. It then renders all the php that are between php tags.
    3. Apache sends the page to the user.
    4. User's browser sees the JavaScript and executes it.

    Just be sure the PHP is outputting valid JavaScript.

提交回复
热议问题