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.

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 += ">&mdash;&mdash;</span>";
        
        $count = $count + 2;
        
        if(count >= 50) 
            break;
        
        $numSpaces = rand(0, 9);
        for($j = 0 ; $j < $numSpaces, $count < 50; $j++) {
            $output += "&nbsp;";
            $count++;

        }
    }    
    
    echo $output;
    
} // end flow

Recommended Answers

All 3 Replies

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

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 += "\">&mdash;&mdash;</span>";
        
        $count = $count + 2;
        
        if(count >= 50) 
            break;
        
        $numSpaces = rand(0, 9);
        for($j = 0 ; $j < $numSpaces, $count < 50; $j++) {
            $output += "&nbsp;";
            $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:

$ar = array( 0 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f');

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

$output .= "<span style=\"color:#";
        $output .= $col;
        $output .= "\">&mdash;&mdash;</span>";

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:

for($int = 0; $int < 50, $count < 50; $int++) {

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

while( $count < 50 ) {

and will be simpler.

The rest of the logic is right on.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.