What will be the output of the following code?< ?phpfunction track() {static $count = 0;$count++;echo $count ;}track();track();track();?>
Question
What will be the output of the following code?
<?php
function track() {
static $count = 0;
$count++;
echo $count;
}
track();
track();
track();
?>
Solution
The output of the provided PHP code will be:
123
Explanation:
- The function
track()
has a static variable$count
, which means that its value persists across multiple calls to the function. - Each time the
track()
function is called,$count
is incremented by 1 and then echoed.
- First Call:
track();
-$count
is initialized to 0, incremented to 1, and outputs1
. - Second Call:
track();
-$count
is now 1, incremented to 2, and outputs2
. - Third Call:
track();
-$count
is now 2, incremented to 3, and outputs3
.
Therefore, when all three calls are made sequentially, the output will be 123
.
Similar Questions
What will be the output of the following PHP code ?< ?php$on_e = 1;$tw_o = 2;$thre_e = 3;$fou_r = 4;echo "$on_e / $tw_o + $thre_e / $fou_r" ;?>
What will be the output of the following PHP code?1.0 MarksNo OutputAll of the above8 === 81
What will be the output of the following PHP code?< ?php$hello = "Hello World";$bye = "Bye";echo $hello;"$bye";?>
What will be the output of the following PHP code?< ?phpecho $red ;?>0NothingTrueError
What will be the output of the following PHP code ?< ?phpone = 1;two = 2;three = 3;four = 4;echo "one / two + three / four";?>
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.