Embed PHP code inside JavaScript? Good practice?


I’m using PHP code inside my JavaScript code. Something like this:

    alert('<?php echo $var; ?>');

I’d like to know if this is a good practice and if it is valid as I’m going to submit my plugin to CodeCanyon with that code. Please guide.

Thank you.

very bad practice…

Is it acceptable if we use this coding style in our plugin? or will it be rejected?

I’m pretty sure it will be rejected because they pay attention at code quality during review

It’s an absolutely wrong idea to use server code inside frontend, do not do it!!!

1 Like

It’s not good practice, specially when there is a better solution.

For WP, you can use wp_localize_script. Look it up.

For non-WP there are other solutions, easiest being using AJAX.