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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With