| | |
trouble with long output string, plz help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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.
PHP Syntax (Toggle Plain Text)
function flow() { $ar = array( 0 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f); $count = 0; $output = ""; for($int = 0; $int < 50, $count < 50; $int++) { $r1 = $ar[rand(0, 15)]; $r2 = $ar[rand(0, 15)]; $g1 = $ar[rand(0, 15)]; $g2 = $ar[rand(0, 15)]; $b1 = $ar[rand(0, 15)]; $b2 = $ar[rand(0, 15)]; $col = "\"$r1.$r2.$g1.$g2.$b1.$b2\""; $output += "<span style=\"color:#"; $output += $col; $output += ">——</span>"; $count = $count + 2; if(count >= 50) break; $numSpaces = rand(0, 9); for($j = 0 ; $j < $numSpaces, $count < 50; $j++) { $output += " "; $count++; } } echo $output; } // end flow
Last edited by tefflox; Aug 2nd, 2006 at 8:30 pm.
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...
PHP Syntax (Toggle Plain Text)
function flow() { $ar = array( 0 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f); $count = 0; $output = ""; for($int = 0; $int < 50, $count < 50; $int++) { $r1 = $ar[rand(0, 15)]; $r2 = $ar[rand(0, 15)]; $g1 = $ar[rand(0, 15)]; $g2 = $ar[rand(0, 15)]; $b1 = $ar[rand(0, 15)]; $b2 = $ar[rand(0, 15)]; $col = $r1.$r2.$g1.$g2.$b1.$b2; $output += "<span style=\"color:#\""; $output += $col; $output += "\">——</span>"; $count = $count + 2; if(count >= 50) break; $numSpaces = rand(0, 9); for($j = 0 ; $j < $numSpaces, $count < 50; $j++) { $output += " "; $count++; } } echo $output; } // end flow
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 .= "\">——</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.
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 .= "\">——</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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Java's String Tokenizer (Java)
- convert int to string (C)
- what is the simplest way to output my results (C)
- Text Output to windows rectangle (C++)
- Counting Spaces in a string (C++)
Other Threads in the PHP Forum
- Previous Thread: Groovy website database query counter ..thing
- Next Thread: gif content overflowing error_reporting function seem not suppressing warning
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error errorlog file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod host href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql problem query radio random recursion recursiveloop remote script search select server sessions sms snippet source space sql static survey syntax system table trouble tutorial up-to-date update upload url validator variable video web youtube






