{"id":282,"date":"2018-01-04T12:41:05","date_gmt":"2018-01-04T12:41:05","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=282"},"modified":"2018-01-04T12:41:55","modified_gmt":"2018-01-04T12:41:55","slug":"use-static-variable","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/use-static-variable\/","title":{"rendered":"Use Static Variable"},"content":{"rendered":"<p>Generally, when the function completes its execution the variable data is deleted automatically by a php script. But sometimes we require those data to continue on another job. In such case, you can use a static variable to do the job.<\/p>\n<p>By using the static keyword when you first declare the variable will keep the script running even after the code is executed. <\/p>\n<p><strong>Example code:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\nfunction mddir() {<br \/>\n    static $x = 5;<br \/>\n    echo $x;<br \/>\n    $x++;<br \/>\n}<br \/>\nmddir();<br \/>\necho &quot;&lt;br&gt;&quot;;<br \/>\nmddir();<br \/>\necho &quot;&lt;br&gt;&quot;;<br \/>\nmddir();<br \/>\n?&gt; [\/php]<\/p>\n<p>The code will produce below result.<\/p>\n<p><code><br \/>\n5<br \/>\n6<br \/>\n7<\/code><\/p>\n<p>In the above code, we have a simple function with the static variable <code>$x<\/code> that has value <code>5<\/code>. Also, in the echo statement, we have asked php script to auto-increment the value by one.<\/p>\n<p>Once it is executed the script will use the static variable to display the outcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generally, when the function completes its execution the variable data is deleted automatically by a php script. But sometimes we require those data to continue<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-282","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=282"}],"version-history":[{"count":3,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/282\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}