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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:43:14+00:00 2026-06-11T18:43:14+00:00

Possible Duplicate: What's the best way to pass a PHP variable to Javascript? I

  • 0

Possible Duplicate:
What's the best way to pass a PHP variable to Javascript?

I have to do a function that automatically take care of passing variables from PHP to JavaScript in a simple and safe.
Currently stored in a class variable array with the values ​​of the variables, and then the idea is to extract in Javascript, but many doubts assail me what the (technically) best way to do this.

Also passes through my head several problems, especially when complex move to Javascript arrays or strings with accents, special symbols or symbols as quote or single quote, including problems with the function json_decode when work with chains with line breaks and carriage returns. (You get invalid JSON, I’ve had to do a cleaning function in PHP to avoid passing variables with these characters, but sometimes I need be)

I searched the web a lot, and I have found many answers but none terminade convince and more with the above problems.

An example:

The PHP Method (too simple):

   var JsVars;
   public function saveJsVar($key, $value) {
      $this->JsVars[$key] = $value; //Possible need to clean certain characters like /n /r
   }

The “javascript part”

<script>
   var SistemVars = SistemVars || {};
   <?php foreach ($JSVARS as $k => $var) { ?>
   SistemVars.<?=$k?> = '' || '<?php echo $var ?>'; // Dont work with arrays only plain
   <?php } ?>
</script>

Another option:

<script>
   var SistemVars = SistemVars || {};
   SistemVars = <?=json_encode($JSVARS)?> // Object, with plain and array BUT problems with certain characters.
</script>

The idea its use PHP (saveJsVar) to save plain variables and arrays, and set the vars in SistemVars object en JavaScript to use it like alert(SistemVars.ParamOne);

with your experience, what do you think is the best way to do 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-11T18:43:15+00:00Added an answer on June 11, 2026 at 6:43 pm

    The 2nd option is the better way to do it, as it saves using a foreach loop. also, json_encode is made for this purpose, so why not use it 🙂 And just FYI, <?= ...?> short tags for PHP are not used anymore Are PHP short tags acceptable to use?

      <script>
      var SistemVars = SistemVars || {};
      SistemVars = <?=json_encode($JSVARS)?> // Object, with plain and array BUT problems with certain characters.
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What's the best way to pass a PHP variable to Javascript? I
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: Best Way to Sprite Images? I have the following image that I
Possible Duplicate: What is the best way to profile javascript execution? I have a
Possible Duplicate: Best way to download a file in PHP i have been asked
Possible Duplicate: Best way to find Browser type & version? In HTML/JavaScript, How can
Possible Duplicate: best way to get the key of a key/value javascript object foo
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Best way to stop SQL Injection in PHP I need to secure
Possible Duplicate: ASP.Net:Best way to run scheduled tasks I have to make a scheduled

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.