Logo
/
Programming
/
PHP Course
/

Variable naming

PHP: Variable naming

$greeting serves as an example of a simple and clear variable name. But often names like $name, $email, or $price are not enough. For example, you may need to describe a user's name, the total number of orders, or the maximum message length. Such names already consist of several words. What will a variable name look like in this case?

Different programming languages use different naming styles. This determines what a variable name made of several words will look like. For example, here is how you can write a variable that stores the maximum message length:

  1. $maxmessagelength
  2. $maxMessageLength
  3. $max-message-length
  4. $max_message_length

Main styles

Here are three popular approaches to writing compound names:

  • kebab-case: words are separated by a hyphen: max-message-length.

    Does not work in PHP, since the hyphen (-) is interpreted as the subtraction operator.

  • snake_case: words are separated by an underscore: $max_message_length.

    This is the standard, for example, in Python. In PHP, many built-in functions are named this way, but this style is not used for variables.

  • CamelCase: each word starts with a capital letter, without separators: MaxMessageLength. It has a variation called lowerCamelCase, in which the first word is written in lowercase: $maxMessageLength.

    It is exactly lowerCamelCase that is the standard for variables in PHP.

How to do it right in PHP

<?php

$userName = "Daenerys";
$maxLength = 280;
$totalOrdersCount = 17;
  • The first word is written in lowercase
  • Each following word starts with a capital letter
  • Separators between words are not used

Each language has its own rules. PHP has a generally accepted coding standard that describes many such aspects. You don't have to remember all of its rules: the code is automatically checked by linters, which we already got acquainted with earlier.

How not to do it

You should not include the data type in a variable name. Such names are harder to read and quickly become outdated. For example, $userNameString or $messagesNumber describe not the meaning of the variable, but its technical implementation.

A name should answer the question "what is stored?", not "what type is it?". That's why it's better to write $userName instead of $userNameString, and $messagesCount instead of $messagesNumber.

Instructions

  1. Create two variables: the number of items in an order and the price of one item. Use lowerCamelCase. What you name the variables is up to you, the tests don't check it.
  2. Write the number 20 to the first variable, and 100 to the second.
  3. Calculate the total cost of the order.
  4. Print the result to the screen.

Tips

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!