简体   繁体   中英

PHP with Javascript - What is the scope of PHP variables inside a <script></script> tag?

I'm trying to mix php and javascript, because my javascript needs to access some php variables when the page loads. I'm having trouble understanding something, and I've made a code example.

<?php
session_start();
$test = 100;
$_SESSION['test'] = 200;
?>
<html>
<head>
<title>Test This Out</title>
</head>
<body>
<h1> Testing Javascript and PHP Mixed </h1>
<p>
<?php 
echo("The value of \$test is $test and the value of \$_SESSION['test'] is ");
?>
</p>
<p>
<script type="text/javascript">
<?php 
session_start();
if(isset($test)) echo("document.write('Non-session variable exists and is $test <br />');"); 
else echo("document.write('Non-session variable does not exist<br />');");
if(isset($_SESSION['test'])) echo("document.write('Session variable exists <br />');"); 
else echo("document.write('Session variable does not exist<br />');");
?>
</script>
</p>
</body>
</html>

Output looks like:

Testing Javascript and PHP Mixed

The value of $test is 100 and the value of $_SESSION['test'] is

Non-session variable exists and is 100
Session variable exists 

So I'm trying to understand what types of php variables are available to the script. It appears that the variable I've called $test is available, but even though it knows that

$_SESSION['test'] 

exists, if I try to output this value (in the exact same way I output the

$test

variable), the whole system hangs.

My questions are: 1. Can the php used in the javascript "see" variables I've defined earlier on in the page? 2. Why is my attempting to print the

$_SESSION['test'] 

variable making the whole thing crash (nothing at all is rendered)? 3. Is the second

session_start(), 

the one in the script, necessary?

Thanks for any help.

PHP runs on the server and Javascript runs on the client. They have utterly different execution environments, and execute at utterly different times. PHP variables have PHP's variable scoping, and JS rules to not enter play, as PHP isn't executing in a "JS environment". That's impossible.

You can have PHP GENERATE JS code/variables, but those variables do not get processed/executed/validated by the JS engine until AFTER the page has been generated by PHP and sent to the client browser.

You can also have JS "send" variables to PHP, but that's done via an AJAX call, and again, the JS runs in the client browser, and the PHP runs on the webserver. The AJAX calls is just a regular HTTP request as far as PHP is concerned, and when a response comes back to the JS running in the browser, it's like any other HTTP response from a server.

Your second session_start() is going to fail - sessions must be started BEFORE any output is generated by the server, as the session ID token is sent to the client browser as a regular HTTP cookie. PHP automatically sends the full HTTP headers whenever the script produces any output, and you've output the start of an HTML page and some JS code before the second session_start() is executed.

If you have something like this:

<script type="text/javascript">
var jsvar = <?php echo $phpvar; ?>;
</script>

As far as the PHP engine is concerned, that's perfectly valid PHP code, and it'll ouput whatever the contents of $phpvar is at that point. But remember - PHP is running on the server - the PHP engine has no idea whatsoever of the environment you're outputting that variable into. It doesn't know it's inside a javascript block, it just knows it's supposed to output a variable's contents. If $phpvar isn't defined at that point, you'll actually be producing:

var jsvar = ;

and that's a syntax error. It is entirely up to YOU to ensure that you're using PHP to generated valid JAVASCRIPT code.l

The code, as you have it now is procedural , meaning that it will run from line 1 until EOF in sequential order, b/c you are not making any custom function calls etc.

You do not need the 2nd session_start(), the first one is enough to initalize the session.

Never heard of a system hanging when trying to var dump a session variable. Maybe try this format (single quotes instead of double quotes):

echo('The value of $test is ' . $test . ' and the value of $_SESSION[test] is ' . $_SESSION['test']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM