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.
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.
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:
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.
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
Fastest Way to Convert JavaScript Nodelist to Array
Switch Statement for Multiple Cases in JavaScript
How to Dodge Jquery Promises Completely When Chaining Two Async Jquery Functions
Recursively Looping Through an Object to Build a Property List
Jquery's .Click - Pass Parameters to User Function
JavaScript Date Regex Dd/Mm/Yyyy
How to Get Jquery.Trigger('Click'); to Initiate a Mouse Click
How to Remove Tinymce and Then Re-Add It
How to Select Element That Does Not Have Specific Class
Bootstrap 4 Navbar Not Collapsing on Large Screen
How to Check If a JavaScript Object Is a Dom Object
Unexpected Token Colon JSON After Jquery.Ajax#Get
Event When Window.Location.Href Changes
How to Implement Routereusestrategy Shoulddetach for Specific Routes in Angular 2
Es6 Modules: Undefined Onclick Function After Import
Using Jquery How to Get Click Coordinates on the Target Element