I'll you give a for instance with linux.
Say I have two files:
file1.php:
and
file2.php:
Code:
#!/usr/bin/php
<?
phpinfo();
?>
Both files are marked executable by my user.
You execute file1.php and it'll give you an output like this:
Code:
j_shaw@lois /opt/j_shaw $ ./file1.php
./file1.php: line 1: ?: No such file or directory
./file1.php: line 2: syntax error near unexpected token `;'
./file1.php: line 2: `phpinfo();'
The second one gives you an output like this:
Code:
j_shaw@lois /opt/j_shaw $ ./file2.php
phpinfo()
PHP Version => 4.4.0-pl1-gentoo
System => Linux lois 2.6.14-hardened-r3 #1 PREEMPT Thu Jan 5 09:20:52 EST 2006 i686
Build Date => Dec 1 2005 15:43:04
...
So what basically happens when you try and execute something is the system first tries to find out what it is. If it is a binary type file it'll try and find the _start symbol (aka int main(...) in a C program) and go from there.
If it is an ascii file it first sees if an interpreter is specified (in the case of File1 there is not, in the case of File2 the #!/usr/bin/php specifies to use the /usr/bin/php program to interpret the file. If there isn't then it uses the default shell to attempt to execute it. This explains the above results because bash, my default shell, can't understand the php code.
You'll notice that the extension (.php) on my above files does not tell the system anything, as it does not interpret file1.php with php just because it has a php executable.