Beginner to PHP - a quick question about command line interface

DragonKind

Solid State Member
Messages
9
Location
USA
Hey everyone, I am a complete beginner to PHP and was having trouble figuring out how to simply type one command to execute PHP script. If anyone knows PHP here, I'd really greatly appreciate any help with my puzzle.

Here are the two problems I am having:

I want to run my php program from the command line, but I always have to type "php programname" instead of just typing "programname" in the command line. How do I make it known to the shell that whenever I type "programname" it runs the program?

Also, I have some arguments that I parse using getopt(). I also have a configuration file, which has a $DEFAULT_ARGS setting. I want the user to be able to set the default args, and if they run program without arguments $DEFAULT_ARGS takes over and becomes the argument list.

Without writing my own parser, is there a way to "spoof" the argv[] variable or somehow make getopt() work with the string I've specified in the configuration file instead?

For example, if I wanted to run this on the command line: php program -a 300 --debug, instead I will have $DEFAULT_ARGS="-a 300 --debug" in my configuration file and simply write program to run the program. This is my end goal. If anyone can help that would be awesome.
 
If you want to run your php command from the command line without typing "php" you'll have to alias to the php command. ie to run "program.php", you'll type "alias program="php program"

If you type "alias" you'll see a list of your current aliases.

I'm no expert in PHP but if it's anything like C++ (which I highly doubt), within function passses you can set a default value for passed in parameters, ie:

thisIsMyFunction(int i=10) { . . . }
will set i to 10 unless something else is passed in. Again, I'm not sure with PHP, that question will probably be best answered by another member.

Or maybe check if a parameter was passed in. If not, set it to whatever you want the default to be, if it is, skip it.
 
Thanks for the tip about alias. I was trying to avoid parsing the parameter passing but I don't think it can be avoided at this point. Thank you for the advices.
 
Back
Top Bottom