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 8035649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:22:12+00:00 2026-06-05T02:22:12+00:00

From time to time I have to pass some variables from PHP to JS

  • 0

From time to time I have to pass some variables from PHP to JS script.
For now I did it like this:

var js-variable = "<?php echo $php-variable; ?>";

But this is very ugly and I can’t hide my JS script in .js file because it has to be parsed by PHP. What is the best solution to handle this?

  • 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-06-05T02:22:13+00:00Added an answer on June 5, 2026 at 2:22 am

    If you don’t want to use PHP to generate your javascript (and don’t mind the extra call to your webserver), use AJAX to fetch the data.

    If you do want to use PHP, always encode with json_encode before outputting.

    <script>
        var myvar = <?php echo json_encode($myVarValue); ?>;
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to copy files that have timestamps from the time the script begins
I have some trouble achieving adequate real-time performance from my application and wondering if
Is it possible to have JavaScript compute a timestamp returned from PHP's time() function
I have a branch which I created from the trunk some time ago. Since
At this present point in time I have some code that does the following:
I have the following setup: class.staff.php This defines many variables, the one I'm working
I have a DevExpress VCL ribbon in my Delphi app From time to time
I have a WCF client that I'm having problems with. From time to time
A lot of time I have to code from home. Normally I just remote
I am coming from C# background and I have hard time figuring out how

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.