I hope this issue hasn't been addressed in another thread - did some searching but found nothing.
I am writing a function to create some randomized comments in the form of a string. It's going to be used in a loop context in production. I wrote a while loop to test the function and I'm getting some strange output. It works great on the first loop but each subsequent loop truncates the strings to their first chars.
<?PHP
$prefix=array();
$prefix[]="Wow! That's";
$prefix[]="That's";
//...
$prefix[]="Amazing image. So";
$prefix[]="Wonderful image. So";
$suffix=array();
$suffix[]="amazing";
$suffix[]="awesome";
//...
$suffix[]="fabulous";
$suffix[]="historic";
$punctuation=array();
$punctuation[]='!';
$punctuation[]='!!';
//...
$punctuation[]='.';
$punctuation[]='...';
function comment() {
global $prefix;
$prefix_max=count($prefix)-1;
$rand=rand(0,$prefix_max);
$prefix=$prefix[$rand];
global $suffix;
$suffix_max=count($suffix)-1;
$rand=rand(0,$suffix_max);
if(strpos(strtolower($prefix),strtolower($suffix[$rand])) > 0) {
$rand=$rand+1;
if($rand > $suffix_max) {
$rand=0;
}
}
$suffix=$suffix[$rand];
if(substr($prefix, -1) == '.' || substr($prefix, -1) == '!') {
$suffix=ucfirst($suffix);
}
$rand=rand(1,100);
if($rand < 18) {$suffix=strtoupper($suffix);}
global $punctuation;
$punctuation_max=count($punctuation)-1;
$rand=rand(0,$punctuation_max);
$punctuation=$punctuation[$rand];
$comment=$prefix.' '.$suffix.$punctuation;
return $comment;
}
$i=0;
while($i < 70) {echo comment()."\r\n"; $i++;}
?>
This is the output from the loop:
Thank you for sharing! That's wonderful...
T w.
T w.
T w.
T w.
T w.
T w.
T w.
T W.
T W.
T W.
T W.
...
I was expecting full different strings like the first returned value from the loop. Any thoughts on why it's getting truncated?