Home > Net >  Why do we need local host or any host to run a php file?
Why do we need local host or any host to run a php file?

Time:01-22

I am a bit new to this web designing thing and I am trying to grasp the concept. I tried to run a php page named index.php but the browser wont execute it.

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php 
    echo "ram";
    ?>


</body>
</html>

enter image description here

CodePudding user response:

That's because browsers can't execute PHP code (they can only execute JavaScript).

PHP code is parsed and executed by the PHP interpreter, which is a separate executable program. It is not integrated with any web browser.

You can run PHP directly from a command prompt via its executable, but in a web application scenario it's usually the case that PHP is integrated with the webserver via a suitable module of the webserver. That means that when your browser sends a HTTP request to the webserver requesting a .php page, the webserver will pass your request to the PHP executable, which reads the code in the requested file, executes it, and returns the resulting output to the webserver, which then forwards it on to the browser as the response to the HTTP request.

(Just for completeness: Of course just like any other HTTP-based scenario, this process is not limited to using a web browser as the client. Any computer program which can talk HTTP is equally able to make requests to a webserver running PHP, and get the response.)

Further reading: What is the difference between client-side and server-side programming?. While that question is dealing mainly with the interaction between browser-side JavaScript and server-side languages such as PHP, it does provide a general background about the architecture of a web application, which is relevant to your question too.

  •  Tags:  
  • Related