Variables names in PHP

A very important detail that will make it easier for us to read a PHP code, is to follow a rule regarding the names that we will choose to all the data storage places that we use; among them, the variables.

Variables names in PHP | Learn PHP | A very important detail that will make it easier for us to read a PHP code, is to follow a rule regarding the names that we will choose to all the data storage places that we use; among them, the variables

Choosing a convention to name it

A very important detail that will make it easier for us to read a PHP code, is to follow a rule regarding the names that we will choose to all the data storage places that we use; among them, the variables.

The objective is to try that the name of a variable symbolizes as best as possible the content that is going to store.

Let's think that a code is written in the same way as any text, so that the reader understands it; For this reason, everything that helps to facilitate reading and improve its rapid comprehension, will save us a lot of time.

Unfortunately, it is very common to find code with names of variables written with strange abbreviations, such as $ var_67_x_1. Within a month when we should read that same code, we will have forgotten the data that contained each of the variables.

Let's see an example of code with very difficult names:

<?php
$data_123 = “Pepe”;
$data_46  = “Hello, Mr. $data_123”;
print ($data_46);
?>

Imagine this projected to hundreds or thousands of lines of code impossible to understand at a glance!

In addition to the descriptive names, another recommendation is that it is not advisable to mix variable names in uppercase and lowercase. The best thing is to follow some criteria, some convention, since this is one of the most frequent causes of errors that make us lose time when trying to discover the cause.

The reason is that PHP is a case-sensitive language, or "sensitive" to uppercase and lowercase; that is, $ a is not the same as $ A. For PHP they are two different variables. It is not the same variable that is called $ name than another call $ Name or that $ name. And if we had defined $ name and then made an echo of $ name ... it will not work! And with just reason, since there is no variable that we try to read, there is another one with a difference letter, and that makes it totally different.

That is why it is highly recommended that we follow a rule to name our variables, and that we maintain it throughout all our projects.

For example, some common conventions regarding the names of the variables are the following:

1. All in lowercase:

$variable
$mylongvariable

2. All lowercase, but hyphens between words, if the name of a variable has several terms:

$variable
$my_long_variable

3. In lowercase the first word -or if the variable is of a single word-, but placing a capital letter at the beginning of each new intermediate word, in case the name of the variable has more than one term. This notation is called camel case - because the ups and downs of the capitals interspersed between them look like the humps of a camel! - A very imaginative name. This is the most widespread syntax, and is the most recommended:

$myvariable
$myLongVariable

4. Cada palabra comienza siempre con mayúscula, aunque la variable sea de una sola palabra:

$Variable
$MyLongVariable

It is not recommended to put variables in uppercase names; for example, $ NAME, since it gives rise to confusion with another type of data warehouse called constant, whose names in PHP are written in all uppercase letters (although without the sign $ in front, but still gives rise to confusion, the reading becomes unclear)

Regardless of which of these conventions we choose, it is advisable to choose only one of these notations -any of the previous ones- and always follow it, that will avoid many mistakes.

When we have codes of hundreds of lines it will be very important that we can read that code with the speed with which we read any text and that it is possible to understand that code to understand that code with a single superficial reading, so it is preferable that we get used from the beginning to use names that follow a syntax rule that is comfortable for us.

Let us mention, finally, that in the PHP language we cannot start the name of a variable with a number: the variables must start with a letter or with a low script, although they can then include numbers in their name, letters of the "a" to the "z" and underscores.

We will not use accents, or blank spaces, or half hyphens, or special characters such as asterisks or any other sign in the names of our variables.

Let's see some examples of variable names, correct and incorrect:

<?php
$place1 = “Buenos aires”; /* Correct, you can start with a letter */

$_place _2 = “México”; /* Correct, you can start with a low script */

$3_place = “España”; /* Incorrect, cannot start with a number */
?>

Did you like it or was it useful?

Help us share it in Social Networks

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.

IT ALSO DESERVES TO PAY TO VISIT:

PREVIOUS


Variables in PHP

Not finding what you need?

Use our internal search to discover more information
Sponsored content:
 

Leave your Comment

SPONSOR

Your business can also appear here. More information