If I write php code inside a javascript method does the php code execute if the method of the javascript is not called at all ?
3
-
Yes if javascript code will written in PHP fileamit– amit2015-12-29 12:21:53 +00:00Commented Dec 29, 2015 at 12:21
-
2Welcome to SO. Please read What topics can I ask about and How to ask a good question And the perfect questionRiggsFolly– RiggsFolly2015-12-29 12:26:01 +00:00Commented Dec 29, 2015 at 12:26
-
PeterssonJesper gave you a good answer. Also, the php will always run on the server before the javascript on the client runs ... or does not run.Yogi– Yogi2015-12-29 12:33:49 +00:00Commented Dec 29, 2015 at 12:33
Add a comment
|
5 Answers
PHP is a server side scripting so that is always run let me explain it with code.
There is no issue for PHP script code work with js. just you need to do this in .php file only.
For example:
example.php
<?php
//Initialize PHP variable value...
$htmlString= 'Hello testing'; ?>
<!-- Use PHP variable into JS -->
<script type="text/javascript">
// notice the quotes around the ?php tag
var htmlString="<?php echo $htmlString; ?>";
alert(htmlString);
</script>
And this will prompt message "Hello testing".
Hope this help you!
Comments
If you want to write php code inside javascript then you can write like following :
var message_from = <?php echo $_SESSION['user']['user_id'] ?>;
These links may help you:
PHP code inside a Javascript's "document.write()"