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

PHPJavaScript

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Chris Stanyon

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; ?>;
leakim971

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

ASKER
RationalRabbit

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Chris Stanyon

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.
ASKER
RationalRabbit

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?
leakim971

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

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
RationalRabbit

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
ASKER
RationalRabbit

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

leakim971

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
RationalRabbit

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
RationalRabbit

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Julian Hansen

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

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Julian Hansen

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

Chris Stanyon

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.