Back to questions
JuniorTheoryCommon
What does the program print?
<?php
function nextId()
{
static $id = 0;
$id++;
return $id;
}
echo nextId(), nextId(), nextId(), "\n";123
- Expecting
111, as if$idstarted from0on every call:staticsets it once, and each later call sees the value the previous call left. - Expecting a
staticvariable to be visible outside the function: it keeps its value between calls but is still local tonextId.
- What would the program print if
staticwere removed from line 5? - Why is a
staticcounter hard to reset between two tests?