To declare a variable in PHP, you need to use the dollar sign ($) followed by the variable name. Variable names must start with a letter or underscore, and can contain letters, numbers, and underscores. Variable names are case-sensitive, meaning that $myVar and $myvar are two different variables.
PHP supports different types of variables, including:
- String Variables: These are used to store text data, such as names, addresses, and messages. String variables are enclosed in single or double quotes, depending on the context in which they are used.
- Numeric Variables: These are used to store numeric data, such as integers or floating-point numbers. Numeric variables can be declared using the standard syntax, without quotes.
- Boolean Variables: These are used to store true/false values. In PHP, the value of true is represented by the keyword true, and the value of false is represented by the keyword false.
- Array Variables: These are used to store multiple values in a single variable. An array can be indexed numerically or associatively, depending on the type of data being stored.
- Object Variables: These are used to store instances of classes, which are templates for creating objects with specific properties and methods.
It is important to note that PHP is a loosely typed language, which means that variables can change their type dynamically during runtime. For example, a string variable can be converted to a numeric variable using typecasting.
Read More:- PHP Introduction
In conclusion, variables are a fundamental concept in PHP programming. They are used to store data and manipulate it throughout a script. PHP supports different types of variables, including strings, numbers, booleans, arrays, and objects. Understanding how to work with variables is essential to building dynamic web applications with PHP.
Variables are "containers" for storing information.
Creating (Declaring) PHP Variables
Variables are used to store data that can be accessed and manipulated throughout a script. To declare a variable in PHP, you simply use the dollar sign ($) followed by the variable name. Variable names can contain letters, numbers, and underscores, but they must start with a letter or an underscore. Additionally, variable names are case-sensitive, meaning that $myVar and $Myvar are two different variables.
Once you have declared a variable, you can assign a value to it using the assignment operator (=). The type of the variable is determined by the type of value that is assigned to it. PHP supports several different types of variables, including:
In PHP, variables can also be used in expressions, such as concatenation, arithmetic operations, and comparison operations. This allows you to perform complex operations on data stored in variables.
In conclusion, declaring variables in PHP is a fundamental concept that involves using the dollar sign ($) followed by the variable name. PHP supports several different types of variables, including strings, numbers, booleans, arrays, and objects, which are determined by the type of value that is assigned to them. Understanding how to declare variables and assign values to them is essential to building dynamic web applications with PHP.
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example

After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.
Think of variables as containers for storing data.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:

The following example will produce the same output as the example above:

The following example will output the sum of two variables:

Note: You will learn more about the echo statement and how to output data to the screen in the next chapter.
PHP is a Loosely Typed Language
PHP is a loosely typed language, which means that variables in PHP are not assigned a specific data type during declaration. Instead, variables can dynamically change their type at runtime based on the type of value assigned to them.
In a loosely typed language like PHP, variables can be assigned values of any type, such as strings, numbers, booleans, arrays, and objects, without explicitly specifying the data type. This makes it easy for developers to write code more quickly, as they don't have to worry about strict type declarations.
For example, in PHP, you can declare a variable $x and assign it a string value "Hello, World!". Later on in the code, you can assign it a numeric value, such as $x = 10. This is possible because PHP automatically converts the data type of the variable $x from string to integer.
Loose typing can make coding more convenient and flexible, as it allows developers to perform operations on variables without worrying about their specific data type. However, it also means that errors can occur if you're not careful. For example, if you try to add a string and a numeric variable together, PHP may try to convert the string to a number, which can result in unexpected results.
To avoid such errors, PHP provides typecasting functions that allow developers to explicitly convert a variable's data type. For example, the (int) function can be used to convert a variable to an integer data type, while the (string) function can be used to convert a variable to a string data type.
In conclusion, PHP is a loosely typed language that allows variables to dynamically change their type at runtime. While this can make coding more convenient, it also means that developers need to be careful when performing operations on variables to avoid unexpected results. Typecasting functions can be used to explicitly convert a variable's data type and avoid potential errors.
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch
In conclusion, variables are a fundamental concept in PHP programming. They are used to store data and manipulate it throughout a script. PHP supports different types of variables, including strings, numbers, booleans, arrays, and objects. Understanding how to work with variables is essential to building dynamic web applications with PHP.
Variables are "containers" for storing information.
Creating (Declaring) PHP Variables
Variables are used to store data that can be accessed and manipulated throughout a script. To declare a variable in PHP, you simply use the dollar sign ($) followed by the variable name. Variable names can contain letters, numbers, and underscores, but they must start with a letter or an underscore. Additionally, variable names are case-sensitive, meaning that $myVar and $Myvar are two different variables.
Once you have declared a variable, you can assign a value to it using the assignment operator (=). The type of the variable is determined by the type of value that is assigned to it. PHP supports several different types of variables, including:
- String Variables: These are used to store text data and are enclosed in either single or double quotes.
- Numeric Variables: These are used to store numeric data, such as integers or floating-point numbers.
- Boolean Variables: These are used to store true/false values. The value of true is represented by the keyword true, and the value of false is represented by the keyword false.
- Array Variables: These are used to store multiple values in a single variable. An array can be indexed numerically or associatively, depending on the type of data being stored.
- Object Variables: These are used to store instances of classes, which are templates for creating objects with specific properties and methods.
In PHP, variables can also be used in expressions, such as concatenation, arithmetic operations, and comparison operations. This allows you to perform complex operations on data stored in variables.
In conclusion, declaring variables in PHP is a fundamental concept that involves using the dollar sign ($) followed by the variable name. PHP supports several different types of variables, including strings, numbers, booleans, arrays, and objects, which are determined by the type of value that is assigned to them. Understanding how to declare variables and assign values to them is essential to building dynamic web applications with PHP.
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example

After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.
Think of variables as containers for storing data.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:
- A variable starts with the $ sign, followed by the name of the variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive ($age and $AGE are two different variables)
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:

The following example will produce the same output as the example above:

The following example will output the sum of two variables:

Note: You will learn more about the echo statement and how to output data to the screen in the next chapter.
PHP is a Loosely Typed Language
PHP is a loosely typed language, which means that variables in PHP are not assigned a specific data type during declaration. Instead, variables can dynamically change their type at runtime based on the type of value assigned to them.
In a loosely typed language like PHP, variables can be assigned values of any type, such as strings, numbers, booleans, arrays, and objects, without explicitly specifying the data type. This makes it easy for developers to write code more quickly, as they don't have to worry about strict type declarations.
For example, in PHP, you can declare a variable $x and assign it a string value "Hello, World!". Later on in the code, you can assign it a numeric value, such as $x = 10. This is possible because PHP automatically converts the data type of the variable $x from string to integer.
Loose typing can make coding more convenient and flexible, as it allows developers to perform operations on variables without worrying about their specific data type. However, it also means that errors can occur if you're not careful. For example, if you try to add a string and a numeric variable together, PHP may try to convert the string to a number, which can result in unexpected results.
To avoid such errors, PHP provides typecasting functions that allow developers to explicitly convert a variable's data type. For example, the (int) function can be used to convert a variable to an integer data type, while the (string) function can be used to convert a variable to a string data type.
In conclusion, PHP is a loosely typed language that allows variables to dynamically change their type at runtime. While this can make coding more convenient, it also means that developers need to be careful when performing operations on variables to avoid unexpected results. Typecasting functions can be used to explicitly convert a variable's data type and avoid potential errors.
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch