Learn what variables are in PHP and how to use them. This beginner-friendly guide explains variable declaration, assignment, types, and case sensitivity with examples.
$
).Understanding PHP Variables:
=
) is used to give a value to a variable.$name
and $Name
are different.<?php
$txt = "PHP is fun!";
echo $txt; // Output: PHP is fun!
?>
<?php
$var = 10; // Integer
$var = "Hello!"; // String
$var = 3.14; // Float
?>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
$greeting
and assigns it the value "Hello, PHP!"
.echo
to display the value of $greeting
.Expected Output:
Hello, PHP!
$number
and assign it the value 10
.$number
using echo
.$number
to 20
and display it again.$number
to "twenty"
(a string) and display it.Expected Output:
10
20
twenty
$a
and $b
, and assign them the values 7
and 3
, respectively.$a
and $b
.
echo
to output each result with a label (e.g., “Sum: 10”).Expected Output:
Sum: 10
Difference: 4
Product: 21
Quotient: 2.333...
$dynamic
and assign it an integer value (e.g., 100
).echo
and gettype()
(e.g., echo gettype($dynamic);
).$dynamic
to a string (e.g., "Dynamic PHP"
) and display its type again.$dynamic
to a float (e.g., 3.14
) and display its type once more.Expected Output:
100
integer
Dynamic PHP
string
3.14
double
$firstName
(assign your name) and $lastName
(assign your surname).$fullName
and display it.
.
in PHP), e.g., $fullName = $firstName . " " . $lastName;
.Expected Output:
John Doe
[1] “PHP Variables,” Tutorial Republic. [Online]. Available: https://www.tutorialrepublic.com/php-tutorial/php-variables.php. [Accessed: Apr. 29, 2025].