Jump to content
Larry Ullman's Book Forums

Recommended Posts

Time to flaunt my noobnis over here now. I just bought the book and am working through the first chapter. Page 23 item 9 suggests adding the following code for debugging:

echo '<pre>' . print_r($tasks,1) . '</pre>';

What I can't get clear up in my head is...what is the optional bool parameter doing? I read all over the 'net  that it allows for assigning the print_r value to a variable instead of printing it out. That is what the PHP manual states also:

 

http://fr2.php.net/print_r

 

The output I get from print_r($tasks,1) for the code in the book is this, just as shown in the book:

Array
(
    [0] => Array
        (
            [1] => Must Do This!
            [2] => Another Task
            [3] => My New Task
            [4] => This is a new task!
        )

    [2] => Array
        (
            [5] => Subtask 1
            [6] => Subtask 2
            [8] => Subtask 3
        )

    [6] => Array
        (
            [7] => Subsubtask 1
        )

)

If I omit the bool and use print_r($tasks), like this:

echo '<pre>' . print_r($tasks) . '</pre>';

This is my output:

Array ( [0] => Array ( [1] => Must Do This! [2] => Another Task [3] => My New Task [4] => This is a new task! ) [2] => Array ( [5] => Subtask 1 [6] => Subtask 2 [8] => Subtask 3 ) [6] => Array ( [7] => Subsubtask 1 ) ) 

The PHP manual shows that I should get the output with new lines with the bool omitted. I am getting human readable data in either case, but the former output is easier to read than the latter. Nowhere do I read that using the optional bool parameter will format the output for a better formatted output.

 

I hate to ignore little things like this because they always find a way to haunt me later. What is going on here with the optional bool?

 

I am using PHP version 5.4.4 if that makes a difference. Thanks.

Link to comment
Share on other sites

I imagine that when you execute your code without the second argument, the array is not printed out within pre tags, and that you get a 1 printed out under the array in pre tags.

Is that correct?

 

It should be, as the PHP.net page states the following:

When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.

 

 

In other words, without the second argument, TRUE is returned, which is represented and output as 1.

In addition, when executing your code without the second argument, I think that the PHP interpreter actually hiccups a bit, and instead of throwing an error, assumes that you want to output the content of the array, which is does (unformatted) right before the pre tags, and then outputs the pre tags with a 1 between them. I think this happens because echoing print_r without the second argument after attempting to concatenate it together with two strings is not normally something you do.

 

However, if you set the second argument to true, then a string is indeed returned, which can of course be concatenated together with other strings and echoed out accordingly.

 

I have a feeling this is what you're actually seeing.

Please let us know though.

  • Upvote 1
Link to comment
Share on other sites

 

I imagine that when you execute your code without the second argument, the array is not printed out within pre tags, and that you get a 1 printed out under the array in pre tags.

Is that correct?

 

Yeah, exactly. That is what I see. Very good explanation. That clears it up for me, now I can move on. Thank you very much.

 

I like this task list code. I have integrated it into my current projects in the admin section so I can stay better organized and my clients can see where I am headed. They can "read only" and I can remove as items are accomplished. As far as I am concerned, the code and new knowledge gained in the first 25 pages has more than paid for this book.

 

Thanks for your time HartlySan. 

Link to comment
Share on other sites

Yeah, the advanced PHP book isn't applicable to everyone, but if you're ready for it, it's a great book. I also like that task list code as well.

 

Nowadays, I find myself creating all sorts of simple webpages that only I can access for the purposes of keeping tracking of my schedule and allowing me to update and view my schedule from any device.

Usually, my task list apps are very simple with minimal styling and features, but honestly, how much do you really need?

I mention all that just to show that I can very much relate to where you're coming from.

 

Anyway, glad my answer helped you.

Enjoy the rest of the book.

Link to comment
Share on other sites

 Share

×
×
  • Create New...