Link to home
Create AccountLog in
Avatar of RationalRabbit
RationalRabbit

asked on

Sending true/false variables to JavaScript via PHP

I'm trying to figure the best comparison operators to use, as well as the best expressions, when transferring PHP variables to Javascript.
I wrote the following, but I do not know if I am missing anything, using the correct operators, or whether it should be reversed (IE: Comparisons for true with false as the default)

   
function VarEval(Var)
   {
      if(Var == null || Var === false || Var == "0" || Var === 0){return false;}
      else{return true;}
   }

Open in new window

 
Variable Example
 
  var Admin      = VarEval(<?PHP echo(json_encode($_POST['Admin'])); ?>);

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Can't really figure out what you're trying to achieve here. If you just want to get a true / false value, then why not just do the comparison in PHP:

var admin = <?= $_POST['Admin']; ?>;

This assumes that $_POST['Admin'] is a boolean. If it's something else, then you can easily run a check in PHP:

var admin = <?= ($_POST['Admin'] == "Something") ? true : false; ?>;
with :
function VarEval(Var)
{
      return ( isset(Var) && !!Var ) ? "true" : "false";
}

Open in new window


<script>
<?php
     echo "var Admin = " . VarEval($_POST['Admin']) . ";";
?>
</script>

Open in new window

Avatar of RationalRabbit
RationalRabbit

ASKER

Sorry, I didn't designate that my function is a JavaScript function, and I'd like to keep it that way.

Most of the time (but not always), the variables are true or false. So this presented a problem when I tried to do
<script>
   var Admin = <?PHP echo($_POST['Admin']) ?>;
</script>

Open in new window

which produced an error.
I suppose, as you stated, when I know it's going to be a true/false variable, I could do it as
<script>
   <?PHP echo("var Admin = " . $_POST['Admin'] . ";");  ?>;
</script>

Open in new window

But I'm kind of looking for a universal JavaScript function I can use in various JavaScript situations, no matter what the variable is, 1, 0, "1", "0", true, false, null, undefined.
Right. If your PHP variable is a boolean (true / false) then you'll need to coerce that into a true false that javascript can understand:

var admin = <?= $_POST['Admin'] ? "true" : "false"; ?>;

Now when the page renders, you will have one of 2 lines:

var admin = true;
var admin = false;

If you don't explicitly echo "true" or "false", then PHP will either echo out "1" for true or nothing for false, which would lead to an error:

var admin = 1;
var admin = ;

And as for a universal function, I really wouldn't recommend that approach. It's likely to cause bugs in your code. For example, does "1" represent a boolean true, or the value 1. Does "0" represent a string of 0, a numeric zero, or boolean false.
The problem here is that if PHP returns a null, the JavaScript will resolve to true.

"And as for a universal function, I really wouldn't recommend that approach. It's likely to cause bugs in your code. For example, does "1" represent a boolean true, or the value 1. Does "0" represent a string of 0, a numeric zero, or boolean false. "

Yes, but suppose the variable being evaluated is a string. How would you suggest to handle it?
<script>
   <?PHP echo("var Admin = " . (   (isset($_POST['Admin']) && !!$_POST['Admin']) ? "true":"false"  ) . ";");  ?>;
</script>

Open in new window

With this
<script>
   <?PHP echo("var Admin = " . (   (isset($_POST['Admin']) && !!$_POST['Admin']) ? "true":"false"  ) . ";");  ?>;
</script>

Open in new window

0 = false
1 = true
empty = false
false  = true
true = true
null = true
Thanks guys. I think I'll stick with what I have, but without the json_encode(). It works, in all instances, although I don't know that the "0" is necessary.
   
function VarEval(Var)
   {
      if(Var == null || Var === false || Var == "0" || Var === 0){return false;}
      else{return true;}
   }

Open in new window

 
Variable Example
 
  var Admin      = VarEval(<?PHP echo($_POST['Admin']); ?>);

Open in new window

null or not set : http://php.net/manual/en/function.isset.php 
false, "0" or 0 or false : (bool) or the double not !!
function VarEval(Var)
   {
       return isset(Var) && !!Var;
   }

Open in new window

You are using a PHP function. as I mentioned earlier, I am using a javascript function, which I intend to use for other JavaScript.
ASKER CERTIFIED SOLUTION
Avatar of RationalRabbit
RationalRabbit

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Why do you want false and null to evaluate to true - that is totally against convention.
I don't. I was showing the results of one person's suggestion. I collapsed it - First time I've seen that collapse selection. I thought it was just my viewing that was affected. I don't like that. I'll un-collapse if I can.
Trying to figure out why you are doing such an elaborate solution

Falsy values are false, 0, null and '' so any of these values will evaluate to False without you have to expressly check for each one.

Secondly if you are doing a return out of the if you don't have to use an else - in my this is the preferred way of handling such a situation.
...
if (condition) return false;
return true;
...

Open in new window


If you want to transform a flasy / truthy value expressly into true / false then

var Admin = !!variable;

Open in new window

Hmmm. Seems like a very odd solution to a problem that doesn't really exist.

You have your variable in PHP and you're using PHP to output that variable, and yet you still want to send it through a JS function to determine true or false. Like I said before, you're likely to introduce bugs at some point. If the PHP variable isn't set, then currently you'll get a PHP error. Also the string "false" will equate to true. The value of -1 will equate to true. An array variable will throw an error.