As others have mentioned, PHP runs on the server - Javascript runs on the client. I thought that you might appreciate some more detail into what this means, and why there's no 'easy answer' to how to do this.
The server is the computer on which the code is being stored.
The client is the browser you are viewing the resultant pages upon.
This might be on the same computer, especially while testing, but they are still considered to be separate entities.
When running PHP + Javascript apps:
1: The browser makes a request to the server.
2: The server pulls up the relevant PHP, crunches it, and inserts it into the html in the appropriate places.
3: The resultant data (html + javascript) is sent across the internet to the browser, which receives it.
4: The browser displays the raw data (as html).
5: The browser crunches the javascript and changes the html accordingly.
6: Additional activity on the page can cause constant repetition of #4, or a return to #1.
Now, the reason people are recommending Ajax calls is as follows: An Ajax call will make a request to the server 'in the background'. It doesn't cause a page reload, therefore step #4 is skipped. It simply receives the relevant data, processes it, and makes changes to the already existing html as needed.
But what this also means is that you can send a request to the server to please run a specific section of PHP - to save data to the database, to request a new piece of data, to run a calculation ...
However. Each type of request needs its own new access point in the PHP - a PHP page without html, if you will. Json is usually the data format of choice for transfer, though xml (the X in AJAX) is also still used.
This is not a simple act, and causes a lot of complication in the code ... but because PHP can build javascript, but javascript cannot directly affect the PHP, making the transfer of data usually a one-way street, this is the only way to bring javascript results back to the server so that PHP can handle it. You cannot just 'change a PHP variable' - you need to start the PHP from scratch once again, and initialize all relevant variables, in order to get the proper result. You're not picking up where you left off, you're making a brand new request.
Hope this helps!