1

Hello I have php working fine with my apache server. However, when executing the php script from the command line (like /usr/bin/php file.php) it just echos the sourcecode of my script back at me, even when executing a PHP in the web directory. Anybody know how to fix this?

5
  • didn't you change your php with cat? Commented Apr 11, 2014 at 16:25
  • are you sure that executable php file exists in usr/bin/php? Have you tried creating batch file and running your code? Commented Apr 11, 2014 at 16:26
  • 1
    check this: php <<< "<?php echo 1;" Commented Apr 11, 2014 at 16:27
  • I think libapache2-mod-php5 module is missing Commented Apr 11, 2014 at 16:27
  • What does Your script file look like? Maybe You need <?php instead of <? - check Your php.ini file for the short_open_tag directive. Commented Apr 11, 2014 at 16:28

3 Answers 3

3

Command line scripts STILL need to have at least <?php to switch over to PHP mode. Remember that there's really no such thing as a PHP script. There's only files which have a <?php ... ?> code blocks within them.

So, something like

#!/usr/bin/php

echo 'hello world!';

Would actually output

echo 'hello world!';

because There's no actual PHP code in there. There's just some text that LOOKS like php code. You need to have

#!/usr/bin/php
<?php

echo 'hello world!';

and then you'll get the expected

hello world!

as output. Without <?php the php interpreter will never "switch" over to actual PHP mode, and just treat all of the text in the file as plain output.

Sign up to request clarification or add additional context in comments.

Comments

1

I have been fighting this issue for over 4 months and finding nothing to fix it.

My problems started when I installed a new 3T drive and then moved all my projects off the default 1.5T drive. Could never get anything working right in "localhost" since. Also cannot get aliases working in the 2 virtual hosts I created, but their index.html files now show fine at:

  1. projects.com
  2. tbnk-svr.com

But none of the following work:

  1. localhost/phpmyadmin,
  2. localhost/seopanel,
  3. localhost/test.php,
  4. localhost/info.php,
  5. php on the command line.

I wrote this up at:

https://www.linuxquestions.org/questions/newreply.php?do=newreply&noquote=1&p=6214546

and since I get no errors in any of the error logs, can not figure out what is denying localhost and the cmd line from working. Test of:

php <<< "<?php echo 1;"
php -v

Both work, so not sure why it is only echoing the file contents as "cat file" would do.

Has to do somehow with it not rendering "localhost"

Cheers!

TBNK

Comments

0

In your past questions, you seem to prefer using short open tags (<?), so that is probably the issue. Try running this instead:

/usr/bin/php -d short_open_tag=yes file.php

If this works, then your current config has short_open_tag disabled. You'll have to edit your CLI config file to change this. You can find the location of the config file using this:

/usr/bin/php -i | grep php.ini

You should see:

Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

Edit the file listed by "Loaded Configuration File". The line with:

short_open_tag = Off

Should be set to On.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.