Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:58:06+00:00 2026-05-26T14:58:06+00:00

I’m trying to mix php and javascript, because my javascript needs to access some

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:58:06+00:00Added an answer on May 26, 2026 at 2:58 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.