PHP Variables

By Shahnail Khan Last updated : November 30, 2023

Understanding PHP language requires you to know how variables work in PHP. In this tutorial, we will dive deep into the basics of PHP variables, discussing their definition, types, and usage.

PHP Variables

Variables are the symbolic names in PHP that are used to store the value/information and can be manipulated. Variables are like containers that refer to a value.

Declaring PHP Variables

In PHP, the variables are declared using the $ symbol followed by the variable name. Unlike other programming languages, PHP does not require explicit declaration of variable types. For example, in C++, you might declare an integer variable as follows:

C++ declaration:

int myNumber = 42; 

Here, int explicitly declares that myNumber is an integer.

In contrast, in PHP, you can simply write:

$myNumber = 42; 

PHP dynamically determines the type based on the value assigned to the variable, eliminating the need for explicit type declarations.

Example

Let's take another example to understand the variables better.

$name = "John Doe";
$age = 25;
$average_score = 87.5;

Here, $name is a variable that stores a string, $age stores an integer, and $average_score stores a floating-point number.

PHP Variable Naming Rules

  • Variable names must be followed by a $ sign.
  • Variable names must start with a letter or underscore (_).
  • Variable names can only contain letters, numbers, and underscores.
  • Variable names are case-sensitive ($Sum and $sum are considered different variables).

PHP Variable Types

PHP supports various types of variables, including:

  • Scalar Types: These hold single values.
    • Integer: $age = 25;
    • Float: $average_score = 87.5;
    • String: $name = "John Doe";
    • Boolean: $is_student = true;
  • Compound Types: These hold multiple values.
    • Array: $colors = array("Red", "Green", "Blue");
    • Object: $car = new Car();
  • Special Types:
    • Resource: Holds a reference to an external resource.
    • NULL: Represents a variable with no value.

Does PHP Have Variable Types?

Yes, PHP has variable types, but it's unique in that it's a dynamically typed language. Unlike other languages where you have to specify the type of data a variable will hold; PHP dynamically figures out the type based on the value assigned to the variable. This flexibility makes it easier to work with different types of data without explicit type declarations.

$myNumber = 42; // PHP recognizes this as an integer

Printing PHP Variables

To print the PHP variables, use the echo statement, which is often used to print data to the screen.

Example

In this example, we are printing the PHP variables using the echo statement by using two different methods.

<?php
$name = "Alvin";
$age = 23;

// printing
echo "My name is $name, and I am $age years old!";

// Printing New Line (use <br/> tag)
echo "<br/>";

// Using dot (.)
echo "My name is $name" . " and I am " . $age . " years old!";
?>

Output:

My name is Alvin, and I am 23 years old!
My name is Alvin and I am 23 years old!

Declaring Different Types of PHP Variables

The different types of variables in PHP, can declared by assigning the appropriate (different type) values to them.

Example

In this example, we are declaring and printing different types of PHP variables.

<?php
$a = 42; // Integer
$b = 3.14; // Floating-point number
$c = "PHP"; // String
$d = true; // Boolean
$e = [1, 2, 3]; // Array

// pritning all values
echo $a . "</br/>";
echo $b . "</br/>";
echo $c . "</br/>";
echo $d . "</br/>";

// printing of an array using print_r()
print_r($e);
?>

Output:

42
3.14
PHP
1
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Checking Case Sensitivity of PHP Variables

PHP variables are case-sensitive. You have to take care of the variable name. In PHP, variables "num", "Num", and "NUM" are different.

Example

In this example, we are checking the case sensitivity of PHP variables.

<?php
// Declaring a variable
$num = 108;

echo $num . "<br/>";
echo $Num . "<br/>";
echo $NUM . "<br/>";
?>

Output:

108
PHP Notice:  Undefined variable: Num in /home/main.php on line 6
PHP Notice:  Undefined variable: NUM in /home/main.php on line 7

Getting Types of PHP Variables

To get the type of a variable, use the gettype() function, which is a library function in PHP.

Example

In this example, we have some different types of variables, and printing their types.

<?php
// Creating variables with random datatype
$var1 = 1;
$var2 = 1.1;
$var3 = null;
$var4 = "example";
$var5 = false;

// using gettype() function to
// get the data type of the variable
echo gettype($var1) . "<br/>";
echo gettype($var2) . "<br/>";
echo gettype($var3) . "<br/>";
echo gettype($var4) . "<br/>";
echo gettype($var5) . "<br/>";
?>

Output:

integer
double
NULL
string
boolean

What is $$ in PHP?

The double dollar sign ($$) in PHP is used for variable variables. It allows you to create a variable with the name of another variable's value. This concept might seem complex at first, but it can be useful in certain scenarios.

Example

Let's understand by the example given below:

<?php
$first_name = "Include";
$last_name = "Help";
$full_name_variable = "first_name";
echo $$full_name_variable; 
?>

Output:

Include

Here, $$full_name_variable is the same as the $first_name. It takes the value of $full_name_variable and uses it as the variable name.

PHP is a Loosely Typed Language: How & Why?

PHP is a loosely typed language. Because, during the variable declaration, you don't worry about specifying the variable type. PHP converts its type automatically based on the provided value or depending on the context in which the variable is used.

Example

Consider this example to see how the variable's type is converting based on the assignments.

<?php
$a = 3;
echo gettype($a) . "<br/>";

$a = 3.2;
echo gettype($a) . "<br/>";

$a = "Hello";
echo gettype($a) . "<br/>";

$a = [];
echo gettype($a) . "<br/>";

$a = ["Hello", "Hey", "Hi"];
echo gettype($a) . "<br/>";

$a = null;
echo gettype($a) . "<br/>";

$a = false;
echo gettype($a) . "<br/>";
?>

Output:

integer
double
string
array
array
NULL
boolean

PHP Variables FAQs

1. What are the PHP variables?

PHP variables are containers for storing data values. They are identified by a dollar sign ($) followed by the variable name, and their data type is determined dynamically based on the assigned value.

2. How many variables are there in PHP?

PHP supports various types of variables, including scalar types (integer, float, string, boolean), compound types (array, object), and special types (resource, NULL).

3. Can I change the type of a variable in PHP?

Yes, in PHP, you can change the type of a variable by assigning a new value of a different type. For example-

<?php
$myVariable = "100"; // $myVariable is initially a string
echo "Type of \$myVariable before: " . gettype($myVariable) . "<br>"; 

$myVariable = 100; 
echo "Type of \$myVariable after: " . gettype($myVariable);
?>

Output:

Type of $myVariable before: string
Type of $myVariable after: integer

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.