The Daily WTF
Cassi’s co-worker (previously) was writing some more PHP. This code needed to take a handful of arguments, like id and label and value, and generate HTML text inputs.
Well, that seems like a perfect use case for PHP. I can’t possibly see how this could go wrong.
echo $sep.”<SCRIPT>CreateField(” . implode(‘, ‘, array_map(‘json_encode’, $args)) . “);</SCRIPT>n”;
Now, PHP’s array_map is a beast of a function, and its documentation has some pretty atrocious examples. It’s not just a map, but also potentially a n-ary zip, but if you use it that way, then the function you’re passing needs to be able to handle the right number of arguments, and– sorry. Lost my train of thought there when checking the PHP docs. Back to the code.
In this case, we use array_map to make all our fields JavaScript safe by json_encodeing them. Then we use implode to mash
To read the full article click on the 'post' link at the top.