PHP Variables

For storing information in PHP, use variables. Variables are also known as containers.Ā  For example, easily store numeric values such as 10, 20, 30, etc in variables. With that, also store any string in variables as shown in the first PHP program, etc. Letā€™s learn about the syntax,

Syntax

Variable in PHP begins with a $ sign. After that, type the variable name. Hereā€™s an example:

Here’s the output,

PHP Variables

As shown above, the following can be seen, with three variables,

  • Variable $a holds the value 10
  • Variable $b holds the value 20
  • Variable $result holds the value; We have two values, as shown above.
  • Any text inside quotes is a string.

PHP Variable Rules

Some rules are defined for the usage of variables in PHP. Letā€™s see them one by one with examples:

  • A variable name must start with a letter or the underscore character.
    For example, $dept
  • A variable starts with the $ sign, followed by the name of the variable.
    For example, $result
  • A variable name cannot start with a number.
    For example, ā€œ2valueā€ isnā€™t the correct way to name a variable.
  • A variable name can only contain alpha-numeric characters and underscores.
    For example, result_rank, value1
  • Variable names are case-sensitive.
    For example, $result and $RESULT are two different variables.

PHP variable naming conventions

PHP Tutorial
PHP Arrays
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading