I am using static variable in php. Ideally this should display 1 to 10.
But it is not. Why is it so ?
test(10);
function test($a)
{
static $count = 0;
$count++;
echo $count;
if ($count < $a) {
echo 'in if ' . $count;
test($count);
}
}
You have wrong variable in your if. If you debug it you would see that function triggers only twice instead of 10.
change
if ($count < $a) {
echo 'in if ' . $count;
test($count);
}
to
if ($count < $a) {
echo 'in if ' . $count;
test($a);
}
Use this if you want to print 1 to 10
function test($a)
{
static $count = 0;
$count++;
echo $count."====".$a;
if ($count < $a) {
echo 'in if ' . $count;
test($a);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.