First lesson is to understand how, where and when things are executed. There is no such thing as "now in javascript / now in php". PHP runs on the server. Javascript runs in the browser. As such, PHP may generate javascript code, but it is not aware of what happens in the browser, nor is anything else on the server. The browser receives output from the server, possibly generated by php, but apart from the output the browser gets, it is not aware of what happens on the server.
Furhtermore, you cannot, serverside, know if something is executed in the browser or not. For example
printf('<script type="text/javascript">alert(%s);</script>', "'hello'");
sleep(2);
echo 'No way to say if that alert box has been shown yet!';
Most (all?) web servers use buffered output. For the most part, you can probably asume the buffer is 4096 or 8192 bytes. As such, it's entirely possible that the echo statement is executed on the server before the browser even gets to see output from the printf statement.
And another piece of interesting information
<?php
echo "Send output to a browser document. Let's call it Document 1";
?>
<script type="text/javascript">
document.write('hello');
/* Take me someplace else. I.e. a new document is created, the old is discarded.
* Let's call this new document Document 2
*/
location.href='http://example.com';
</script>
<?php
echo "I'm still sending output to Document 1, which no longer exists!";
echo 'Or, as discussed before, Document 1 may not even have been created yet.';
echo 'All of this may still be in the webserver buffer. Either way, the user will never see this.';
?>
Also note that
include (city + 'gallery.php');
will
1. Generate a notice (undefined constant city, assumed 'city'). Make sure php.ini sets error_level = E_ALL and that log_errors points to a logfile which your webserver user can access. You may possibly want to enable display_errors in a development environment as well.
2. Tries to include the file '0', because
city: undefined constant, assumed 'city', means that since this constant has not been defined (no, it's not a javascript variable - as far as the server is concerned, there are no javascript variables), and then PHP goes on to assume you mean the string literal 'city'.
+ is NOT used to concatenate things in PHP, that's the javascript way of doing it. + is used for addition, and to add things, we need numbers. 'city' is thus converted to 0, and so is 'gallery.php', which gives 0 + 0 = 0. Finally, since include requires a string, 0 is converted to '0'.
3. generate a warning (require(0) failed to open stream). Well, at least unless you have a file named '0'.