0

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?

1
  • 1
    function comment($prefix,$suffix,$punctuation) {} instead of using global Commented Apr 7, 2014 at 14:19

2 Answers 2

5

This is because you're using global, and your comments() function is changing the arrays into strings

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

And this is one of the main reasons why the use of global is so strongly discouraged

Sign up to request clarification or add additional context in comments.

2 Comments

What is the preferred way to get vars into a function? As far as I know there are two ways - global, passing vars in as arguments (i.e. function(arguments) ) - are there other methods and what is best practice if I don't want to use globals?
The preferred way, if you're programming in a procedural style, is to pass them as arguments; there's other options if you're programming OOP
0

You are overwriting the prefix and suffix array. Try this:

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

1 Comment

This was exactly the problem - I was overwriting the array with a string value. When I defined different variables for the string vs. array it worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.