使用循环内的函数创建随机字符串返回奇怪的输出(PHP)

时间:2014-04-07 14:13:16

标签: php function loops while-loop

我希望这个问题还没有在另一个主题中得到解决 - 做了一些搜索,却一无所获。

我正在编写一个函数来以字符串的形式创建一些随机注释。它将用于生产中的循环上下文中。我写了一个while循环来测试函数,我得到了一些奇怪的输出。它在第一个循环中运行良好,但每个后续循环都会将字符串截断为它们的第一个字符。

<?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++;}
?>

这是循环的输出:

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

我期待完全不同的字符串,比如循环中的第一个返回值。关于为什么会被截断的任何想法?

2 个答案:

答案 0 :(得分:5)

这是因为您正在使用global,而您的comments()函数正在将数组更改为字符串

e.g

global $prefix;                    // references the global variable $prefix
                                   //    which is initially defines as an array
$prefix_max=count($prefix)-1;
$rand=rand(0,$prefix_max);
$prefix=$prefix[$rand];            // changes the value of the global variable 
                                   //    $prefix to a string

这是强烈劝阻使用全球的主要原因之一

答案 1 :(得分:0)

您正在覆盖前缀和后缀数组。 试试这个:

    <?PHP

$prefixArray=array();
$prefixArray[]="Wow! That's";
$prefixArray[]="That's";
//...
$prefixArray[]="Amazing image. So";
$prefixArray[]="Wonderful image. So";


$suffixArray=array();
$suffixArray[]="amazing";
$suffixArray[]="awesome";
//...
$suffixArray[]="fabulous";
$suffixArray[]="historic";


$punctuation=array();
$punctuation[]='!';
$punctuation[]='!!';
//...
$punctuation[]='.';
$punctuation[]='...';

function comment() {
    global $prefixArray;
    $prefix_max=count($prefixArray)-1;
    $rand=rand(0,$prefix_max);
    $prefix=$prefixArray[$rand];

    global $suffixArray;
    $suffix_max=count($suffixArray)-1;
    $rand=rand(0,$suffix_max);
    if(strpos(strtolower($prefix),strtolower($suffixArray[$rand]))!==FALSE) {
        $rand=$rand+1;
        if($rand > $suffix_max) {
            $rand=0;
        }
    }

    $suffix=$suffixArray[$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()."<BR>"; $i++;}
?>
相关问题