Is it a good idea to declare variables with accents?

Asked

Viewed 1,792 times

11

I was taking a look at the PHP Handbook on the variables. There I found an excerpt of code that left me with the "foot behind".

$täyte = 'mansikka';    // válido; 'ä' é um caracter ASCII (extendido) 228

I always see in tutorials for beginners in PHP affirmations that it is not a good idea to declare variables with accents and similar things. However, now in the PHP Manual I saw this variable $täyte.

I decided to run a test ...

$usuário = 'Wallace';
 
$cidadão = 'Brasileiro';
 
$preferências = ['PHP', 'jQuery'];
      
print_r(compact('usuário', 'cidadão', 'preferências'));
 
var_dump($usuário);

... and I got that result:

Array
(
    [usuário] => Wallace
    [cidadão] => Brasileiro
    [preferências] => Array
        (
            [0] => PHP
            [1] => jQuery
        )

)
string(7) "Wallace"

See on IDEONE that everything went right.

Another point I’d like to make is that when I was learning a little bit of java, I saw a piece of code like this:

JButton botão = new JButton();

And that also worked correctly.

Questions

  • Is there any possible problem in declaring variables with accents (like $joão, $está_certo, $é_array) in relation to the PHP language?

  • It is a common practice among programming languages not to use accented characters in the declaration of variables?

  • 3

    I could give you the answer, however I don’t have much time to elaborate, but first read the answer to this question, http://answall.com/questions/40893/time-of-processing-%C3%A9-affected-by-size-of-vari-names%C3%A1vel and after this article http://local.joelonsoftware.com/wiki/O_M%C3%Adnimo_absoluto_que_todo_development_de_software_absolutely,Positivamente_Precisa_Saber_Sobre_Unicode_E_Conjuntos_de_Caracteres%28Sem_Desculpas! %29, in conclusion I believe that the conversion of Bites could be interpreted differently. due to the fact that these are characters below 127.

  • 1

    Thank you very much, @Guilhermelautert. I’ll give a read yes

  • 2

    http://answall.com/q/16555/91

3 answers

8


The biggest problem with using extended ASCII characters is that not every computer interprets them equally. All computers interpret ASCII the same, but when it comes to extensions - e.g. characters in languages other than English, interpretation is not guaranteed to be equal.

Then, as was said in another answer, this difference in interpretation can interfere with the source code... for example, on your computer:

$variável

In my:

$vari?vel

6

Wallace, the use of accentuation of variables is allowed, but it is not a good habit to use them.

There are some patterns of Omega, an example of them is camelCase, where variables are named with the first word entirely in minuscule and the others have the first letter capitalized. ex: clientExterno.

I found this question in the stackexchange that discusses which is the best default to use in php.

It is a common practice among programming languages not to use accented characters in the declaration of variables?

It is not a common practice in programming languages to use accentuation in variable names. In some languages this is given by encoding problem that may occur.

You mentioned java variable names with accentuation, I found a page that speaks a little about the pattern that java uses. What he talks about accentuation is, there may be encoding problems and or his programming partner is not used to accentuation, this may happen for example in an open source project, where people from various places are coding.

Basically, each language has a standard Nomeclature for variables that is used.

It does not mean that accentuation is prohibited, just not recommended.

  • 1

    Exactly that is the question. Why it is not a good habit to use them? As I said in my question: Is there any possible problem?

  • I liked the question posted. But I see that the question posted is "what are the variable name conventions for PHP?". Even it was closed because it is based on opinions. The very focus is to understand the reason for the non-recommendation

  • 1

    Wallace, there’s that page(https://en.wikipedia.org/wiki/Naming_convention_(Programming)) on the default of Nomeclatura, which lists some of the reasons why the patterns are used. In addition to the reasons on this page I believe that the accent is not used because of the encoding.

3

Is there a possible problem in declaring accented variables (like $joão, $está_certo, $é_array) in relation to the PHP language?

One of the biggest problems I see is that sometimes a developer might have a different encoding in their IDE and end up detonating the variable names

It is a common practice among programming languages not to use accented characters in the declaration of variables?

Yes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.