How to create variables in PHP?

Creating PHP variables

A variable is a container for storing data.
In PHP, a variable must begin with the $ sign, followed by the name of the variable
When creating variables in PHP, you need to follow some basic rules.

  • Variable names can have any combination of letters, numbers and the underscore;
  • A variable name must start with a letter or the underscore character;
  • A variable name cannot start with a number;
  • A variable name can not contain spaces or hyphens;
  • Variable names are case-sensitive;

Good variable names make your PHP code easy to understand and maintain. Choose names for variables that tell you what the variable is for.

$my_number = 5;

To assign a text to a variable, the text must pe put between double or simple quotes.

$my_text = "This is my text";

In a variable name use camel case or the underscore to distinguish between multiple words, like:

#firstName = "Mihai";
#first_name = "Mihai";

The variable $this is reserved for use in PHP object oriented scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *