PHP Tutorial
- PHP Tutorial
- Install PHP
- PHP Code
- PHP Echo and print
- PHP Variable
- PHP Variable Scope
- PHP $ and $$
- PHP Constants
- PHP Data Types
- PHP Operators
- PHP Comments
Control Statement
- PHP If else
- PHP Switch
- PHP For Loop
- PHP foreach loop
- PHP While Loop
- PHP Do While Loop
- PHP Break
- PHP Continue
PHP Functions
- PHP Functions
- Parameterized Function
- PHP Call By Value and reference
- PHP Default Arguments
- PHP Variable Arguments
- PHP Recursive Function
PHP Arrays
PHP Strings
PHP Math
PHP Form
PHP Include
State Management
PHP File
Upload Download
PHP OOPs Concepts
- OOPs Concepts
- OOPs Abstract Class
- OOPs Abstraction
- OOPs Access Specifiers
- OOPs Const Keyword
- OOPs Constructor and destructor
- Encapsulation
- Final Keyword
- OOPs Functions
- OOPs Inheritance
- OOPs Interface
- OOPs Overloading
- OOPs Type Hinting
PHP MySQLi
- MySQLi CONNECT
- MySQLi CREATE DB
- MySQLi CREATE Table
- MySQLi INSERT
- MySQLi UPDATE
- MySQLi DELETE
- MySQLi SELECT
- MySQLi Order by
PHP Topics
- Compound Types
- is_null() Function
- Special Types
- Inheritance Task
- Special Types
- MVC Architecture
- PHP vs. JavaScript
- PHP vs. HTML
- PHP vs. Node.js
- PHP vs Python
- PHP PDO
- Top 10 PHP frameworks
- phpMyAdmin
- Count All Array Elements
- Create Newline
- Get Current Page URL
PHP Mail
PHP constants are name or identifier that can’t be changed during the execution of the script except for magic constants, which are not really constants. PHP constants can be defined by 2 ways:
- Using define() function
- Using const keyword
Constants are similar to the variable except once they defined, they can never be undefined or changed. They remain constant across the entire program. PHP constants follow the same PHP variable rules. For example, it can be started with a letter or underscore only.
Conventionally, PHP constants should be defined in uppercase letters.
Note: Unlike variables, constants are automatically global throughout the script.
PHP constant: define()
Use the define() function to create a constant. It defines constant at run time. Let’s see the syntax of define() function in PHP.
- define(name, value, case-insensitive)
- name: It specifies the constant name.
- value: It specifies the constant value.
- case-insensitive: Specifies whether a constant is case-insensitive. Default value is false. It means it is case sensitive by default.
Let’s see the example to define PHP constant using define().
<?php
define("MESSAGE","Hello JavaTpoint PHP");
echo MESSAGE;
?>
Output:
Hello JavaTpoint PHP
Create a constant with case-insensitive name:
<?php
define("MESSAGE","Hello JavaTpoint PHP",true);//not case sensitive
echo MESSAGE, "</br>";
echo message;
?>
Output:
Hello JavaTpoint PHP Hello JavaTpoint PHP
<?php
define("MESSAGE","Hello JavaTpoint PHP",false);//case sensitive
echo MESSAGE;
echo message;
?>
Output:
Hello JavaTpoint PHP Notice: Use of undefined constant message - assumed 'message' in C:\wamp\www\vconstant3.php on line 4 message
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 MESSAGE="Hello const by JavaTpoint PHP";
echo MESSAGE;
?>
Output:
Hello const by JavaTpoint PHP
Constant() function
There is another way to print the value of constants using constant() function instead of using the echo statement.
Syntax
The syntax for the following constant function:
- constant (name)
<?php
define("MSG", "JavaTpoint");
echo MSG, "</br>";
echo constant("MSG");
//both are similar
?>
Output:
JavaTpoint JavaTpoint
Constant vs Variables
Constant | Variables |
---|---|
Once the constant is defined, it can never be redefined. | A variable can be undefined as well as redefined easily. |
A constant can only be defined using define() function. It cannot be defined by any simple assignment. | A variable can be defined by simple assignment (=) operator. |
There is no need to use the dollar ($) sign before constant during the assignment. | To declare a variable, always use the dollar ($) sign before the variable. |
Constants do not follow any variable scoping rules, and they can be defined and accessed anywhere. | Variables can be declared anywhere in the program, but they follow variable scoping rules. |
Constants are the variables whose values can’t be changed throughout the program. | The value of the variable can be changed. |
By default, constants are global. | Variables can be local, global, or static. |