0

When i want to run a php file in the terminal, i do : $php hello.php Now, if i want to get rid of the .php at the end, i can add this at the top of file-> #!/bin/env php Now, in the command line i can run, $./hello But when i do this and i try to open the file in webbrowser, it doesn't work properly and instead all of the code gets printed out as is.. For ex, if I had < ?php printf("Hello"); ?> instead of just Hello being shown in browser the whole < ?php printf... is shown. Basically, i want to get rid of the extension i see on top of the browser in the address bar.. I dont want http://xyz.com/hello.php, i only want http://xyz.com/hello How to do this?

2
  • Have you check the file permissions to see if hello.php is executable?
    – user103853
    Aug 16, 2014 at 17:59
  • You can check using ls -l. If it's not enabled, use chmod +x hello.php to change it.
    – user103853
    Aug 16, 2014 at 18:13

1 Answer 1

2

You seem to be a little bit confused. PHP is a server-side scripting language. It was made for specific purposes. A main purpose of using PHP is to create dynamic websites -- websites that their contents can be dynamically changed.

PHP can also be used as a general-purpose programming language. We can write programs in PHP more like we do in C.

Since PHP is an interpreted language, unlike C, when you write a program like

<?php printf("Hello"); ?>

we run it using an interpreter (i.e., a program that parses and executes PHP code on the fly) whose name is, not surprisingly, php.

Given a PHP program that's written in a file named hello.php, for example, we can run the following command in the terminal to run our program

php hello.php

When you write #!/bin/env php atop your php file to be able to run your program by simply executing

./hello.php

you're not totally getting rid of the writing the name of the interpreter program (i.e., php) before the name of the php file. Rather, we're sort of hiding it. Your program is still interpreted using the interpreter named php.

This usage of the PHP programming language is more of the general-purpose one.

Another usage of the PHP programming language is as a server-side scripting language. Web browsers do NOT interpret or run PHP code. In fact, your PHP code doesn't even get sent to the user with the HTML, CSS and/or JavaScript code.

When a browser requests a .php page, the PHP code in this page is first interpreted on the server (maybe using the same interpreter named php), if this code outputs anything (like some HTML code), this output is placed where the code that outputs it resides then the page gets sent to the browser containing only HTML, CSS and/or JavaScript code.

So you're trying to do two completely different things.

To get "Hello" shown by a browser, you need a combination of HTML and PHP. Consider this simple web page

<!DOCTYPE html>
<html>
    <head>
        <title>Hello</title>
    </heda>
    <body>
        <!-- some php code -->
        <?php printf("Hello"); ?>
    </body>
</html>

Try saving this code in a file named hello.php and put it in your ~/vhosts/localhost/public/ directory in the appliance then open up the terminal and execute the following

cd ~/vhosts/localhost/public/
chmod 600 hello.php

Now, open up your browser and enter the URL http://localhost/hello.php and hit Enter. You should now see "Hello" displayed by the browser.

I believe having a URL like http://localhost/hello involves some other configurations to the server that I'm honestly not aware of currently.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .