/    Sign up×
Community /Pin to ProfileBookmark

Why static Variable Show Nothing ?

Folks,

Reading this and testing their codes.
They all show nothing on screen. Complete blank.
Note the first code doesn’t have the function call line. Did the manual forget it ?

https://www.php.net/manual/en/language.variables.scope.php

1.

[code]
<?php

function test()
{
static $a = 0;
echo $a;
$a++;
}

?>
[/code]

Even adding the call line shows a blank screen.

[code]
<?php

function test()
{
static $a = 0;
echo $a;
$a++;
}

test()
?>
[/code]

Why is manual full of half empty codes ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — Folks,

https://www.php.net/manual/en/language.variables.scope.php

Even this shows blank page:
<i>
</i>&lt;?php
function test()
{
static $count = 0;

<i> </i>$count++;
<i> </i>echo $count;
<i> </i>if ($count &lt; 10) {
<i> </i> test();
<i> </i>}
<i> </i>$count--;
}
?&gt;


I had to add my own line to get it to work:
<i>
</i>test();


Why is manual full of half empty codes ?
Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — Folks,

This blank too! Code from the manual!
<i>
</i>&lt;?php
function foo(){
static $int = 0; // correct
static $int = 1+2; // correct
//static $int = sqrt(121); // wrong (as it is a function)

<i> </i>$int++;
<i> </i>echo $int;
}


I had to add my own line to get it to work:
<i>
</i>foo();


Why is manual full of half empty codes ?
Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — Stupid manual!

Half-baked codes have confused me now and so I will try this forum which is better tHaN iT.

Didn't understand about the recursive thing and so someone care to explain with a simpler non-buggy cOdE ?

https://www.php.net/manual/en/language.variables.scope.php
Copy linkTweet thisAlerts:
@inktApr 15.2021 — If you want a function to run, you have to explicity tell it to. Read the basics on functions before you try to use them. This is very basic. The examples arent half empty, just assumed that you would know the beginner stuff, before you moved on.
Copy linkTweet thisAlerts:
@inktApr 15.2021 — In regards to scope, I tend to pass vars by reference into functions rather that use globals. Here is an example.


function foo(&amp;$intNumber) {<br/>
var $intSum = $intNumber + 3;<br/>
return $intSum;<br/>
}<br/>
echo foo(2);<br/>
//output is 5
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...