Course Content
About Lesson

Variables and Constants

  • 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.";
?>

My name is John and I am 20 years old.

  1. Must start with $.
  2. First character after $ must be a letter or underscore.
  3. Cannot start with a number.
  4. Case-sensitive ($Name and $name are different).
PHP
<?php
echo "This is PHP!"; // statement ends with ;
?>
  • Constants are like variables but cannot be changed once defined.
  • Use define() or const.
PHP
<?php
define("SITE_NAME", "My Website");
echo SITE_NAME;

const PI = 3.14;
echo " Value of PI: " . PI;
?>

My Website
Value of PI: 3.14