PHP Variables

Learn via video courses
Topics Covered

Overview

In PHP (Hypertext Preprocessor) a variable is a container that can store a value or a reference to a value. Variables can hold various data types such as strings, integers, floats, and booleans. PHP variable names must start with a dollar sign ($), followed by a letter or underscore, and then followed by any combination of letters, numbers, or underscores. PHP variables are dynamic, meaning they can change their data type or value during runtime. One must understand the concept of php variables to start programming in PHP.

Introduction

PHP (Hypertext Preprocessor) is a popular server-side scripting language that is widely used for web development. One of the most fundamental concepts in PHP is variables. Variables are used to store data or values that can be manipulated or used throughout a PHP script.

In simple terms, a variable is like a container that holds a specific piece of information, such as a string of text, a number, or a boolean value. All the PHP variables are dynamic, which means that they can be assigned and reassigned values during the execution of a script.

Variable Naming Rules

Since we know that in php variables are used to store values that can be accessed and manipulated throughout the script. Variable naming is an important aspect of programming as it helps in a better understanding of the code and makes it easier to maintain. The above are some of the variable naming rules in PHP:

1. Variables in PHP Must Begin with a Dollar sign ($) Followed by a Letter or an Underscore:

In PHP all the variable names must begin with a dollar sign followed by a letter or an underscore, they cannot begin with a number or any other special character. Let us see an example:

Here in the above example the $Firstname = "Yash" and $_surname = "Agarwal";are valid as they are beginning with a dollar sign($) followed by a letter or an underscore, but $151020name = 25; is invalid as it begins with a dollar sign but is not followed by a letter or an underscore, it begins with a number.

2. Variables in PHP can be Assigned values of any Data Type Including Integer, Float, String, Array, Object, Boolean, and Null:

In PHP we can assign values if any data type which can include integer, float, string, array, object, boolean, and null. Let us see an example:

In the above code, we can see that the variables in PHP can be assigned values of any data type including integer, float, string, array, object, boolean, and null.

3. Variable Names in PHP are Case-Sensitive. :

In PHP the variable names are case sensitive hence if we write $yash and $Yash both of them are considered as two different variables. Let us see an example:

Output

In the above example, we can see that except for the first line, all the lines will have an error because all the PHP variables are case sensitive, we should always remember this while coding in PHP.

Creating PHP Variables

You can create a variable in PHP by using the dollar sign ($) followed by the variable name. The variable name can be any combination of letters, numbers, and underscores. However, the variable name must start with a letter or underscore, and it cannot start with a number.

Now let us see an example of how we can declare a variable in PHP:

Now let us read about each of them and see how to declare them:

Declaring a string Variable in PHP

To declare a string variable in PHP we have to simply assign a string value to a variable using the following syntax:

Now let us see how to declare an integer variable in PHP:

Declaring an Integer Variable

To declare an integer variable in PHP we have to simply assign an integer value to a variable using the following syntax:

In the above code, we have simply declared an integer variable called $a and assigned it the value 1520. Now let us see how to declare a float variable in PHP:

Declaring a Float Variable

To declare a float variable in PHP we have to simply assign a float value to a variable using the following syntax:

In the above code, we have simply declared a float variable called $b and assigned it the value 15.20. Now let us see an example declaring all three variable types together in PHP:

Output

Examples

Sum of two variables

Let us see an example of adding two PHP variables together:

Output

Case Sensitive

In PHP, case sensitivity refers to whether or not the language distinguishes between uppercase and lowercase letters in variable names, function names, and other identifiers. Here's an example of case sensitivity in PHP:

Output

Rules: PHP Variables Must Start With Letter or Underscore Only

A variable name in PHP cannot start with a number or any other character except a letter or underscore. For example, the following variable names are valid in PHP:

While the following variable names are not valid:

Now let us read about variable scopes in PHP:

Varaiable Scopes In PHP:

In PHP, variable scope refers to the range in which a variable can be accessed and manipulated within a PHP script. There are three main types of variable scopes in PHP: global, local, and static. Each type of scope has its own rules regarding where and how variables can be accessed. Let us read about each scope in detail:

1. Global scope:

A variable declared outside a function or class has a global scope, meaning it can be accessed from anywhere in the script. It can be accessed both inside and outside functions. Let us see an example:

Output

2. Local scope:

A variable declared inside a function has a local scope, meaning it can only be accessed within that function. It cannot be accessed outside of that function.

Output

3. Static scope:

A variable declared inside the function with the static keyword has a static scope. It means that the variable retains its value between function calls. It can only be accessed inside the function where it was declared. Example:

Output

Examples of Using Variables in Arithmetic, strings, and Arrays

Arithmetic:

PHP allows you to perform arithmetic operations using variables. For example, you can use variables to add, subtract, multiply, and divide numbers. Here's an example:

Output

Strings:

PHP also allows you to work with strings using variables. For example, you can concatenate two or more strings together using the . operator. Here's an example:

Output

Arrays:

PHP also has built-in array support, allowing you to store multiple values in a single variable. You can use variables to access and manipulate the values in an array. Here's an example:

Output

Conclusion

  • Variables in PHP are used to store values that can be accessed and modified throughout the program.
  • PHP variables are dynamically typed, meaning they can hold values of any data type.
  • To declare a variable in PHP, use the dollar sign ($) followed by the variable name.
  • PHP variables are case sensitive, so variableandvariable and Variable are considered different variables.

MCQ'S

a. Which of the following is not a valid variable type in PHP?

i) boolean
ii) integer
iii) array
iv) char
Correct answer: <iv>

b. What is the scope of a variable defined inside a function in PHP?

i) Global scope
ii) Local scope
iii) Class scope
iv) Object scope
Correct Answer: <ii>

c. Variables in PHP must begin with which sign?

i)?
ii)!
iii) $
iv) %
Correct Answer:<iii>