Knowee
Questions
Features
Study Tools

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?

&lt;?php
function track() {
    static $count = 0;
    $count++;
    echo $count;
}
track();
track();
track();
?&gt;
🧐 Not the exact question you are looking for?Go ask a question

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.
  1. First Call: track(); - $count is initialized to 0, incremented to 1, and outputs 1.
  2. Second Call: track(); - $count is now 1, incremented to 2, and outputs 2.
  3. Third Call: track(); - $count is now 2, incremented to 3, and outputs 3.

Therefore, when all three calls are made sequentially, the output will be 123.

This problem has been solved

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";?>

1/1

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.