brandonto brandonto - 6 months ago 16
CSS Question

If HTML, CSS, and Javascript are client-side, why are they components of a PHP file?

First and foremost, forgive me for my complete lack of knowledge of web development. I have just recently found interest in this topic but I need some answers to clear things up. And second, I have tried searching for this before but couldn't find the answer I was looking for.

I often hear of the term server-side and client-side programming in regards to web development. They say that server-side and client-side are in a way decoupled from each other. From my understanding, server-side programming makes use of PHP, Rails, Node, ASP.NET, etc., as the technologies and client-side programming makes use of HTML, CSS, Javascript, etc.

Here is where I am fundamentally confused.. From what I know, a PHP file can include HTML, CSS, and Javascript... My question is:

If server-side and client-side programming are indeed separate, why does PHP include HTML, CSS and Javascript? If all of these are done in PHP, the server, where does the client come in? In a typical website run on a PHP server, will there be standalone HTML, CSS, and Javascript files that are not PHP files? Will the client-side developer have have edit the HTML, CSS, and Javascript parts of the PHP file, while the server-side developer works on the PHP part of the file?

Again, sorry, I have absolutely 0 experience with web development and I hope the answer to this question will clear up some things for newcomers like me.

Answer

Your question sure is a good one many people asked sometime in their lives(web developers). PHP indeed is a server side script, but the .php extension acts like a normal .html file most of the time.

PHP needs to be a partner with JS and HTML to work.

E.g. A login form. First, the client has completed the form and submitted it. JS then comes in power, using ajax to send your login information to the server(It could be the same document xxx.php, but the server only cares about the php script part).

Then, it sends back a result from the server and may insert a snippet of JS into your login form, where JS empowers and redirect the user from their HTML interface to a new website.

As you can see from the above example, clients and server handles a webpage differently disregarding their file extension. Clients cannot download the PHP source code and the PHP server doesn't care about other than php code themselves.

A single web file is like a port, where clients send information to a php page and the server returns a snippet.

Clients and servers may use one single .php page or can refer to different pages, but the server side webpage is always unaltered


If server-side and client-side programming are indeed separate, why does PHP include HTML, CSS and Javascript?

So it can compactly pack small things inside one web page. Clients view the interface, server executes the PHP code. However, it is not necessary to pack everything into one webpage.

Also, the .php extension can be viewed by clients, so that they know they will interact with the server sometime on that page. Also, .php does not necessary need to include PHP code.

If all of these are done in PHP, the server, where does the client come in?

Clients need to use JS to send information to the server for its response.

In a typical website run on a PHP server, will there be standalone HTML, CSS, and JavaScript files that are not PHP files?

Client needs to send information to a address e.g. http://example.com/response.php to get a response. So yes, it disregards where the PHP is, because if it is one single .php file, the client still sends information to the same address the webpage was downloaded to get response.

Will the client-side developer have have edit the HTML, CSS, and JavaScript parts of the PHP file, while the server-side developer works on the PHP part of the file?

Indeed. Clients download a webpage, not using the same in the server, changes can be made before information sending, so hackers can hack people's passwords. Clients don't get to read the PHP, so they send queries to the server, while the server only reads anything PHP, because the .php webpage is unaltered on the server, that's why people use PHP to prevent hacking. After the PHP server has responded, usually, they will insert information in the webpage browsing and trigger JS code to execute to change the webpage's HTML and CSS.