zlloyd
zlloyd

Reputation: 111

In what order does a system reads its code when having multiple programming language?

In what order does a system executes its code when having multiple programming language? Let's say I have this system with codes arrange like this

|
HTML1 - 1st HTML code here
|
PHP1 - 1st PHP code here
|
HTML2 - 2nd HTML code here
|
PHP2 - 2nd PHP code here
|
HTML3 - 3rd HTML code here

does it read from top to bottom like this:
Execute HTML1 -- PHP1 -- HTML2 -- PHP2 -- HTML3

OR

there is some hierarchy when executing the codes?
Like Execute first all PHP codes then HTML codes follows?

The purpose of this question is in order for a beginner like me to understand how codes are executed so that I can plan on how my code will flow

Upvotes: 1

Views: 71

Answers (2)

deceze
deceze

Reputation: 522597

The same system rarely interprets PHP and HTML, it does either or.

It's a little hard to guess what exactly you're talking about, but I'm assuming you're talking about a file like this:

<html>
   ...

   <?php echo 'foo'; ?>

   <p><?php echo 'bar'; ?></p>

   <script type="text/javascript">
       alert('baz');
   </script>
</html>

In this case, each "language" is handled by a different system entirely.

  1. The client sends an HTTP request to the web server.
  2. The web server fires up PHP.
  3. PHP interprets and runs all <?php ?> snippets (from top to bottom) and completely ignores anything else.*
  4. The web server returns the processed file to the client, which now does not contain <?php ?> snippets anymore. It returns whatever PHP spit out after it was done.
  5. The client parses the HTML to visually build a website out of it.
  6. Once the HTML is parsed, the <script> snippets are executed by the client's Javascript implementation, if it has such a thing.

A visual reminder, taken from https://stackoverflow.com/a/13840431/476:

                    |
               ---------->
              HTTP request
                    |
+--------------+    |    +--------------+
|              |    |    |              |
|    browser   |    |    |  web  server |
| (Javascript) |    |    |  (PHP etc.)  |
|              |    |    |              |
+--------------+    |    +--------------+
                    |
  client side       |      server side
                    |
               <----------
          HTML, CSS, Javascript
                    |

* By "ignore" I mean it just passes it right through as output, it does not interpret it as PHP code.

Upvotes: 6

Jojo
Jojo

Reputation: 1913

Basically it's PHP first and then HTML.

This is because PHP code is executed server-side. It usually operates on some data, generates some HTML markup and then all of it is being sent to the client. When client receives the generated markup then his browser parses and displays it.

Also all of the CSS and JavaScript is executed within the clients browser.

Upvotes: 1

Related Questions