Just wondering, does “typeof” javascript verification (you know, what you do to check for undefined variables in javascript) not work the same way with PHP vars?
I do
alert(typeof <? echo $_SESSION['thing'] ?>);
and this doesn’t even execute (I’d figure it would at least pop up as blank)
But if I do
alert(<? echo $_SESSION['thing'] ?>);
the alert says “undefined”
So I’m wondering if the correct way to check for undefined PHP vars using javascript is just to echo them? If it’s not please correct me, but if it is I thought it’d be good to share.
Thanks
There’s no such thing as PHP variables in JavaScript.
What PHP is doing is “composing” the output text of the JavaScript.
Hit “View source” and look at the final text that PHP put together.
Adding to the answer @Chief17 gave you:
Be sure to either output quotes from PHP or quote the value in javascript if it’s a string. Otherwise it will likely cause more errors.