0

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 file Commented Dec 29, 2015 at 12:21
  • 2
    Welcome to SO. Please read What topics can I ask about and How to ask a good question And the perfect question Commented 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. Commented Dec 29, 2015 at 12:33

5 Answers 5

5

The PHP code runs on the server, meanwhile the Javascript code runs on the client. The PHP code will always run, no matter of what Javascript you try to surround it with.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2

Yes dear, Your PHP code will run always. as PHP is server side wheres JavaScript is Client side. so when you called page Both method run simultaneously. so no matter if you defined PHP code inside JavaScript.

Comments

2

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()"

Can I write PHP inside JavaScript inside PHP?

How to write the php code in js file

Comments

1

If it is inside a PHP tag and inside a PHP file, it will run the code, no matter what or where it is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.