When and Where Does JavaScript Run, How About PHP? How to Combine the Two

When and where does JavaScript run, how about PHP? Can I combine the two?

The short answer is No. You cannot run PHP functions from JavaScript[With the exception of AJAX], nor can you run JavaScript functions from PHP. The two run times are separate.

How?

To understand how JavaScript and PHP cooperate, you should first understand the basics of the HTTP protocol that powers the web.

HTTP Sequence

The diagram above demonstrates the basics of the HTTP protocol. The user (you) asks the client (your browser) to fetch you a page. The browser will then ask the server (Google, in this example) for the page. The server will reply with an HTML page, the client parses that page, and asks for the images, fonts and any other resources needed to load the page correctly. The client then presents the completed page to the user.

So where does JavaScript come in?

JavaScript is run in the Client (i.e. the browser). So JavaScript runs after the response from the server has arrived. Let's add that to our diagram.

Sequence with JavaScript

JavaScript scripts start running as soon as they are loaded, and will continue to run if they have event listeners waiting for events from the user (like clicking, typing, or moving around).

Where does PHP fit in?

PHP runs on the Server, the web server (Which is a program responsible for serving web content) will run PHP according to its configuration. PHP will process the input from the web server, and return output. That output is served back to the client.

Updated diagram:

Sequence with JavaScript and PHP

As you can see, the PHP execution does not persist. It is executed, and then ends once the response is sent.


As you can see, there is no overlap between the PHP execution and the JavaScript execution, so it isn't actually possible to make a function on one of them to work based on input from the other.

But.. but.. I've heard of AJAX!

AJAX is merely causing another HTTP request from JavaScript. You can call it a way to use PHP functions from JavaScript, but it's actually not quite that.

AJAX Sequence

As you can see, with AJAX, JavaScript will send a request to the server, which will invoke PHP, PHP will run again, like in a normal request (PHP doesn't necessarily knows that this is even an AJAX request!) and the server returns the response back to JavaScript, which uses it to do stuff.

In this case, there is an overlap between the time PHP runs and the time JavaScript runs, since JavaScript invoked a request.

Also see:

  • How to pass variables and data from PHP to JavaScript?

How to combine PHP and Javascript together?

I think you are confused, you are trying to use a variable of javascript in php.

You cannot do this:

<?= $latest[?>i<?=]->file; ?>'

Which expands to:

<?php
$latest[
?>
i
<?php
]->file;
?>

how can you possibly know the value of i, if i is a variable generated in the client side?, do not mix the ideas, i is defined in the browser and the php code is on the server.

Combining and Compressing multiple JavaScript files in php

You can use jsmin-php

Their sample code is:

require 'jsmin-1.1.1.php';

// Output a minified version of example.js.
echo JSMin::minify(file_get_contents('example.js'));

You may easily join several js files by doing something like:

require 'jsmin-1.1.1.php';

// Output a minified version of example.js.
echo JSMin::minify(file_get_contents('example.js') . file_get_contents('example2.js'));


Related Topics



Leave a reply



Submit