About Lesson
Variables and Constants
Variables:
- Variables store data.
- Start with
$
symbol. - Case-sensitive.
PHP
<?php
$name = "John"; // string
$age = 20; // integer
echo "My name is $name and I am $age years old.";
?>
Output:
My name is John and I am 20 years old.
Rules for Variables:
- Must start with
$
. - First character after
$
must be a letter or underscore. - Cannot start with a number.
- Case-sensitive (
$Name
and$name
are different).
PHP
<?php
echo "This is PHP!"; // statement ends with ;
?>
Constants:
- Constants are like variables but cannot be changed once defined.
- Use
define()
orconst
.
PHP
<?php
define("SITE_NAME", "My Website");
echo SITE_NAME;
const PI = 3.14;
echo " Value of PI: " . PI;
?>
Output:
My Website
Value of PI: 3.14