Jump to content

Arrays


zero_ZX

Recommended Posts

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

Ah, thanks a lot :D

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.