Jump to content
Larry Ullman's Book Forums

Chapter 11 - Working With Command Line Arguments


Recommended Posts

There are several things about "Chapter 11 - Working With Command Line Arguments" (starting around page 449) that are not clear to me from the explanation in the book. I hope Larry can respond to them.

 

I. Figure 11.21 shows the execution of the code on page 432. However, figure 11.21 makes reference to .

/args.php

but the code at the bottom of page 432 which created figure 11.21 doesn't make any reference to an args.php file.

 

 

II. On page 453, as Larry is describing the steps to create

number2.php

, he provides the following code at step #2

 

If ($_SERVER['argc'] == 2) {

 

and he says

 

"Since the script will receive the script's name as its first argument, it would need to receive two arguments to be properly used."

 

I don't understand this. Why does it need to receive two arguments to be properly used? What would the second argument be?

 

III. On the same page, as Larry is describing step 3 in number2.php, he writes

 

"First, the name of the file is identified as the second argument provided (the arguments list being an array, the indexiging begins at 0)"

 

Here he says that the name of the file is identified as "the second argument." However, in my previous question (or step number 2 in the book), he says "Since the script will receive the script's name as its first argument."

 

Is not the "name of the file" in step 3 the same as "the script's name" in step 2. If so, why does he say, in step 2, it will be "the first argument," but in step 3, he says it will be "the second argument"

 

IV in figure 11.23, he runs an

args.php 

script. Where do we get it? It wasn't in the files I downloaded?

 

V On a mac, do I run it (once I located args.php) by doing

 

php number2.php args.php 

 

and if so, what are the first and second arguments that he's talking about (which I mention in question 2 and 3 above). If I understand this code

 

code]php number2.php args.php [/code]

 

I only see one argument being passed, which is

args.php

 

 

Thank you for your help.

Link to comment
Share on other sites

I. Figure 11.21 shows the execution of the code on page 432. However, figure 11.21 makes reference to .

/args.php

but the code at the bottom of page 432 which created figure 11.21 doesn't make any reference to an args.php file.

 

I'm not sure I'm following you on this one. The code at the bottom of page 432 is what I put in a file named args.php, so that I could execute it to show the results in Figure 11.21. It's just a dummy script that merely reports back the arguments provided when it's called.

 

II. On page 453, as Larry is describing the steps to create

number2.php

, he provides the following code at step #2

 

If ($_SERVER['argc'] == 2) {

 

and he says

 

"Since the script will receive the script's name as its first argument, it would need to receive two arguments to be properly used."

 

I don't understand this. Why does it need to receive two arguments to be properly used? What would the second argument be?

 

The second argument is the name of the file to be numbered, as in the explanation under Step 3.

 

III. On the same page, as Larry is describing step 3 in number2.php, he writes

 

"First, the name of the file is identified as the second argument provided (the arguments list being an array, the indexiging begins at 0)"

 

Here he says that the name of the file is identified as "the second argument." However, in my previous question (or step number 2 in the book), he says "Since the script will receive the script's name as its first argument."

 

Is not the "name of the file" in step 3 the same as "the script's name" in step 2. If so, why does he say, in step 2, it will be "the first argument," but in step 3, he says it will be "the second argument"

 

No, the purpose of the script is to add line numbers automatically to another script. So the first argument received will be the name of the script being executed, which is always number2.php. The second argument should be the name of the other file: the one the user wants numbered. This argument would change with multiple uses of number2.php.

 

IV in figure 11.23, he runs an
args.php 

script. Where do we get it? It wasn't in the files I downloaded?

 

That's just a file I chose to number. As I say on page 432, and clarify above, that's a bit of sample code. You can number any text file you want.

 

V On a mac, do I run it (once I located args.php) by doing

 

php number2.php args.php 

 

and if so, what are the first and second arguments that he's talking about (which I mention in question 2 and 3 above). If I understand this code

 

php number2.php args.php 

 

Again, if you see Step 10, it says to run the script using

php number2.php filename

, replacing "filename" with the actual file you want numbered. For me that was "args.php", but for you it can be any plain text file you have. The first argument will be number2.php. The second argument will be args.php or whatever filename you provide.

 

I only see one argument being passed, which is
args.php

 

Right, yes. There's only one argument actually being provided by the user, but the PHP script always receives the name of the script being executed through the command line as its first argument. So every call to a command line PHP script will always have at least one argument.

 

 

Thank you for your help.

 

 

You're welcome. Sorry for the confusion and let me know if anything is still not clear.

Link to comment
Share on other sites

 Share

×
×
  • Create New...