prakash
prakash

Reputation: 184

PHP, HTML, Javascript execution order

I have a test.php file and this file contains some PHP code, HTML elements and some internal JavaScript and some external JavaScript include.

I want to know which is first to load or execute.

PHP or HTML or JavaScript? I want to know execution order.

Your answers are greatly appreciated and very helpful to me and others also.

Upvotes: 14

Views: 9781

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173662

Pragmatically speaking, this is the typical order:

  • PHP runs first and constructs the page.
  • The browser loads the resulting HTML (any JavaScript found gets executed immediately)
  • Any JavaScript that was tied to the DOM ready or load event gets executed once the whole HTML is read and all objects are loaded respectively.

Upvotes: 16

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

PHP will execute first, then HTML and finally javascript.

  • You send request to server, server executes your script
  • Then returns rendered html to browser, browser parses HTML(inline javascript executed)
  • Finally executes external included javascript files, one by one in order they are included.

Upvotes: 5

Related Questions