What’s wrong with this code
Ten points if you can figure out why the below code doesn’t work as expected
$s = "$123,456.78";
if(preg_match("#^\$([,0-9]+)\.\d\d$#", $s, $matches) == 1)
{
echo "It works!\n";
}
Yep, because I was using double quotes. PHP thought that ‘\$’ meant the literal ‘$’ (as opposed to a variable substitution) not ‘\$’ which was what I wanted to pass to preg_match.
I should have either escaped the back slash (’\\$‘) or just used single quotes (as they don’t support variable substitution).