How can one efficiently count the number of "{" and "}" in PHP code?

To efficiently count the number of "{" and "}" in PHP code, you can use a simple approach by iterating through each character in the code and incrementing separate counters for "{" and "}" whenever they are encountered. This way, you can keep track of the total count of both opening and closing curly braces.

<?php
$code = '<?php // Your PHP code here';

$openBraceCount = 0;
$closeBraceCount = 0;

for($i = 0; $i < strlen($code); $i++) {
    if($code[$i] == '{') {
        $openBraceCount++;
    } elseif($code[$i] == '}') {
        $closeBraceCount++;
    }
}

echo "Number of '{' : " . $openBraceCount . PHP_EOL;
echo "Number of '}' : " . $closeBraceCount . PHP_EOL;
?>