Pregunta

I have always been under the impression that all php functions had to begin with [a-zA-Z].

For instance, this would work:

function a1() {
  return "Something, because I'm written properly.";
}

... while this would not:

function 1a() {
  return "Nothing, because you'll encounter an error before this function ever runs.";
}

However the character that displays as a result of rawurldecode('%E2%80%A9'), when the document displaying it has a declared content type of UTF8, can also be used to define a function.

In a text editor that does not display hidden characters, it ultimately looks the the function has been defined as function () { which can then be executed by calling (what appears to be no more than) ();

I can't paste the source code and have it still show up properly, so here are some screenshots. The first is a screenshot of what's been displayed in my browser, and the second is a screenshot of the actual source code as displayed inside my text editor (TextWrangler) with Display hidden characters turned on:

Browser:


enter image description here


Source code:


enter image description here

My question: is this intentional? Should I be able to define functions/variables with non-printing characters and still have them work flawlessly? And if so, is it documented somewhere?

I couldn't find any info about it, but I (obviously) don't know everything.

Thanks!

¿Fue útil?

Solución

From manual:

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

As explained in the other answer linked, regular expression is applied byte-per-byte, allowing "many weird Unicode names".

Doing it that way has some side-effects like you've seen. However, I can't imagine it was the original intent of people behind PHP, it would be just a direct consequence of the way they've implemented it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top