Re: 2 errors I can not understand

From: Date: Tue, 13 Mar 2007 23:04:52 +0000
Subject: Re: 2 errors I can not understand
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-250502@lists.php.net to get a copy of this message
This did fix the problem but I am amazed that $s%$d=0 would be interpereted as a statement assigning d to 0 since there is some other stuff in front of d... I would think that would produce an error at compile time since $s%$d is an illegal variable name. Normally when my php script errors at compile time nothing will display to the screen. ----- Original Message ----- From: "Myron Turner" <turnermm02@shaw.ca> To: "Matt Carlson" <saryon@swbell.net> Cc: "Jonathan Kahan" <jonathan@estrintech.com>; "php Lists" <php-general@lists.php.net> Sent: Tuesday, March 13, 2007 6:22 PM Subject: Re: [PHP] 2 errors I can not understand
Matt Carlson wrote:
I think you have an issue with the line while($d<$s) when it comes to the number 3. $d will NEVER be < $s if $s = 3. I think you want $d<=$s?? Or maybe a switch for the number 3? ----- Original Message ---- From: Jonathan Kahan <jonathan@estrintech.com> To: php Lists <php-general@lists.php.net> Sent: Tuesday, March 13, 2007 4:30:45 PM Subject: [PHP] 2 errors I can not understand Hi all, Please see my output below followed by the code. I have been trying for some to figure out why 1) I can not get a line feed to work in the web page that i am using to display the output as I am not running from the commad line 2) Why my loop is only executing 3 times when i want it to execute 50 Any help would be greatly appreciated. Jonathan The next line is all that displays on the output html page: Output: The number 0 is 0 The number 1 is 0 The number 2 is 1 The number 3 is 1 php script: <html> <body> <?php for ($k=0;$k<50;$k++) { echo ("The number ".$k." is ".isprime($k)); echo "\r\n"; } function isprime ($s) { $a="2";
                switch ($s)
                {
                case "0":
                $a="0"; break;
                case "1":
                $a="0"; break;
                case "2":
                $a="1"; break;
                default:
                $d=3;
                        while ($d<$s)
                        {
                                if ($s%$d=0)
                                {
                                $a="0";
                                }
                        $d+=2;
                        }
                }
                if ($a=="2")
                {
                $a="1";
                }
return $a; } ?> </body> </html> "tjon2.php" 78L, 447C written
You have an infinite loop going: if ($s%$d=0) should be: if ($s%$d==0) --otherewise your loop keeps going:
     while ($d<$s)
since $d (0) will be less and $s -- _____________________ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/


« previous php.general (#250502) next »