note 109378 deleted from functions.returning-values by aharvey
| From: | aharvey@php.net | Date: | Wed, 05 Sep 2012 02:12:54 +0000 |
| Subject: | note 109378 deleted from functions.returning-values by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190499@lists.php.net to get a copy of this message | ||
Note Submitter: toron_mail at yahoo dot com
----
I used recursive to reverse a string, created code as below
<?php
$str="abcdef";
function convert($str) {
static $retStr;
if ($str==NULL || $str=="") {
return $retStr; // here the $retStr="fedcba", correct
} else {
$retStr .= substr($str, -1);
$str = substr($str, 0, -1);
convert($str);
}
}
echo "\nThe return string = ".convert($str)."\n"; // the return string is empty
?>
if I changed the code, use global, like below
<?php
$str="abcdef";
$retStr = "";
function convert($str) {
global $retStr;
if ($str==NULL || $str=="") {
return NULL;
} else {
$retStr .= substr($str, -1);
$str = substr($str, 0, -1);
convert($str);
}
}
convert($str);
echo "\nThe global = ".$retStr; // get the right result "fedcba"
?>