Updating variable value through a function in php
<p>I am new in web page development, and it seems that this is a basic question but I can't figure out how to solve.</p>

<p>Through a button I need to change the value of a variable in php, specifically every time a button is pushed the value of a variable must increase.</p>

<p>The code is the following:</p>

<pre><code>     &lt;!DOCTYPE html&gt;
     &lt;html lang="en"&gt;
     &lt;head&gt;
        &lt;meta charset="utf-8"&gt;
        &lt;title&gt;Test&lt;/title&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"&gt;&lt;/script&gt;
        &lt;script&gt;
             function echoVal()
             {
                 alert("&lt;?php val(); ?&gt;");
             }
        &lt;/script&gt;

        &lt;?php
             function val() {
                static $a = 0;
                $a++;
                echo $a;
             } 
        ?&gt;

        &lt;button onclick="echoVal()"&gt; Increase &lt;/button&gt;
     &lt;/body&gt;
     &lt;/html&gt;
</code></pre>

<p>The main problem is that the value of the variable $a is always 1, no increasing its value when the button is pushed. I have saved the file with extension .php (I am not sure if that will make any difference, but I just mention it).</p>

<p>Any suggestion to solve this issue?</p>

<p>Thanks in advance for any suggestion.</p>

<p>Edited Code:</p>

<pre><code>     &lt;!DOCTYPE html&gt;
     &lt;html lang="en"&gt;
     &lt;head&gt;
        &lt;meta charset="utf-8"&gt;
        &lt;title&gt;Test&lt;/title&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"&gt;&lt;/script&gt;
        &lt;script&gt;
             function echoVal()
             {
                 alert("&lt;?php val(); ?&gt;");
             }
        &lt;/script&gt;

        &lt;?php
             function val() {
                static $a = 0;
                $a++;
                $filename ='infoX.txt';
                if (is_writable($filename)) {
                    if (!$handle = fopen($filename, 'w')) {
                        echo "Cannot open file";
                    } else {
                        if (fwrite($handle, $a) == FALSE) {
                            echo "Cannot write to file";
                        } else {
                            echo $a;
                        }
                        fclose($handle);
                    }  
               } else {
                    echo "The file is not writable";
               }
             } 
        ?&gt;

        &lt;button onclick="echoVal()"&gt; Increase &lt;/button&gt;
     &lt;/body&gt;
     &lt;/html&gt;
</code></pre>
