Constants in PHP

Estudies4you
Constants in PHP

Constants in PHP

Constants like variables are also used to store information. However, the main difference between variables and constants being that the value of constant cannot be changed
Constants are used to lock in values to ensure that they are not modified accidentally
Constants are used to store mathematical constants, passwords, paths etc...
Constants make it easier run a program using different values by allowing you to change the values of constants and running the updated code. This saves the effort of searching the entire program and changing the value at each instance

Examples:
define("PASSWORD", admin);
// PASSWORD is the name of the constant to which the value admin is being assigned.

Script: Constant.php
<?php
define("PASSWORD", "admin");
echo (PASSWORD); echo"</br>";
echo constant ("PASSWORD");
echo"</br>";
echo "PASSWORD";
?>

Output
admin
admin
Password


Pseudo Types
  • Pseudo types are data types used in PHP documentations to explain certain features.
  • They do not really exist.
  • In PHP programming syntax, you would be seeing pseudo types as follows


To Top