views:

54

answers:

1

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 windows 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?

+3  A: 

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);
}
ircmaxell
Thank you for the correction :). Understood my mistake.
Chaitanya