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

LVL 1
RationalRabbitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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; ?>;
0
leakim971PluritechnicianCommented:
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

0
RationalRabbitAuthor 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.
0
Determine the Perfect Price for Your IT Services

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

Chris StanyonWebDevCommented:
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.
0
RationalRabbitAuthor 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?
0
leakim971PluritechnicianCommented:
<script>
   <?PHP echo("var Admin = " . (   (isset($_POST['Admin']) && !!$_POST['Admin']) ? "true":"false"  ) . ";");  ?>;
</script>

Open in new window

0
RationalRabbitAuthor 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
0
RationalRabbitAuthor 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

0
leakim971PluritechnicianCommented:
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

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Why do you want false and null to evaluate to true - that is totally against convention.
0
RationalRabbitAuthor 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.
0
Julian HansenCommented:
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

0
Chris StanyonWebDevCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.