PHP Tutorial
What is PHP Environment Setup of PHP PHP Syntax PHP Comments PHP Echo & print PHP Variables PHP Scope Variables PHP $ and $$ Variable PHP Constants PHP Operators PHP Data TypesPHP Conditional Statements
PHP Loop Type
PHP Arrays
PHP String PHP FunctionsPHP Form Examples
Form Intro Get & Post Form Validation / Required PHP Complete FormPHP Advanced
PHP Include / Require PHP Date & Time PHP Session PHP Cookies PHP File Handling PHP Open File PHP Read File PHP Write File PHP Append File PHP Delete File PHP File Upload PHP Sending Mail PHP Error HandlingPHP Constants
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.
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
PHP constants can be defined by 2 ways:
- Using const keyword
- Using define() function
Note: Unlike variables, constants are automatically global across the entire script.
<?php
const MESSAGE = "Hello Mates Welcome to Elevenstech";
echo MESSAGE;
?>
Output:
Hello Mates Welcome to Elevenstech
Create a PHP Constant
To create a constant, use the define() function.
Syntax: define(name, value, case-sensitive)
Parameters:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-sensitive: Specifies whether the constant name should be case-sensitive. Default is false
Example:
Create a constant with a case-sensitive name:
<?php
define("WELCOME", "Welcome to elevenstechwebtutorials.com");
echo WELCOME;
?>
Output : Welcome to elevenstechwebtutorials.com
Example:
Create a constant with a case-insensitive name:
<?php
define("WELCOME", "Welcome to elevenstechwebtutorials.com", true);
echo welcome;
?>
Output : Welcome to elevenstechwebtutorials.com
PHP Constant Arrays
In PHP7, you can create an Array constant using the define() function.
Example:
Create an Array constant:
<?php
define("message", [
"Welcome",
"to",
"Elevenstech"
]);
echo message[2];
?>
Output : Elevenstech
Constants are Global
Constants are automatically global and can be used across the entire script.
Example:
This example uses a constant inside a function, even if it is defined outside the function:
<?php
define("WELCOME", "Welcome to elevenstechwebtutorials.com");
function myFirstCode()
{
echo WELCOME;
}
myFirstCode();
?>
Output : Welcome to elevenstechwebtutorials.com
Difference between PHP Constants and PHP Variables
PHP Constants:
- In PHP constants there is no need to use $ sign.
- A PHP constant once defined cannot be redefined.
- We can not define a constant using any simple assignment operator rather it can only be defined using define().
- The data type of PHP constant cannot be changed during the execution of the script.
- PHP constants are automatically global across the entire script.
- Usually, constants are written in numbers.
- PHP constant is comparatively slower than PHP variable
PHP Variables:
- In PHP Variables the $ sign is been used.
- A PHP variable can be undefined as well as can be redefined.
- The data type of the PHP variable can be changed during the execution of the script.
- On the other hand, variables are written in letters and symbols.
- PHP variables are not automatically global in the entire script.
- We can define a variable using a simple assignment operation(=).
- A PHP variable is comparatively faster than the PHP constant
SEARCH POST HERE
Support Us
Subscribe My YouTube Channel
Join Our Telegram Channel & Support Eachother
CATEGORIES
INTERVIEW QUESTIONS
POPULAR POSTS
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date