admin管理员组

文章数量:1435859

I'm a PHP phr34k addicted to JavaScript on the quest for some knowledge. If one were to include php code in their scripts, what would be the best method? I have provided some sample code as an example on how I would go about including PHP in my scripts.

Is this a valid method?

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP with JavaScript?</title>
<script type="text/javascript" src=".4.2/jquery.min.js"></script>
<script type="text/javascript" src="test.js.php"></script>
</head>
<body>
</body>
</html>

test.js.php

<?php 

$foo = 'bar';

?>

$(document).ready(function() {
    var foo = '<?php echo $foo; ?>';
    alert(foo);
});

Thanks again!

I'm a PHP phr34k addicted to JavaScript on the quest for some knowledge. If one were to include php code in their scripts, what would be the best method? I have provided some sample code as an example on how I would go about including PHP in my scripts.

Is this a valid method?

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP with JavaScript?</title>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="test.js.php"></script>
</head>
<body>
</body>
</html>

test.js.php

<?php 

$foo = 'bar';

?>

$(document).ready(function() {
    var foo = '<?php echo $foo; ?>';
    alert(foo);
});

Thanks again!

Share Improve this question asked Feb 1, 2011 at 17:25 j3ffzj3ffz 7151 gold badge6 silver badges18 bronze badges 12
  • Have you tried? (SPOILER, yes it works, but you're outputting JS through PHP, not including PHP in JS) – nico Commented Feb 1, 2011 at 17:27
  • imho, best practice is not to mix php and javascript. – Maxym Commented Feb 1, 2011 at 17:28
  • 2 @Maxym what if I want a pletely interactive page but also want people to be able to login? You'll need some way of letting the JS know who's logged in at the moment... putting it in an element and getting the contents of that isn't that much of a great idea either. – goto-bus-stop Commented Feb 1, 2011 at 17:33
  • @Reanimation: Great point. Why do work when you could just... not? – Andrew Noyes Commented Feb 1, 2011 at 17:42
  • 1 @Reanimation: you just use AJAX in those cases, allowing you to separate what's happening server-side from what is happening client-side. – nico Commented Feb 1, 2011 at 17:44
 |  Show 7 more ments

3 Answers 3

Reset to default 5

Also set the content type header in test.js.php

header('Content-Type: text/javascript');

You can also define foo inline like

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP with JavaScript?</title>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    var foo = '<?php echo $foo;?>';
</script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
</body>
</html>

test.js

$(function(){
    alert(foo);
});

I've seen people use the PHP interpreter to bine multiple JS source files before serving them to the client. That way the JS developers can benefit from having multiple files for more organized development but avoid sending multiple JS files (thus multiple HTTP requests) to the client.

However, these days there are several build scripts just for JavaScript. Sprockets, for example, allows you to automate building JavaScript files. Before that I considered it best practice to "pile" the JavaScript dependencies before hand. I wrote a simple Python script, for example, that would look for @include ments in JS source and order includes by their order of need. Probably better than wasting server time in exchange for a slight development convenience.

EDIT: Just take special care that you dump your variable data into the JavaScript properly. For example, if $foo is a string then you need to make sure that it's surrounded by double quotes. As is that code is going to go looking for a JavaScript variable called bar.

Unless you have a very bizarre situation, what you've described isn't really possible. PHP is evaluated on the server, while Javascript is sent to the user agent and executed by its Javascript engine (on the client side).

No user agents that I know of contain a PHP engine, so there's no way to have PHP executed on the client side. Besides, unless you're use some heinous escapes, the PHP will be executed by the server anyway before the Javascript is sent to the client.

In the latter example you give, the PHP gets evaluated on the server and the client is sent a Javascript file that looks exactly like:

$(document).ready(function() {
    var foo = 'bar';
    alert(foo);
});

So there's no PHP contained within the Javascript; rather, you're dynamically generating (normal) Javascript via PHP.

If this latter is what you intended, then yes - this works fine. The PHP engine doesn't know anything about Javascript, and just generates some text that happens to have a particular meaning to a JS-parsing client. So the presence of Javascript doesn't change anything on the PHP side, and since it's processed out, the (previous) presence of PHP doesn't change anything on the Javascript side.

(If you wanted your Javascript to execute some PHP on the client, however, that's fundamentally not possible.)

本文标签: Best Practices Including PHP in your JavaScript codeStack Overflow