Geoff Millikan
asked on
Most effective way to execute PHP CLI scripts?
http://www.php-cli.com/php-cli-tutorial.shtml
The tutorial above says that it's ugly and ineffective to run a PHP CLI script like this:
Why is the second way better?
Thanks,
http://www.t1shopper.com/
PS. The PHP tutorial doesn't indicate if there's a preferred method:
http://www.php.net/manual/en/features.commandline.usage.php
The tutorial above says that it's ugly and ineffective to run a PHP CLI script like this:
#> /usr/bin/php /path/to/script/test.php
And that it's much better to code it like a bash script like this:#!/usr/bin/php -q
<?php
echo "Hello world of PHP CLI!";
?>
And then run it like this:#> chmod 755 /path/to/script/bash_formatted_test.php
#> ./path/to/script/bash_formatted_test.php
Why is the second way better?
Thanks,
http://www.t1shopper.com/
PS. The PHP tutorial doesn't indicate if there's a preferred method:
http://www.php.net/manual/en/features.commandline.usage.php
The second way is the preferred way of running the script because you're telling BASH to use the php cli interpreter in that first line.
The secondary part (changing file permissions) is required to make something executable.
I agree with the tutorial.
The secondary part (changing file permissions) is required to make something executable.
I agree with the tutorial.
ASKER
bportlock: I don't need or want to use a web browser, we want to execute PHP as CLI. For example, as a cron job. Any insight on the question at hand?
DrDamnit: Why is that better? Is it because it's "cleaner and more straightforward" or is there some efficiency to be gained in the form of a more direct path to the binary which results in less system bus usage, or maybe less RAM used, etc ?
DrDamnit: Why is that better? Is it because it's "cleaner and more straightforward" or is there some efficiency to be gained in the form of a more direct path to the binary which results in less system bus usage, or maybe less RAM used, etc ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I also endorse bportlock's lynx trick. Never though of that, but it will work just the same.
6 in one hand, half a dozen in the other.
The only advantage you have with CLI is if you want to write to the screen while running a script. I am OCD with outputs, so I like to watch the pretty letters scream by....
opening file SOMEFILE....[OK]
Sending email to drdamnit@awesomeness.com.. ..[OK]
and that jazz.
But if you don't need / want that, either way is sufficient.
6 in one hand, half a dozen in the other.
The only advantage you have with CLI is if you want to write to the screen while running a script. I am OCD with outputs, so I like to watch the pretty letters scream by....
opening file SOMEFILE....[OK]
Sending email to drdamnit@awesomeness.com..
and that jazz.
But if you don't need / want that, either way is sufficient.
ASKER
Very interesting. Using lynx has the advantage of caching content (if that is desired) but has the disadvantage of using up extra overhead of an Apache process. Executing the PHP script via CLI SAPI means no Apache resources are consumed which seems a bit more efficient unless I'm missing something?
So it's no more effective (at least when executing CLI scripts) to run them one way over another. The different styles are merely presentational preferences, right?
So it's no more effective (at least when executing CLI scripts) to run them one way over another. The different styles are merely presentational preferences, right?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
...you need only ever worry about the one php.ini file...
I thought there was only one php.ini file which was always used? Are you saying I can specify one for the web server (libphp5.so) and another for CLI?
" Are you saying I can specify one for the web server (libphp5.so) and another for CLI?"
Yes you can do exactly that. The -c swith on the PHP command lets you specify the php.ini to use.
http://www.php.net/manual/en/features.commandline.options.php (scan for --php-ini )
Yes you can do exactly that. The -c swith on the PHP command lets you specify the php.ini to use.
http://www.php.net/manual/en/features.commandline.options.php (scan for --php-ini )
ASKER
Ah, that has a distinct advantage in that now I can configure my php-apache.ini file a lot smaller than my php-cli.ini file. For example, I'm reducing the amount of memory and the execution time in php-apache.ini and in increasing those things in php-cli.ini.
Very nice.
Very nice.
lynx -dump http://www.example.com/path/to/script.php