PHP cannot communicate directly with the user. Everything PHP does happens before the user ever gets ANY part of the page. So all decision have to be made before then.
To do the javascript way, I just did something like:
if (condition met) echo "onLoad=myjavascript_function";
as PHP is printing out the BODY tag.
check places like hotscripts for the the javascript code