Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command-line script PHP does not run

Tags:

php

mysql

cron

lamp

I am trying to build a PHP script to process data manually to later convert it to a cronjob. This script also gets data from MySQL and a third-party SOAP interface. When I try to run it from the command line I have an error and the script does not run.

It shows:

./test.php: line 1: ?php: No such file or directory
Enter a number:
./test.php: line 5: syntax error near unexpected token `('
./test.php: line 5: `$line = trim(fgets(STDIN));'

Here's what I have in my script:

echo 'Enter a number: ';
$line = trim(fgets(STDIN));
var_dump($line);

I know this script works. What is wrong?

like image 315
Adéline Avatar asked Jan 22 '12 18:01

Adéline


People also ask

Can we use PHP for command line scripts?

Yes, we can create a command-line PHP script as we do for web script, but with few little tweaks. We won't be using any kind of HTML tags in command-line scripting, as the output is not going to be rendered in a web browser, but displayed in the DOS prompt / Shell prompt.

How do I trigger a PHP file?

Step 1: First of all, open the Apache Friends website and download XAMPP for Windows, and install it. Step 2: Start the XAMPP Program Control Panel. Click on the “Start” button next to the “Apache” to start your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.

Why PHP page is not working?

The most common reason for a blank page is that the script is missing a character. If you left out a ' or } or ; somewhere, your PHP won't work. You don't get an error; you just get a blank screen.


1 Answers

You get this error because you execute this script like ./script.php. In order to make sure the PHP script understand and run properly, you have to include #!/usr/bin/php at the top of your script.

Example:

#!/usr/bin/php
<?php
echo 'Enter a number:';
$line = trim(fgets(STDIN));
var_dump($line);

If PHP is installed in the /usr/bin folder. If not, you can verify using the locate php command and then use the right path.

Or the other alternative will be

php /path/to/script.php
like image 62
Book Of Zeus Avatar answered Oct 05 '22 05:10

Book Of Zeus