Tuesday, August 25, 2015

example of static scope in php

static means it's initiated only one time
output will be :
value of x inside sample is 1
value of x inside sample is 2
value of x inside sample is 3



<?php

function scope(){
    echo "Value of x is ", $x;
}
//static means it's initiated only once
//first it's initiated by 0
function sample() {
    static $x = 0;
    $x = $x + 1;
    echo "value of x inside sample is ", $x, "<br>";
}

sample();
sample();
sample();

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }