Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 19th, 2003, 02:30 PM
Authorized User
 
Join Date: Oct 2003
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>

 
Old November 19th, 2003, 03:43 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old November 19th, 2003, 06:33 PM
Authorized User
 
Join Date: Oct 2003
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.

 
Old November 19th, 2003, 06:50 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old November 19th, 2003, 07:03 PM
Authorized User
 
Join Date: Oct 2003
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?



 
Old November 19th, 2003, 07:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Paste the code you're using.


Take care,

Nik
http://www.bigaction.org/
 
Old November 19th, 2003, 07:20 PM
Authorized User
 
Join Date: Oct 2003
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>

 
Old November 19th, 2003, 07:40 PM
Friend of Wrox
 
Join Date: Jun 2003
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/
 
Old November 19th, 2003, 07:53 PM
Authorized User
 
Join Date: Oct 2003
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.








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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.