2

I'm reading some RAW code from here http://www.zfsnippets.com/snippets/view/id/17/output/raw/table-view-helper where the author is doing a lot of slash escaping like this \'class\'.

protected $_attribs = array(
    \'class\' => \'table\',
    \'cellpadding\' => \'0\',
    \'cellspacing\' => \'0\',
    \'border\' => \'0\',
);

From what I understand it's supposed to be code that will be used to construct the markup of a table. Why exactly is the author doing it this way and isn't there a better way than this hideous workaround. I was thinking double quotes should do "'class'" but not sure. Anyone knows for sure what's the right way of doing this?

This is everywhere else where this var is called. It's basically being used after that in a getter setter style, with array_merge I guess

public function setAttribs($options = null)
{
    if (is_array($options)) {
        $this->_attribs = array_merge($this->_attribs, $options);
    }
    return $this;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
silow
  • 4,256
  • 3
  • 21
  • 24
  • Are double-quotes less "hideous"? Is backslash a "workaround"? It seems like it would be exactly the same number of characters either way. – Ken Dec 17 '10 at 01:59
  • 1
    The full code file please? I think it's a code snippet that is getting `eval`'ed. I tested ` \'y\');` and a syntax error happened as expected. Using double quotes will require escaping `$`. A better way to avoid escaping is to use nowdocs http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc – Ming-Tang Dec 17 '10 at 02:00
  • 1
    @Ken, Well, I've stated in the question that for me, yes, backslashes are breaking the readability of the code and I'm asking about alternatives. – silow Dec 17 '10 at 02:01
  • silow: because this code is probably include in a variable which got some quote around, or it's wrong – RageZ Dec 17 '10 at 02:02
  • 1
    If it's copied from a blog, PHP's magic quotes on his server screwed that code snippet? – Ming-Tang Dec 17 '10 at 02:03
  • I'd agree with SHiNKiROU. That's likely a magic quotes problem. – s3v3n Dec 17 '10 at 02:26
  • @silow - when you ask questions, get answers then delete the question, I wouldn't expect help from those answering again, as you *completely* wasted their time. – Nick Craver Dec 17 '10 at 10:41

1 Answers1

7

I think that just the blog or site where this code belongs wrong escaped it. Just ignore them.

s3v3n
  • 8,203
  • 5
  • 42
  • 56
  • Yeah, the site has run the raw code through `addslashes()`, probably inadvertantly. – staticsan Dec 17 '10 at 02:20
  • It would be RAW code if the file would be requested directly and it will not have enough permissions to be executed, so it would be sent directly to the output. However as you can see the url seems to be different from a regular file path, so that rather seems to be a escape problem. – s3v3n Dec 17 '10 at 02:24