We help IT Professionals succeed at work.

Sending true/false variables to JavaScript via PHP

RationalRabbit
on
89 Views
Last Modified: 2018-10-04
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

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019

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.
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019

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.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.