# PHP static variables

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

Example demonstrating need for static variables

```<?php
function test()
{
\$a = 0;
echo \$a;
\$a++;
}
test();
test();
test();
test();
?>
```

Result This function is useless since every time it is called it sets \$a to 0 and prints 0.

The \$a++ which increments the variable serves no purpose since as soon as the function exits the \$a variable disappears.

To make a useful counting function which will not lose track of the current count, the \$a variable is declared static:

Example use of static variables

```<?php
function test()
{
static \$a = 0;
echo \$a;
\$a++;
}
test();
test();
test();
test();
?>
```

Result Now, \$a is initialized only in first call of function and every time the test() function is called it will print the value of \$a and increment it.

Static variables also provide one way to deal with recursive functions.

A recursive function is one which calls itself.

The following simple function recursively counts to 10, using the static variable \$count to know when to stop:

Static variables with recursive functions

```<?php
function test()
{
static \$count = 0;

\$count++;
echo \$count;
if (\$count < 10) {
test();
}
\$count--;
}
test();
?>
```

Result Static variables can be assigned values which are the, result of constant expressions, but dynamic expressions, such as function calls, will cause a parse error.

Declaring static variables

```<?php
function foo(){
static \$int = 0;          // correct
static \$int = 1+2;        // correct
static \$int = sqrt(121);  // wrong  (as it is a function)

\$int++;
echo \$int;
}
foo();
foo();
?>
```

Note:

Static declarations are resolved in compile-time.
{"ft_lang_label":"__label__en","ft_lang_prob":0.776784,"math_prob":0.98086244,"size":1700,"snap":"2021-04-2021-17","text_gpt3_token_len":394,"char_repetition_ratio":0.1981132,"word_repetition_ratio":0.09090909,"special_character_ratio":0.27411765,"punctuation_ratio":0.16666667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9923219,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-23T13:35:19Z\",\"WARC-Record-ID\":\"<urn:uuid:e710fe9b-86f8-4b39-9255-cd32b76780d6>\",\"Content-Length\":\"14544\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:60b3255a-f27d-4cc0-bfc8-d8211c9f560a>\",\"WARC-Concurrent-To\":\"<urn:uuid:3159a986-96b6-4e39-ad8b-aaeda5c62434>\",\"WARC-IP-Address\":\"52.85.144.28\",\"WARC-Target-URI\":\"https://www.demo2s.com/php/php-static-variables.html\",\"WARC-Payload-Digest\":\"sha1:QYGZWIENY4W7LPTPO34AHU3DNYSSFCO2\",\"WARC-Block-Digest\":\"sha1:MEHNNFOHEM5LZHJ5UF53PHGNBFJJB7G6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618039594808.94_warc_CC-MAIN-20210423131042-20210423161042-00260.warc.gz\"}"}