3

I am wondering if a PHP script can be executed from a shell command line.

Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.

Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux

I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.

Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows? all I have at the moment installed is WAMP, which has mysql, php and apache server.

I am sorry for being a novice.

thanks for your kind help

David Garcia
  • 3,056
  • 18
  • 55
  • 90

4 Answers4

3

I am wondering if a PHP script can be executed from a shell command line.

It's possible either by executing:

$ php -f your_script.php

Or by inserting #/usr/bin/env php into the first line of the script and by making it executable.

$ head -n 1 your_sript.php
#/usr/bin/env php
$ chmod +x your_script.php
$ ./your_script.php

Note: this example only works on UNIX systems.

Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.

You can use the same Syntax/Functions etc. The only difference is that there are command line arguments in $argv and some other values in the $_SERVER variable.

Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux.

That doesn't really matter. For your usecase you don't really need a webserver, and a full featured GUI. The advantage of having a command line tool is, you can combine your program with other program available like grep etc.

Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?

You don't need ubuntu, you can also execute a shell script from windows. The PHP executable is located in the %PATH%. This question will help you in order to do this: https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them

Then simply open cmd.exe and execute a script using php -f your_script.php

Community
  • 1
  • 1
MarcDefiant
  • 6,649
  • 6
  • 29
  • 49
3

I am wondering if a PHP script can be executed from a shell command line.

Yes

Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.

It won't have $_REQUEST and friends populated, and $_SERVER won't have server information in it.

Is it better to run a script from shell for performance

Maybe. It avoids the overhead of runnning a webserver. It stops you having cached versions in memory for faster re-execution.

and also is it better to run it from windows or unix/linux

Benchmark it.

I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.

There doesn't seem to be any need to involve a web server for that.

Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?

The standard Windows shell can.

all I have at the moment installed is WAMP, which has mysql, php and apache server.

You'll need the command line version of PHP. I've no idea if WAMP includes it or not.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Yes, PHP can be run from command line.

No, there aren't any differences in coding.

The only difference is that it's not Apache running the script, but the user you are currently logged in as. That could mean different privileges on certain maps and folders.

Community
  • 1
  • 1
Tim S.
  • 13,597
  • 7
  • 46
  • 72
1

Yes you can execute PHP from the command line using:

/path/to/php.exe /path/to/script.php

The main difference is that it doesn't run through Apache, so you won't have things that rely on it (like some $_SERVER data).

Also it won't be subject to timeouts on the command line, unless you have a PHP limit set.

Take a look at http://php.net/manual/en/features.commandline.php for more info.

fire
  • 21,383
  • 17
  • 79
  • 114