RE: [PHP] 2 errors I can not understand

2007-03-15 Thread Ford, Mike
On 14 March 2007 22:52, Richard Lynch wrote:

 On Wed, March 14, 2007 6:52 am, Myron Turner wrote:
  Richard Lynch wrote:
   On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:
   The = operator takes precedence, and $d is set to 0.
   
  But why?  According to the manual, the modulus operator has
  precedence over the equals!  So shouldn't this expression  resolve
  to:($s % $d) = 0 which gives an error?
 
 Ya got me there.
 
 I hadn't even checked the precedence list before answering.
 
 Unless somebody has a rational explanation, I think this should be an
 error... 

When a similar anomaly involving the ! operator was reported 
(http://bugs.php.net/bug.php?id=17180), the response was that PHP is smart 
about such things and invokes the DWIM principle to silently alter the 
precedence of = in certain situations.  This resulted in the addition of a note 
to the end of the operator precedence page at 
http://uk2.php.net/manual/en/language.operators.php#language.operators.precedence,
 but it seems to me that this is too specific to the ! operator and should be 
generalized.  In fact, I'd completely forgotten that I suggested as much at the 
end of the bug report -- but this hasn't been taken up, so maybe a bunch of you 
want to re-activate the bug report and support my suggestion??

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] 2 errors I can not understand

2007-03-15 Thread Myron Turner

Ford, Mike wrote:

On 14 March 2007 22:52, Richard Lynch wrote:

  

On Wed, March 14, 2007 6:52 am, Myron Turner wrote:


Richard Lynch wrote:
  

On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:
The = operator takes precedence, and $d is set to 0.



But why?  According to the manual, the modulus operator has
precedence over the equals!  So shouldn't this expression  resolve
to:($s % $d) = 0 which gives an error?
  

Ya got me there.

I hadn't even checked the precedence list before answering.

Unless somebody has a rational explanation, I think this should be an
error... 



When a similar anomaly involving the ! operator was reported 
(http://bugs.php.net/bug.php?id=17180), the response was that PHP is smart 
about such things and invokes the DWIM principle to silently alter the precedence of = in 
certain situations.  This resulted in the addition of a note to the end of the operator 
precedence page at 
http://uk2.php.net/manual/en/language.operators.php#language.operators.precedence, but it 
seems to me that this is too specific to the ! operator and should be generalized.  In 
fact, I'd completely forgotten that I suggested as much at the end of the bug report -- 
but this hasn't been taken up, so maybe a bunch of you want to re-activate the bug report 
and support my suggestion??

Cheers!

Mike

  
I've submitted a bug report with respect to this: 
http://bugs.php.net/bug.php?id=40820.


--

_
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

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



Re: [PHP] 2 errors I can not understand

2007-03-14 Thread Myron Turner

Richard Lynch wrote:

On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:
  

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.



You still have not correctly puzzled out what $s % $d = 0 is doing...

The = operator takes precedence, and $d is set to 0.
  
But why?  According to the manual, the modulus operator has precedence 
over the equals!  So shouldn't this expression  resolve to:

  ($s % $d) = 0
which gives an error?

--

_
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

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



Re: [PHP] 2 errors I can not understand

2007-03-14 Thread Tijnema !

On 3/14/07, Myron Turner [EMAIL PROTECTED] wrote:

Richard Lynch wrote:
 On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:

 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.


 You still have not correctly puzzled out what $s % $d = 0 is doing...

 The = operator takes precedence, and $d is set to 0.

But why?  According to the manual, the modulus operator has precedence
over the equals!  So shouldn't this expression  resolve to:
  ($s % $d) = 0
which gives an error?

Might it be that it generates only an error in specific error levels?

Tijnema


--

_
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

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




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



Re: [PHP] 2 errors I can not understand

2007-03-14 Thread Myron Turner

Robert Cummings wrote:

On Wed, 2007-03-14 at 12:57 +0100, Tijnema ! wrote:
  

On 3/14/07, Myron Turner [EMAIL PROTECTED] wrote:


Richard Lynch wrote:
  

On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:



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.

  

You still have not correctly puzzled out what $s % $d = 0 is doing...

The = operator takes precedence, and $d is set to 0.



But why?  According to the manual, the modulus operator has precedence
over the equals!  So shouldn't this expression  resolve to:
  ($s % $d) = 0
which gives an error?
  


  

Might it be that it generates only an error in specific error levels?



Simple proof of precedence problem:

?php

$x = 6; $y = 4;

echo ($x%$y=5).\n;
echo ($x%$y=4).\n;

?

Cheers,
Rob.
  
This illustrates what in fact happens.  But I still don't understand 
why, since, given the precedence of these operators, the = should be 
applied after the modulus.  The expression should resolve to ($x % $y) = 
5 and not to $x % ($y=5).  Or shouldn't it?


--

_
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

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



Re: [PHP] 2 errors I can not understand

2007-03-14 Thread Robert Cummings
On Wed, 2007-03-14 at 07:42 -0500, Myron Turner wrote:
 Robert Cummings wrote:
  On Wed, 2007-03-14 at 12:57 +0100, Tijnema ! wrote:

  On 3/14/07, Myron Turner [EMAIL PROTECTED] wrote:
  
  Richard Lynch wrote:

  On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:
 
  
  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.
 

  You still have not correctly puzzled out what $s % $d = 0 is doing...
 
  The = operator takes precedence, and $d is set to 0.
 
  
  But why?  According to the manual, the modulus operator has precedence
  over the equals!  So shouldn't this expression  resolve to:
($s % $d) = 0
  which gives an error?

 

  Might it be that it generates only an error in specific error levels?
  
 
  Simple proof of precedence problem:
 
  ?php
 
  $x = 6; $y = 4;
 
  echo ($x%$y=5).\n;
  echo ($x%$y=4).\n;
 
  ?
 
  Cheers,
  Rob.

 This illustrates what in fact happens.  But I still don't understand 
 why, since, given the precedence of these operators, the = should be 
 applied after the modulus.  The expression should resolve to ($x % $y) = 
 5 and not to $x % ($y=5).  Or shouldn't it?

You must have missed the post where I said it looks like a bug :)

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

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



Re: [PHP] 2 errors I can not understand

2007-03-14 Thread Jonathan Kahan
Thanks for alll the feedback. I also needed to correct a logic issue with 
this code to check that a number not be divisible by 2 as my function was 
stating all perfect powers of 2 were prime. I need to remeber as I move from 
other languages the difference between = and ==. Of course I will be more 
careful about this next time.



- Original Message - 
From: Robert Cummings [EMAIL PROTECTED]

Newsgroups: php.general
To: Myron Turner [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]; Jonathan Kahan [EMAIL PROTECTED]; php Lists 
php-general@lists.php.net

Sent: Wednesday, March 14, 2007 8:29 AM
Subject: Re: [PHP] 2 errors I can not understand



On Wed, 2007-03-14 at 06:52 -0500, Myron Turner wrote:

Richard Lynch wrote:
 On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:

 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.


 You still have not correctly puzzled out what $s % $d = 0 is doing...

 The = operator takes precedence, and $d is set to 0.




But why?  According to the manual, the modulus operator has precedence
over the equals!


Must be a bug... parenthesis will help to not find the bug :)

Cheers,
Rob.
--
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`' 


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



Re: [PHP] 2 errors I can not understand

2007-03-14 Thread Richard Lynch
On Wed, March 14, 2007 6:52 am, Myron Turner wrote:
 Richard Lynch wrote:
 On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:
 The = operator takes precedence, and $d is set to 0.

 But why?  According to the manual, the modulus operator has precedence
 over the equals!  So shouldn't this expression  resolve to:
($s % $d) = 0
 which gives an error?

Ya got me there.

I hadn't even checked the precedence list before answering.

Unless somebody has a rational explanation, I think this should be an
error...

-- 
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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Tijnema !

On 3/13/07, Jonathan Kahan [EMAIL PROTECTED] wrote:

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

Line feed in a browser is made with the HTML tag br
instead of printint \r\n you should try br


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

I don't see any error in that loop, on my test server it is executing
50 times...


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;$k50;$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

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




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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Jonathan Kahan

That worked in answering part 1 now i got to figure out part 2

- Original Message - 
From: Tijnema ! [EMAIL PROTECTED]

To: Jonathan Kahan [EMAIL PROTECTED]
Cc: php Lists php-general@lists.php.net
Sent: Tuesday, March 13, 2007 5:55 PM
Subject: Re: [PHP] 2 errors I can not understand



On 3/13/07, Jonathan Kahan [EMAIL PROTECTED] wrote:

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

Line feed in a browser is made with the HTML tag br
instead of printint \r\n you should try br


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

I don't see any error in that loop, on my test server it is executing
50 times...


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;$k50;$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

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







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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Matt Carlson
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 [EMAIL PROTECTED]
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;$k50;$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

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





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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Matt Carlson
I retract my statement.  I think I spoke too quickly.  I think I don't fully 
understand the code (can't pay attention right now for some reason), and I 
talked too quick.

I do think it has to do with the while loop though, as that is where it seems 
to be dying.

- Original Message 
From: Matt Carlson [EMAIL PROTECTED]
To: Jonathan Kahan [EMAIL PROTECTED]; php Lists php-general@lists.php.net
Sent: Tuesday, March 13, 2007 5:12:03 PM
Subject: Re: [PHP] 2 errors I can not understand

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 [EMAIL PROTECTED]
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;$k50;$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

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





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





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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Jake McHenry

It worked for you? It didn't for me until i changed

if ($s%$d=0)

to

if($s%$d == 0)

Jake


- Original Message - 
From: Jonathan Kahan [EMAIL PROTECTED]

To: Tijnema ! [EMAIL PROTECTED]
Cc: php Lists php-general@lists.php.net
Sent: Tuesday, March 13, 2007 6:08 PM
Subject: Re: [PHP] 2 errors I can not understand



That worked in answering part 1 now i got to figure out part 2

- Original Message - 
From: Tijnema ! [EMAIL PROTECTED]

To: Jonathan Kahan [EMAIL PROTECTED]
Cc: php Lists php-general@lists.php.net
Sent: Tuesday, March 13, 2007 5:55 PM
Subject: Re: [PHP] 2 errors I can not understand



On 3/13/07, Jonathan Kahan [EMAIL PROTECTED] wrote:

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

Line feed in a browser is made with the HTML tag br
instead of printint \r\n you should try br


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

I don't see any error in that loop, on my test server it is executing
50 times...


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;$k50;$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

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







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



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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Myron Turner

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 [EMAIL PROTECTED]
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;$k50;$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/

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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Jonathan Kahan

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 [EMAIL PROTECTED]

To: Matt Carlson [EMAIL PROTECTED]
Cc: Jonathan Kahan [EMAIL PROTECTED]; 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 [EMAIL PROTECTED]
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;$k50;$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/






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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Robert Cummings
On Tue, 2007-03-13 at 19:04 -0400, Jonathan Kahan wrote:
 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.

Nothing wrong with $s%$d=0. What you have is the following:

$s % ($d = 0)

Probably what was intended was:

($s % $d) == 0

Moral of the story? Don't be sloppy. Take pride in writing readable
code. Anyone can produce gibberish.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Jim Lucas

Robert Cummings wrote:

On Tue, 2007-03-13 at 19:04 -0400, Jonathan Kahan wrote:

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.


Nothing wrong with $s%$d=0. What you have is the following:

$s % ($d = 0)

Probably what was intended was:

($s % $d) == 0

Moral of the story? Don't be sloppy. Take pride in writing readable
code. Anyone can produce gibberish.

Cheers,
Rob.

another suggestion would be to have it written this way

0 == ($s % $d)

if you by chance did this

0 = ($s % $d)

it will give you an error, because you cannot assign a value to a 
literal value.


Just a thought.

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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Robert Cummings
On Tue, 2007-03-13 at 18:46 -0700, Jim Lucas wrote:

 another suggestion would be to have it written this way
 
 0 == ($s % $d)
 
 if you by chance did this
 
 0 = ($s % $d)
 
 it will give you an error, because you cannot assign a value to a 
 literal value.

Yeah, this has flown across the list a few times in the past. I can see
the value in it, but it just looks weird so I don't use it :)

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Jim Lucas

Robert Cummings wrote:

On Tue, 2007-03-13 at 18:46 -0700, Jim Lucas wrote:

another suggestion would be to have it written this way

0 == ($s % $d)

if you by chance did this

0 = ($s % $d)

it will give you an error, because you cannot assign a value to a 
literal value.


Yeah, this has flown across the list a few times in the past. I can see
the value in it, but it just looks weird so I don't use it :)

Cheers,
Rob.

Makes the mistake obvious instead of hidden  :)

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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Richard Lynch
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;$k50;$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 (KR?)
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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Richard Lynch
On Tue, March 13, 2007 6:04 pm, Jonathan Kahan wrote:
 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.

You still have not correctly puzzled out what $s % $d = 0 is doing...

The = operator takes precedence, and $d is set to 0.

*THEN* $s % 0 is calculated, and, err, whatever $s % 0 is, that's the
answer you get...  Danged if I know what $s % 0 is, as it's kind of
meaningless, really...

But that is what happens.

There *ARE* cases where it is handy to use an assignment and take the
result of that operation, and use it as part of a bigger expression,
however.

Suppose, for example, that instead of is_prime your wrote a function
has_divisor(), and it returned 0 for prime numbers, and a divisor for
non-prime:

2 = 0
3 = 0
4 = 2
5 = 0
6 = 2
7 = 0
8 = 2
9 = 3

You could write this very much like your current is_prime, only a bit
different. :-)

if ($result = is_prime($k)){
  echo $k is divisible by $result, so is not primebr /\n;
}
else{
  echo $k is primebr /\n;
}

Here we are using the result of the assignment as our test expression
for the if(), killing two birds with one stone.

It's a little bit puzzling at first, but is clear enough to
experienced developers that the idiom has taken hold, and is quite
common.

Some folks don't like it, and won't use it, mind you, but it shouldn't
freak you out when you see it either. :-)

-- 
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



Re: [PHP] 2 errors I can not understand

2007-03-13 Thread Richard Lynch
On Tue, March 13, 2007 6:27 pm, Robert Cummings wrote:
 Moral of the story? Don't be sloppy. Take pride in writing readable
 code. Anyone can produce gibberish.

The converse is not true.
:-)

IOW, some of us produce gibberish even when we are trying to write
readable code.

'Course, Perl hackers seem to take pride in producing gibberish while
trying to write working code, but that's a whole 'nother story. :-)

-- 
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