note 109378 added to functions.returning-values
| From: | toron_mail at yahoo dot com | Date: | Wed, 11 Jul 2012 21:30:40 +0000 |
| Subject: | note 109378 added to functions.returning-values | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189106@lists.php.net to get a copy of this message | ||
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"
?>
----
Server IP: 69.147.83.197
Probable Submitter: 72.83.147.9
----
Manual Page -- http://www.php.net/manual/en/functions.returning-values.php
Edit -- https://master.php.net/note/edit/109378
Del: integrated -- https://master.php.net/note/delete/109378/integrated
Del: useless -- https://master.php.net/note/delete/109378/useless
Del: bad code -- https://master.php.net/note/delete/109378/bad+code
Del: spam -- https://master.php.net/note/delete/109378/spam
Del: non-english -- https://master.php.net/note/delete/109378/non-english
Del: in docs -- https://master.php.net/note/delete/109378/in+docs
Del: other reasons-- https://master.php.net/note/delete/109378
Reject -- https://master.php.net/note/reject/109378
Search -- https://master.php.net/manage/user-notes.php