I am a newbie to php?

Toshiba / Mini nb305-n440bn 10.1
January 15, 2011 at 08:27:22
Specs: Windows XP
I found a code to make a list of possible permutations. How do I use this code?
Permutation Generator.I am a newbie to php.

<?
function permutations($letters,$num){
$last = str_repeat($letters{0},$num);
$result = array();
while($last != str_repeat(lastchar($letters),$num)){
$result[] = $last;
$last = char_add($letters,$last,$num-1);
}
$result[] = $last;
return $result;
}
function char_add($digits,$string,$char){
if($string{$char} <> lastchar($digits)){
$string{$char} = $digits{strpos($digits,$string{$char})+1…
return $string;
}else{
$string = changeall($string,$digits{0},$char);
return char_add($digits,$string,$char-1);
}
}
function lastchar($string){
return $string{strlen($string)-1};
}
function changeall($string,$char,$start = 0,$end = 0){
if($end == 0) $end = strlen($string)-1;
for($i=$start;$i<=$end;$i++){
$string{$i} = $char;
}
return $string;
}
?>

To use this Generator you can do something like this :

<?
$Array=permutations("ABC",3);
for($i=0 ; $i < count($Array) ; $i++) {
echo "$i." . $Array[$i] . "
";
}
?>


See More: I am a newbie to php?

Report •


#1
January 16, 2011 at 18:07:27
Uh, put the whole thing in a file (i.e: file.php), and then run it with php -f

php -f file.php


Report •
Related Solutions


Ask Question