Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 19th, 2003, 02:30 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default CH 6 pg 216 Recursive Code

Im now on this recursive thing. Im not sure how to update this to globals on code. I get this reoccuring error on line 9 and 6 but in different orders.


<html><head></head>
<body>
    <form method = "POST" action = "recursion.php">
        <p>I would like to know the factorial of <input name = "value" type = "text">
        <br />
        <input type = "submit" value = "GO!">
    </form>
</body></html>


-----

Notice: Use of undefined constant value - assumed 'value' in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH6\recursion.php on line 12

Notice: Undefined variable: value in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH6\recursion.php on line 12

Notice: Use of undefined constant value - assumed 'value' in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH6\recursion.php on line 6

Notice: Use of undefined constant value - assumed 'value' in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH6\recursion.php on line 9

-----

<html><head></head>
<body>
    <?php
    function recursion ($value)
    {
        if ($_POST[value] <= 1)
            return 1;
        else
            return $_POST[value] * recursion($_POST[value] - 1);
    }

    echo "The factorial of " . $_POST[value] . " is " . (recursion($value) );
    ?>
</body></html>

Reply With Quote
  #2 (permalink)  
Old November 19th, 2003, 03:43 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, your undefined constant warnings are because you're NOT quoting your array index names. It should be $_POST['value'] or $_POST["value"], not $_POST[value].


Second, your recusive function doesn't look like it's going to work. See, the body of your function always operates on $_POST['value'], without ever changing the value. Recursive functions should operate on their parameters, and should *ALWAYS* have a base case that exits the function.

You never modify $_POST['value'] during the function's execution, so every time you call that function after submitting a value > 1, you're going to recurse indefinitely. The program will either die becuase the script execution time limit has been reached or you're out of memory.


This is how it should look:

Code:
<?php
function factorial($value)
{
   if ($value <= 1) return 1;  // see, we're accessing a local variable,
                               // $value, not the actual value passed in
                               // by the user.
   else return $value * factorial($value - 1);
}

echo "The factorial of {$_POST['value']} is " . factorial($_POST['value']);
?>

There's no reason a utility function should know or care about data submitted by a user, because you limit the usefulness of your function. The function calculates factorials. It doesn't care where that number comes in from.

It just so happens that in this case, the value originally came from user input. Using the function I've defined, you're not limited to this. For example:

$four_fact = factorial(4);

You wouldn't be able to do the above if you hard-coded $_POST['value'] in your function implementation.


Also, your function is poorly named -- the function calculates factorials, let that decide what you name it. Sure, it uses recursion to do it, but that's not anything that a USER of the function should know about. For example, you don't care how join() works internally, you just know what it accepts as parameters and what it returns as a value.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old November 19th, 2003, 06:33 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So then this is correct?

<html><head></head>
<body>
    <?php
    function recursion ($value)
    {
        if ($value <= 1)
            return 1;
        else
            return $_POST['value'] * recursion($_POST['value'] - 1);
    }

    echo "The factorial of {$_POST['value']} is " . recursion($_POST['value']);
    ?>
</body></html>

But with this is get

Document contains no data.

Reply With Quote
  #4 (permalink)  
Old November 19th, 2003, 06:50 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, it's not correct, for the reasons I already described in my first reply. The function I included in my response is the way it SHOULD look, and there's a few paragraphs describing what I've changed and why. It also explains why your version is incorrect.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #5 (permalink)  
Old November 19th, 2003, 07:03 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok. I got it now.

But I still have a question.

When I put in a number like 290 it says:
Document contains no data

Why?



Reply With Quote
  #6 (permalink)  
Old November 19th, 2003, 07:08 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Paste the code you're using.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #7 (permalink)  
Old November 19th, 2003, 07:20 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<html><head></head>
<body>
    <?php
    function factorial ($value)
    {
        if ($value <= 1)
            return 1;
        else
            return $value * factorial ($value - 1);
    }

    echo "The factorial of {$_POST['value']} is " . factorial($_POST['value']);
    ?>
</body></html>

Reply With Quote
  #8 (permalink)  
Old November 19th, 2003, 07:40 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Huh... Perhaps it's just taking too long and PHP times out. After all, 290! is a pretty big number, and you're asking PHP to make 290 function calls without ever returning from any of these functions, which consumes crazy memory.

Since this is just an excersize in recursion, I suggest trying numbers like 5 or 10. Lemme know what happens with that kind of input.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #9 (permalink)  
Old November 19th, 2003, 07:53 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried number up to 150 and its fine. 190 to 210 is also ok. 220 and it stops working.

As long as it works.

Thanks again for the help.



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 5 Arrays Pg 134 Sadlowski BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 February 23rd, 2006 01:25 PM
Ch 2 pg 24 "Using the Properties..." LuluDev BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 2 January 23rd, 2006 09:12 AM
CH 3 pg 96 Question mririe BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 8 August 25th, 2004 04:17 AM
CH 5 pg 160 wadesmart BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 8 November 12th, 2003 11:23 PM
CH 3 pg 76 wadesmart BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 October 26th, 2003 08:46 PM



All times are GMT -4. The time now is 06:44 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.