Re: 2 errors I can not understand

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



While the others have provided the answers for your questions, I went
a little further before reading the whole thread, so here's some bonus
free advice. :-)

On Tue, March 13, 2007 4:30 pm, Jonathan Kahan wrote:
> 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

As noted, use <br> only use <br /> to be oh-so-current HTML expert.

> 2) Why my loop is only executing 3 times when i want it to execute 50

> Output: The number 0 is 0 The number 1 is 0 The number 2 is 1 The
> number 3
> is 1

You probably should make the result more transparent than 0, 1, 3.
Something like "prime" and "not prime" with:

echo "The number $k is ", (isprime($k) ? "" : "not "), "prime";

> php script:
>
> <html>
> <body>
> <?php
> for ($k=0;$k<50;$k++)
> {
> echo ("The number ".$k." is ".isprime($k));

echo is not a function.

So the parens here are order of operation, and "force" PHP to evaluate
the '.' operators before printing the string -- These parens are thus
kinda bogus, really, as PHP will be doing the concatenation before the
echo anyway.

You can also just embed $k in quotes (not apostrophes) and echo takes
multiple args so you could just do:
echo "The number $k is ", isprime($k), "<br />\n";
//The \n is for pretty output in "View Source" in browser.

The difference between concatenating before echo and using , for
multiple args is probably insignificant, except inside a super-fast
and super-long loop...  But it's a good habit to form, and costs
nothing more, really.
[NOTE: Some developers hate the comma and multiple args to echo for
whatever reason...]

> echo "\r\n";
//Ah, yes.  No need for a second 'echo' statement
//And \r\n is Windoze-centric, but harmless
//Unless you worry about an extry byte on each line.

It's a tiny bit less cluttered to have just one echo, though.

> }
>
> function isprime ($s)

Standard function names:
is_prime (K&R?)
isPrime (camelCaps)
IsPrime (StudlyCaps)

isprime, however, is kinda icky, as the next function you need to
write may have several words in its name, and 'longfunctionamehere'
gets pretty unreadable pretty fast...

I prefer is_prime, personally, but you can pick any of the standards.

Using isprime is probably a Bad Idea, though, honestly...

> {
> $a="2";

Why are you making $a be a string?
Just use:
$a = 2;

>
>                 switch ($s)
>                 {
>                 case "0":
>                 $a="0"; break;
>                 case "1":
>                 $a="0"; break;
>                 case "2":
>                 $a="1"; break;

All of these are integer operations, not strings...
Lose the quotes.

>                 default:
>                 $d=3;
>                         while ($d<$s)
>                         {
>                                 if ($s%$d=0)

//This if() statement is not doing what you think it is.
//Hint: = == and === are all different in PHP
//Hint: Reearch the order of operations for % and = in PHP is

>                                 {
>                                 $a="0";

At this point, instead of continuing to check the remaining potential
divisors, you should probably just return 0;

>                                 }
>                         $d+=2;
>                         }
>                 }
>
>                 if ($a=="2")
>                 {
>                 $a="1";
>                 }

It feels hinky to me to assign $a with 2, run through a loop that
might change it to 0, or might change it to 1, and then, at the end,
if it's still 2, set it to 1 anyway.

I suspect you left out a test somewhere for ($a == 1) and bailing out
of the loop, which is what prompted my suggestion to just return 0
above.

I hate a spaghetti non-local exit (i.e., 'return' anywhere but the
end) as much as the next guy, but there *IS* a time when it's the
Right Thing to do, and something as simple and straight-forward as
this, I'd just go ahead and do the return inside the loop and say
[bleep] the Programming Purists.  YMMV

> return $a;
> }
> ?>
> </body>
> </html>

You also have not allowed (in the code presented) for input to isprime
to be, say, -1

Sure, your loop will not do that, but the function as it stands isn't
very robust...

This may not matter, or it might be something worth working on.
Depends on what you are doing.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux