Trying to avoid too much information, but here is the full cycle of PHP when used with Apache on Linux.
Prerequisities
A common server setup is called the LAMP stack which stands for Linux, Apache, Mysql, and PHP. Generally you can find dozens of ready to use LAMP stack setups along with guides for using them so in context of your question, all you need to focus on is Apache and PHP.
Stage 1 - Delegation
When a web browser contacts a web server running Apache with PHP, the first step is for Apache to find the desired content. Say you go to www.mywebsite.com/hello.php, Apache is going to see you're looking for a file called hello.php. At this point, because of the suffix (.php) , Apache knows that this file needs to be interpreted by PHP so it delegates processing to the PHP interpreter.
Stage 2 - Setup
The hand over from Apache to PHP includes a slew of headers that tells PHP the following: what kind of transaction is being processed ( GET/ POST/ PUT/ DELETE ), the IP address of the incoming request, the browser's user agent ( Firefox, MSIE , IPhone, etc ), if there are cookies. More importantly, Apache hands to PHP the path to hello.php file on the server.
Stage 3 - Processing
Depending on configuration, PHP might need to do some basic house keeping ( setting itself up ) but under ideal conditions its ready to go and opens hello.php Part of PHP is a module called a lexer which looks at hello.php and figures out how to deal with the file. With the example provided a very simple example might look like:
- T_STRING = "<html>\n\t<head>\n\t"
- T_OPEN_TAG;
- T_ECHO;
- T_STRING = "HTML Title from PHP ";
- ;
- T_CLOSE_TAG;
- T_STRING = "\t</head>\n\t<body>\n\t<h1>hello world!</h1>\n\t</body>\n</html>"
Note, I've made up most of the T_ codes, but they're pretty close to what is real.
Line 1 - PHP knows it's outside of what called scope, so it immediately passes this entire string to Apache, the webserver. Apache will most likely then pass this entire string onto the web browser.
Line 2. - T_OPEN_TAG tells PHP it's entering into the PHP scope and waits it's first instruction.
Line 3 - T_ECHO tells PHP it's going to make an echo statement, so it's rules then kick in to look for an expression or string to output.
Line 4 - As luck would have it, the next token is a string, so PHP now knows it's going to echo "HTML Title from PHP "
Line 5 - The ; tells PHP that the echo statement is complete and more importantly this is syntactically correct... so PHP hands the string "HTML Title from PHP" to Apache which passes this onot the browser.
Line 6 - The Close ?> tag tells PHP that it is leaving the PHP language scope, so it goes back to a much simpler set of rules
Line 7 - Like line 1, the entire string is passed to Apache to be passed to the web browser
At this point, PHP reaches what's called EOF or end of file and knows it's done processing the file hello.php. It does cleanup work and then tells Apache it is done.
Finalization
At this point the request has been mostly fulfilled, Apache will most likely hang up on the web browser, sending notification that all content is complete.
Let me know if you have any questions or need any pointers on where to go next. Also note there is a LOT of details left out of here for brevity/sanity sake, but for just understanding how birds eye view of PHP's relationship to the web browser & web server, this should be enough to get started.
Demonstration script
$test = 'Hello world <' . '?' . 'php echo \'this is in scope\'; ?' . '> and we\'re done';
$tokens = token_get_all($test);
print_r($tokens);
The output will be real world token string PHP generates. Each token can be either a string or a three element tuple/numeric array where index 0 == the Token ID, index 1 == raw string, and I can't for the life of me remember what the third element is. Use token_name if your curious what each token's name is.