trouble with long output string, plz help

Thread Solved

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

trouble with long output string, plz help

 
0
  #1
Aug 2nd, 2006
here is the code, which is meant to output a string of about 50 characters made of — and  , in random color for the dashes, with the dashes occurring only in pairs. I get a "0" for the final output.

  1. function flow() {
  2. $ar = array( 0 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f);
  3.  
  4. $count = 0;
  5. $output = "";
  6.  
  7. for($int = 0; $int < 50, $count < 50; $int++) {
  8.  
  9. $r1 = $ar[rand(0, 15)]; $r2 = $ar[rand(0, 15)];
  10. $g1 = $ar[rand(0, 15)]; $g2 = $ar[rand(0, 15)];
  11. $b1 = $ar[rand(0, 15)]; $b2 = $ar[rand(0, 15)];
  12.  
  13. $col = "\"$r1.$r2.$g1.$g2.$b1.$b2\"";
  14.  
  15. $output += "<span style=\"color:#";
  16. $output += $col;
  17. $output += ">&mdash;&mdash;</span>";
  18.  
  19. $count = $count + 2;
  20.  
  21. if(count >= 50)
  22. break;
  23.  
  24. $numSpaces = rand(0, 9);
  25. for($j = 0 ; $j < $numSpaces, $count < 50; $j++) {
  26. $output += "&nbsp;";
  27. $count++;
  28.  
  29. }
  30. }
  31.  
  32. echo $output;
  33.  
  34. } // end flow
Last edited by tefflox; Aug 2nd, 2006 at 8:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: trouble with long output string, plz help

 
0
  #2
Aug 2nd, 2006
somehow i can't edit the earlier post, here is the updated code that is outputting only random numbers, mostly low numbers, but some like 5.6e+100. and that is all...

  1. function flow() {
  2. $ar = array( 0 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f);
  3.  
  4. $count = 0;
  5. $output = "";
  6.  
  7. for($int = 0; $int < 50, $count < 50; $int++) {
  8.  
  9. $r1 = $ar[rand(0, 15)]; $r2 = $ar[rand(0, 15)];
  10. $g1 = $ar[rand(0, 15)]; $g2 = $ar[rand(0, 15)];
  11. $b1 = $ar[rand(0, 15)]; $b2 = $ar[rand(0, 15)];
  12.  
  13. $col = $r1.$r2.$g1.$g2.$b1.$b2;
  14.  
  15. $output += "<span style=\"color:#\"";
  16. $output += $col;
  17. $output += "\">&mdash;&mdash;</span>";
  18.  
  19. $count = $count + 2;
  20.  
  21. if(count >= 50)
  22. break;
  23.  
  24. $numSpaces = rand(0, 9);
  25. for($j = 0 ; $j < $numSpaces, $count < 50; $j++) {
  26. $output += "&nbsp;";
  27. $count++;
  28.  
  29. }
  30. }
  31.  
  32. echo $output;
  33.  
  34. } // end flow
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: trouble with long output string, plz help

 
0
  #3
Aug 3rd, 2006
Hi tefflox,

I found a few errors:

1) The array $ar has value a, b, c, etc which are undefined. What you actually want is strings:
[PHP]
$ar = array( 0 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f');[/PHP]

2) The style attribute fails because you have an extra slash in there. It should be:

[PHP]$output .= "<span style=\"color:#";
$output .= $col;
$output .= "\">&mdash;&mdash;</span>";[/PHP]
3) In if(count >= 50) , count is undefined. It should be $count.

4) Most important, to concat strings in PHP use .= instead of += as + is the addition operator. Some languages use + to concat strings but php will first cast the integer type to the string before using the + operator. Strings with chars will evaluate to 0.

The logic is all ok except for using the for loop:

[PHP]for($int = 0; $int < 50, $count < 50; $int++) {[/PHP]

This should really be a while loop since its more logical:

[PHP]while( $count < 50 ) {[/PHP]

and will be simpler.

The rest of the logic is right on.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: trouble with long output string, plz help

 
0
  #4
Aug 3rd, 2006
thanks for the help. i'm new to php, intermediate in java, so i had been trying it the java way, also thanks for the other tips. cheers
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC