zero_ZX Posted August 12, 2010 Share Posted August 12, 2010 Hi, I'm having some trouble trying to generate my random string for a name. This is my code: <?PHP function rand_string( $length ) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $size = strlen( $chars ); for( $i = 0; $i < $length; $i++ ) { $str .= $chars[ rand( 0, $size - 1 ) ]; } return $str; } $salt_string = rand_string( 5 ); $test = "1234"; echo "salt_string = $salt_string"; echo "<br>"; echo "test = 1234"; echo "<br>"; $array = array('$test', '$salt_string'); echo "<br>"; echo "array = $array"; echo "<br>"; $imploded = implode($array); echo "<br>"; echo "imploded = $imploded"; ?> And it returns: salt_string = 8pbAe test = 1234 array = Array imploded = $test$salt_string I don't understand why my imploded variable is not 12348pbAe Quote Link to comment Share on other sites More sharing options...
joel24 Posted August 12, 2010 Share Posted August 12, 2010 Because you are putting the variables inside SINGLE quotes and it is taking those values as text, not a reference to a variable $array = array('$test', '$salt_string'); Remove the quotes around the variables. Quote Link to comment Share on other sites More sharing options...
zero_ZX Posted August 12, 2010 Author Share Posted August 12, 2010 Ah, thanks a lot For those of you who want working code: <?PHP function rand_string( $length ) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $size = strlen( $chars ); for( $i = 0; $i < $length; $i++ ) { $str .= $chars[ rand( 0, $size - 1 ) ]; } return $str; } $salt_string = rand_string( 5 ); $test = "1234"; echo "salt_string = $salt_string"; echo "<br>"; echo "test = 1234"; echo "<br>"; $array = array($test, $salt_string); echo "<br>"; echo "array = $array"; echo "<br>"; $imploded = implode($array); echo "<br>"; echo "imploded = $imploded"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.