I have this code:
var foo = 'bar';
file_put_contents('foo.txt', ' + foo + ');
var baz = <?php echo 42; ?>;
Why does this not write "bar" into my text file, but alerts "42"?
Your code is split into two entirely separate parts, the server side and the client side.
The example code executes like this:
Step 1, PHP executes all code between
<?php ?> tags. The result is this:
file_put_contents call did not result in anything, it just wrote " + foo + " into a file. The
<?php echo 42; ?> call resulted in the output "42", which is now in the spot where that code used to be.
alert call works, while the
foo variable is not used anywhere.
To call some PHP code, the client will have to send a new HTTP request to the server. This can happen using one of three possible methods:
Here's a question outlining these method in greater detail
window.location or submit a form, emulating possibilities 1. and 2.
Server-side languages (PHP etc.): They retrieve records from databases, maintain state over the stateless HTTP connection, and do a lot of things that require security. hey reside on the server, these programs never have their source code exposed to the user
So when you make a HTTP request on server than The server first reads the PHP file carefully to see if there are any tasks that need to be executed and send response to client side and again as @deceze said *Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.*
So now what can I do if I need to call PHP? It depends how you need to do it: either by reloading the page or by using an AJAX call.
foo is not evaluated on the server side and therefore its value can't be written to a file on the server.
The best way to think about this process is as if you're generating a text file dynamically. The text you're generating only becomes executable code once the browser interprets it. Only what you place between
<?php tags is evaluated on the server.
In web application every task execute in a manner of request and response.
Client side programming is with html code with Java script and its frameworks, libraries executes in the internet explorer, Mozilla, chrome browsers. In the java scenario server side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs