Is it a good idea to declare variables with accents?

9

I was taking a look at the PHP Manual on the variables . There I found a snippet of code that left me with the "back foot".

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

I always see in php beginner tutorials that it is not a good idea to declare variables with accents and the like. 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 this result:

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

)
string(7) "Wallace"

See the IDEONE that everything went well.

Another point I'd like to put is that when I was learning a bit of java , I saw a snippet of code like this:

JButton botão = new JButton();

And that also worked correctly.

Questions

  • Is it a common practice among programming languages to not use accented characters in the variable declaration?

asked by anonymous 01.09.2015 / 17:57

3 answers

6

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 from languages other than English, interpretation is not guaranteed to be the same.

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

$variável

In my:

$vari?vel
    
01.09.2015 / 19:27
5

Wallace, using accentuation of variables is allowed, but not a good habit to use.

There are some naming standards, an example of which is the camelCase, where the variables are named with the first word totally minuscule and the others have the first letter capitalized. ex: external customer.

I found this question in the stackexchange which discusses the best pattern to use in php.

  

Is it a common practice among programming languages to not use accented characters in the variable declaration?

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

You've cited java variable names with accentuation, I found a page that speaks one little of the standard that java uses. What he talks about accentuation is, encoding problems can occur and either your programming partner is not accustomed to accentuation, this can happen for example in an open source project where people from various places are coding.

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

It does not mean that accent is forbidden, just not recommended.

    
01.09.2015 / 18:05
3
  

Is there a possible problem in declaring variables with accents (such as $ joao, $ is_certain, $ é_array) in relation to the PHP language?

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

  

Is it a common practice among programming languages to not use accented characters in the variable declaration?

Yes

    
01.09.2015 / 18:30