use html localStorage in PHP -
my main question is, why below code prints out:
false boolean value true
i expect variable "boolean" value false
i want store data in javascript , later use in php, possible?
<!doctype html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>storage test?</title> </head> <body> <script type='text/javascript'> localstorage.save = 'false'; </script> <?php $boolean = "<script type='text/javascript'>" . "document.write(localstorage.save);". "</script>"; echo $boolean; if($boolean = 'true'){ echo "<p>boolean value true</p>"; } else { echo "<p>boolean value false</p>"; } ?> </body>
like said, you're not comparing, assigning because of single equal sing =
in if
statement.
next cannot directly read localstorage php. if had double equals ==
compare, still outout boolean value true
.
that because put string
inside $boolean
:
$boolean = "<script type='text/javascript'>document.write(localstorage.save);</script?";
you're not evaluating javascript code that.
when php variable contains something, wether string or number etc. evaluate true
inside if
statement. unless value either false
, 0
or null
.
to compare real boolean value have use explicit compare. 3 equal signs ===
.
if ( $somebool === true ) { // stuff }
but no, cannot directly localstorage value js php. you'd need ajax call pass php. , think you're trying do.
Comments
Post a Comment