Command Line arguments - PHP
Am trying the following php script which finds out the maximum between 2 numbers, it accepts the arguments through command line. I check whether the input is provided right, based on the number of command line arguments.
<?php
function larger($arg1,$arg2) {
return max($arg1,$arg2);
}
if($argc > 3 || $argc < 3) print 'Invalid Arguments'; exit(1);
if($argc==3) {
print larger($argv[1],$argv[2]);
}
?>
Am executing the program in a w开发者_JAVA百科indows system, and the file resides in xampp/php directory. While executing I don't get any output neither any error report. How do i check whether am right or wrong?
exit(1)
will always be called since it's outside of that if statement. Try this:
<?php
function larger($arg1,$arg2) {
return max($arg1,$arg2);
}
if($argc > 3 || $argc < 3) {
print 'Invalid Arguments';
exit(1);
} else {
print larger($argv[1],$argv[2]);
exit(0);
}
精彩评论