PHP Variables - Data Types and Constants
PHP Variables - Data Types and Constants
TYPES AND
CONSTANTS
PHP Variables
• Variables are used to store data, like string of text,
numbers, etc. Variable values can change over the course
of a script. Here're some important things to know about
variables:
define(name, value)
OUTPUT:
Good Evening
Constants(contd.)
• PHP constant: const keyword - PHP introduced a
keyword const to create a constant. The const keyword
defines constants at compile time. It is a language
construct, not a function. The constant defined using
const keyword are case-sensitive.
<?php
const WISHES="Good day";
echo WISHES;
?>
OUTPUT:
Good day
Constants(contd.)
• Constant() function - There is another way to print the
value of constants using constant() function.
constant (name)
Constants(contd.)
<?php
define("WISHES", "Good Evening");
echo WISHES. "<br>";
echo constant("WISHES");
//both are similar
?>
OUTPUT:
Good Evening
Good Evening
Constants(contd.)
• PHP Constant Arrays - In PHP, you can create an Array
constant using the define() function.
<?php
define("courses", [
"PHP",
"HTML",
"CSS"
]);
echo courses[0];
?>
OUTPUT:
PHP
Constants(contd.)
• PHP Constant Arrays - In PHP, you can also create an
Array constant using const keyword.
<?php
const WISHES=array("PHP",
"HTML",
"CSS");
echo WISHES[0];
?>
OUTPUT:
PHP
Constants(contd.)
• Constants are Global - Constants are automatically
global and can be used across the entire script.
<?php
define("WISHES", "Good Evening");
function test() {
echo WISHES;
}
test();
?>
OUTPUT:
Good Evening