6

Possible Duplicate:
transfer a Variable from php to js

This might seem trivial. I am setting a PHP variable's value as false. Then after some processing I am outputting some JavaScript variables in a script. This is the code

$a = true;
$b = false;
echo '<script type="text/javascript">
          var a = '.$a.';
          var b = '.$b.';
      </script>';

When the script finishes I get this output:

var a = 1;
var b = ;

So I get syntax error in JavaScript. Now the question is, how to have those values as true boolean values in JavaScript as well?

Intended output:

var a = true;
var b = false;

I don't want string like 'true' or 'false'...or 1 and 0, but boolean true and false only. Any help regarding this, also with some explanation as to why PHP behaves this way?

Community
  • 1
  • 1
Shades88
  • 7,934
  • 22
  • 88
  • 130
  • 3
    Look at the logic of JavaScript booleans http://www.quirksmode.org/js/boolean.html – Ron van der Heijden Jul 25 '12 at 06:35
  • There are similiar questions on SO - http://stackoverflow.com/questions/4948663/php-get-bool-to-echo-false-when-false - http://stackoverflow.com/questions/2249235/is-there-a-way-to-get-true-false-values-from-a-boolean-in-php - http://stackoverflow.com/questions/11512705/print-false-in-php And all do the same by using strings instead. – sascha Jul 25 '12 at 06:35

6 Answers6

9
echo '<script type="text/javascript">
          var a = '.($a?"true":"false").';
          var b = '.($b?"true":"false").';
      </script>';

I suppose, You cant simply echo true/false to get the word, You need to convert it to string.

dpitkevics
  • 1,238
  • 8
  • 12
  • 1
    right and when you echo it in js, the js value will hold a boolean, not a string because it won't echo with quotes around it – dano Jul 25 '12 at 06:35
4

Use json_encode.

$a = true;
$b = false;
echo '<script type="text/javascript">
          var a = '.json_encode($a).';
          var b = '.json_encode($b).';
      </script>';
xdazz
  • 158,678
  • 38
  • 247
  • 274
3

One more way to do it would be using var_export()

echo '<script type="text/javascript">
          var a = ', var_export($a), ';
          var b = ', var_export($b), ';
      </script>';
Adi
  • 5,089
  • 6
  • 33
  • 47
2

Encode as JSON.

$ php
<?php
echo json_encode(true) . "\n";
echo json_encode(false) . "\n";             
true
false
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

I suspect this would work and be easy to add:

($val ? "true" : "false")
Marcus Johansson
  • 2,626
  • 2
  • 24
  • 44
1

I would use json_encode() on the php side, and JSON2 on the js side to transfer variables. (json2 is included in most js frameworks)

<?php
$js_vars = json_encode(array(
    'a' => true,
    'b' => false,
));
?>
<script>
   JS_VARS = JSON.parse('<?php print $js_vars?>');
   console.log(JS_VARS.a, JS_VARS.b);
</script>

Works for single variables too, but I would recommend to group your variables so they won't pollute the javascript's global object.

complex857
  • 20,425
  • 6
  • 51
  • 54