How do I match PHP and JavaScript values


I’ve picked up a PHP value at random and I want to match it with the value in my Javascript array set. Is there anyway to do this?
Please advice.


I’m not sure I follow what you’re trying to do.

I guess a quick-hack could be to have PHP echo some script type=“text/javascript” tags with the variable being defined.

// Make random value, x
bla bla bla

// Pass on to Javascript
echo('<script type=“text/javascript”>var x = ’ . $x . ‘;</script>’);


That what I want to do. But how do I check with something like this on the javascript side:

if (x == $x) {
// continue

I get it. sorry for stupid question.

That can’t be done. When PHP runs, it builds on the server side the content of the HTML page and then sends the HTML content to the client. When the client runs it runs any javascript that is on the page. There is no communication between PHP and JavaScript.

It can be possible to use Ajax techniques to send information to another PHP script, and at some later time receive a response, but that tends to be relatively slow. There can be better techniques, if you can share with us what you want to accomplish.

Thanks for suggestion.

What I am trying to do is a live event (not chat). As you said, using Ajax is slow. So I gather who is online with JavaScript and I want to show them on front page. However, I only want people I set as featured to be appeared on the front page. I’ve picked them at random of limited number.

As you can see, the problem is I want to match the IDs I picked with PHP and the IDs I picked with JavaScript to make sure that they are both featured and online.

That sounds like a PHP database solution then. Have PHP get from the database a list of featured people who are currently logged in, and use that information when creating the page.
That’s how I’d approach the problem.

It’s more complicated. It’s a real-time platform and I found JavaScript is far more effective than PHP (PHP can’t do time interval). So the heart of the app is JavaScript and I use PHP just because JavaScript can’t do it.

Anyway, with Shaun suggestion I found it’s easy to achieve this.