Why does PHP allow you to create identifier names with special characters?

11

Normally in programming languages and databases, identifier names (variables, functions, classes, methods, tables, fields, etc.) must begin with a letter or underline , numbers can come in sequence and to avoid problems accented and special characters should be avoided.

In PHP, variables must start with a $ fault, but it is possible to create identifiers with strange names like the ones below:

<?php
header('Content-Type: text/html; charset=utf-8');

function executarAção($ação){
    echo $ação .' <br>';
}

function 웃(){
    echo 'boneco de palito da uml é você mesmo? <br>';
}

function variavelEstranha(){
    ${0} = 'Olá mundo estranho :D';
    echo ${0};  
}

executarAção('kboom');
웃();
variavelEstranha();

Question:

Why does PHP allow you to create variables and functions with special characters?

Example

Note: In my test, I saved the file as utf-8.

    
asked by anonymous 28.10.2014 / 21:30

1 answer

8

Because programmers from other parts of the world, such as Saudi Arabia, for example, would probably like to create a function like this:

نفعلشيئا()

There is no reason for the compiler to forbid the use of "special characters" in the source code.

You can agree to not use in your project for some specific reason, and you can still set up a source code analyzer to break the build if you encounter characters that are forbidden by convention. But the compiler does not know some aspects of the culture of his team or is not worried.

Update: Sorry for the use of the word "compiler" :-) Some languages are only interpreted. But the answer is the same.

By the way, your example, 웃, seems to be a valid character in Korea ("smiling", according to Google Translate).

Update 2 - a brief reflection: Why do not we use special characters in our systems?

What makes a character special?

In this documentation from a Windows feature, I found an interesting definition:

  

Special characters are characters that are not found in the   keyboard.

Now on my keyboard I see everything I need to write "executeAction" . So some definition of special character is wrong (ours here in this question or that of the Microsoft documentary).

I liked this other definition :

  

They are characters like periods, symbols (@ *!%;:.) or spaces in   which are not accepted by the registration system for filling   of the user name and password fields.

That's a good definition. It establishes the domain for its definition of special characters: certain fields of the registration system.

So I conclude that the definition of "special characters" varies by context. What's special here may not be there.

Our definition of special character:

We, programmers of Portuguese language, consider special to the characters that are part of our life: cedilha and accentuation. We do not like them in our systems because ... because ... why? Of course, we do not even need a formal definition, our experience reveals that this only gives a problem:

  • Each programmer saves the file with a different encoding, so the cedilla you saved on your machine appears as a strange symbol in mine.

  • Each application that uses our database has been compiled or will be interpreted using a different encoding, so the SELECT that I wrote in an application using cedilla will not find the table created with cedilla from another application. >

  • Each programmer has a different "notion" of which words are accentuated, so the function I name with an accent will not be easily found by another programmer who believes the word does not carry an accent.

    / li>

  • And so on ...

Conclusion: Now, a well-meaning PHP programmer should find it cool that Portuguese-speakers can use their common accent in source code. If the interpreter has no problem dealing with these characters, why limit its use? And, of course, as I mentioned, there are many languages out there using the most diverse "special characters."

The problems I've mentioned can be ignored by this PHP programmer because it's "easy" to eliminate them: everyone just uses UTF-8 encoding in their editors and other tools, and everyone knows their language well. We do not bet on that (I at least do not bet) and we follow our tradition of agreeing not to use special characters.

Of course, prior to the advent and massification of more modern coding standards (UTF-8), problems with the use of "special characters" were serious because coding standards were limited. Even at that time the compilers and interpreters could not do much to limit the use of characters because, as already said, the definition of which characters are special will vary.

    
28.10.2014 / 21:55