I have this code:
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:
- A link, which causes the browser to load a new page.
- A form submission, which submits data to the server and loads a new page.
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. They reside on the server, these programs never have their source code exposed to the user.
So when you make a HTTP request on server, the server first reads the PHP file carefully to see if there are any tasks that need to be executed, and sends a response to the client side. 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.
- You can do so by reloading the page and sending a HTTP request
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
I will try to explain it in a simple way.
Client-Side is what users see/ code, which is visible on the browser.
Client-Side programming focus on “how a page will look like” and its behavior over browsers.
- HTML is what we see.
- CSS decides its designing(Colours, Floating Images, Padding, etc.).
Server-Side Programming includes code which provides data to Client-Side. The user is never able to see the server-side.
Server Side Programming involves Programming Language(Java, PHP, .Net, C#, C, C++, NodeJS etc), Database(SQL, Oracle, MySql, PostgreySql, No-Sql, MongoDB, etc), Third Party API(Rest, Soap), Business Logic.
Server-Side Programming focus on “how to make data available for Client-Side.”
- Server-Side Language is responsible for communicating between different sources of data like database, third-party API, file system, blockchain, etc.. These languages maintain a particular API for client-side to interact with.
- The database is responsible for storing information.
- Business Logic defines “how to use data and what to do with data.”
Client-Side request data or request to store data, from Server-side via API provided by Server-Side. This request and response of data are done by following HTTP/FTP protocol like REST API, SOAP API.