Page 1 of 1
[Solved] Newbie question: How do I re-evaluate a variable?
Posted: Wed Aug 19, 2009 6:09 pm
by Szandor
I'm pretty new to PHP and I have a small problem. I've create a code that creates random names from name elements and patterns, but I can't get it to run multiple times. I currently use this code to invoke it:
Code: Select all
<?
require_once('engine.php');
for($a = 1;$a != 6;$a++) {
echo "\t\t\t<li>" . $name . "</li>";
}
?>
The code in "engine.php" does all the work and produces the variable "$name". What I want to be able to do is to re-evaluate "$name" every time the loop is run so that a new random name is generated.
How can I do that?
Re: Newbie question: How do I re-evaluate a variable?
Posted: Wed Aug 19, 2009 6:52 pm
by aceconcepts
From the code you posted the $name is only generated once as engine.php is called outside of the for() scope.
Re: Newbie question: How do I re-evaluate a variable?
Posted: Wed Aug 19, 2009 7:22 pm
by Szandor
I tried changing the above code to the following instead:
Code: Select all
<?
for($a = 1;$a != 6;$a++) {
require('engine.php');
echo "\t\t\t<li>" . $name . "</li>";
}
?>
The problem is, this generates the following error:
Fatal error: Cannot redeclare randel() (previously declared in F:\xampplite\htdocs\rannames\engine.php:55) in F:\xampplite\htdocs\rannames\engine.php on line 63
This is engine.php:
Code: Select all
<? // Name randomizer engine, using specially formatted documents to define the name structure and a PHP parser to randomize names from that structure.
// We open the file for reading...
$file = fopen($namefile, "r") or exit("Unable to open file!");
// ... and put everything in the file in arrays. This loop is run on each line until the end of the file.
while(!feof($file)) {
// We go to the next line (or the first line, if run for the first time).
$nextline = fgets($file);
// Anything after two slashes is removed, as are empty lines and beginning and trailing whitespaces.
$nextline = preg_replace("/\/\/.+|(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $nextline);
$nextline = preg_replace("/^[ \t]+|[ \t]+$|\n$/", "", $nextline);
// If, after this, the line is not empty...
if ($nextline != "") {
// ... we check to see if it's a pattern (begins with a dot). If it is, it's put into the pattern array.
if (preg_match('/^\./',$nextline)) {
$pattitle = preg_replace('/^\./','',$nextline);
$pattitle = preg_replace('/(?= ).*/','',$pattitle);
$nextline = preg_replace('/^[^\n|^ ]*(?= )/','',$nextline);
$nextline = preg_replace('/^ /','',$nextline);
$patterns[$pattitle] = $nextline;
} else {
// If it's not a pattern, we check to see of it's a section header...
if (preg_match('/^\[.*/',$nextline)) {
$nextline = preg_replace('/^\[/','',$nextline);
$nextline = preg_replace('/\]$/','',$nextline);
$sectline = $nextline;
// ... or else we put it in an appropriate place in the array.
} else {
if ($nextline != "") {
$elements[$sectline][] = $nextline;
}
}
}
}
}
// When it's all over, we close the file. We won't be needing it anymore.
fclose($file);
// Let's randomly select a pattern.
$thepattern = array_rand($patterns);
$thepattern = $patterns[$thepattern];
// This function does the substituting.
function randel($anelement) {
global $elements; // We need this global.
$anelement = preg_replace("/\{|\}/","",$anelement); // We strip away the curly brackets.
$anelement = implode($anelement); // It needs to be a string, not an array.
$anelement2 = count($elements[$anelement], COUNT_RECURSIVE) - 1; // Since array_rand() can't be used on multi-dimensional arrays, we count the number of values in the selected sub-array, ...
$anelement2 = rand(0,$anelement2); // ... randomly choose a number ...
$theelement = $elements[$anelement][$anelement2]; // ... and then call the sub array.
return $theelement;
}
// Now we print out the pattern, substituting any stuff in curly brackets with a random entry from the list of elements.
$name = preg_replace_callback('/\{[^\{\}\r\n]*\}/','randel',$thepattern);
?>
Re: Newbie question: How do I re-evaluate a variable?
Posted: Wed Aug 19, 2009 7:27 pm
by Szandor
Ahh! I worked it out!
I took out the last line of "engine.php" and placed it in the loop instead. Works great! I may yet become a PHP programmer...

Re: [Solved] Newbie question: How do I re-evaluate a variable?
Posted: Thu Aug 20, 2009 12:30 am
by aceconcepts
Keep working at it and you will
