"google-chrome <file>" downloads the file instead of opening it

The original answer:

The browser itself cannot run PHP code. You need a web server to interpret a PHP file as web page, that can be displayed through the browser.

If it is a .html file, it will be displayed. Try to create simple HTML file as next and call it index.html, for example:

<html>
    <header><title>This is title</title></header>
    <body>Hello world!</body>
</html>

Then run:

google-chrome index.html

Something more, if you type google-chrome ind and then press Tab the rest part of the file name (ex.html) will be autocomplete. But if it is index.php the autocompletion will not work.


More detailed answer:

The browsers their-self cannot run PHP code. Mainly they work with HTML and some scripting languages as JS. That we see in the browser's window is a result of the interaction between the web server on the one hand and the client's web browser on the other hand.

The PHP code is executed onto the server side and there must be installed interpretation program - so called php. Within Ubuntu, with installed php, if we have a simple PHP scenario, to display it into the browser's window we don't need a web server:

  • Let's assume we have this file:

    $ cat index.php 
    
    <?php
        echo "<html>\n";
        echo "\t<header><title>This is title</title></header>\n";
        echo "\t<body>Hello world!</body>\n";
        echo "</html>\n";
    ?>
    
  • The output of its interpretation is:

    $ php index.php 
    
    <html>
        <header><title>This is title</title></header>
        <body>Hello world!</body>
    </html>
    
  • So, we can redirect the output to a new file and then we can run this new file via the browser:

    php index.php > index.html && chromium-browser index.html &
    

    * In my case it is chromium-browser instead of google-chrome.

  • The result will be:

    enter image description here


Partly related topic: How can i display php/html code output into terminal not browser?