Sending true/false variables to JavaScript via PHP

RationalRabbit
RationalRabbit used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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; ?>;
leakim971Multitechnician
Top Expert 2014

Commented:
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

Author

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Author

Commented:
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?
leakim971Multitechnician
Top Expert 2014

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

Open in new window

Author

Commented:
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

Author

Commented:
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

leakim971Multitechnician
Top Expert 2014

Commented:
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

Author

Commented:
You are using a PHP function. as I mentioned earlier, I am using a javascript function, which I intend to use for other JavaScript.
<script>
function VarEval(Var)
   {
      if(Var == null || Var === false || Var === 0){return false;}
      else{return true;}
   }
var Admin      = VarEval(<?PHP echo($_POST['Admin']); ?>);
</script>

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Why do you want false and null to evaluate to true - that is totally against convention.

Author

Commented:
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.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial