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 Link to comment https://forums.phpfreaks.com/topic/210529-arrays/ 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. Link to comment https://forums.phpfreaks.com/topic/210529-arrays/#findComment-1098429 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"; ?> Link to comment https://forums.phpfreaks.com/topic/210529-arrays/#findComment-1098444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.