最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript jQuery inside PHP function - Stack Overflow

programmeradmin0浏览0评论

This is a theoretical question. My question is whether a jQuery function or script can be written inside a PHP function. E.g.

<?php function phpfunc(){
$a=10; ?>

<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>

Is this possible and legal?

This is a theoretical question. My question is whether a jQuery function or script can be written inside a PHP function. E.g.

<?php function phpfunc(){
$a=10; ?>

<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>

Is this possible and legal?

Share Improve this question edited May 14, 2014 at 14:43 Niet the Dark Absol 325k85 gold badges473 silver badges599 bronze badges asked May 10, 2014 at 19:28 HackerManiacHackerManiac 2431 gold badge4 silver badges20 bronze badges 3
  • 1 yes, that will work fine. Just echo $a instead of doing nothing with it. – Bryan Commented May 10, 2014 at 19:34
  • This will work .and possible – Babu Commented May 10, 2014 at 19:36
  • @user3187838 the answer your selected doesn't answer your question, Edit your question or review the answers – Patrick Commented May 10, 2014 at 21:01
Add a comment  | 

5 Answers 5

Reset to default 4

Yes. It is possible and Legal one too. we generally use the same when we require any server side value to be set on client-side on runtime.

Hope this answers your query.

Thanks Much!

When php code is interpreted by the sever writing something like:

<?php 

function foo()
    {

        <script type=text/javascript> ... </script>

    }

?>

As part of the code in <?php ?> is interpreted as php and string inside the function doesnt represent any of php functions

You can echo javascript code (or any content of a HTML document) through your php code like:

<?php 

function foo(){

echo "<script type=text/javascript> alert('it works!)'; </script>";


} ?>

so when you execute the function, you wil add the javascript to the document by echoing it and therefore execute it.

You can also use php variables to echo variables to javascript like:

<?php 

function foo(){

echo "<script type=text/javascript> alert('{$phpVariable}'); </script>";


} ?>

or

<?php 

function foo(){

echo "<script type=text/javascript> var variableFromPHP = {$phpVariable}; </script>";


} ?>

Yes, it's okay to do that. No, it's probably not a good idea. But there's nothing really stopping you.

Just be aware that if your variable happens to have a ' in it, you'll get messed up.

So, whenever you want to pass a variable from PHP to JavaScript, be sure to use json_encode:

<script type="text/javascript">
    alert(<?php echo json_encode($something); ?>);
</script>

Note that there's no quotes in the JavaScript part - json_encode will add quotes if needed. This can be used to pass almost any kind of variable.

The short answer is no(edit : yes).

javascript is executed on the client and only on the client.

You can however echo javascript to the client.

So something like this :

$JSfunction = "<script>alert('This is working')</script>";

can be echoed to the page by doing echo $JSfunction;

edit :

Since you didn't mention where that function is located, I assumed you meant the PHP function on the server side.

To be clear, If it's written on the html page itself, it's perfectly fine and can be done.

complete answer

<? function phpfunc(){
$a=10; ?>
<script>var a ='<?php echo $a ?>'</SCRIPT> <?php } ?>

<?php phpfunc() ?>
<script>console.log(a);</script>

You must echo that $a

Yes you may use it like in normal html page. Use script include inside echo inside the php script.

You may use it outside the php script normally. Use container tag for the same in the php page.

I hope it would help.

发布评论

评论列表(0)

  1. 暂无评论