Constants in PHP

Constants in PHP cannot be changed or made undefined once they have been defined. Similar to a variable in PHP is a constant. There are two methods to create constants in PHP.
- Using define() function
- Using const keyword
How to Create a PHP Constant
Using define() function:
To create a constant in PHP, we use the define()
function.
Syntax
- name: It is considered the constant name.
- value: It is considered as the constant value.
- case-insensitive: It is considered whether a constant is case-insensitive.
Example
Let’s create a constant with a case-sensitive name:
<!DOCTYPE html> <html> <body> <?php // case-sensitive constant name define("WELCOME", "Hello oraltadalafil.com!"); echo WELCOME; ?> </body> </html>
OUTPUT: Hello oraltadalafil.com!
Let’s create a constant with a case-insensitive name:
Example
<!DOCTYPE html> <html> <body> <?php // case-insensitive constant name define("WELCOME", "Hello oraltadalafil.com!", true); echo welcome; ?> </body> </html>
OUTPUT: oraltadalafil.com!
PHP Constant Arrays
In PHP7, by using the define() function we can create an Array constant.
Example
<!DOCTYPE html> <html> <body> <?php define("fruits", [ "apple", "orange", "mango" ]); echo fruits[0]; ?> </body> </html>
OUTPUT:
apple
Using const keyword:
In PHP, a constant is made using the keyword const. Constants are defined during compilation by the keyword const. Case matters when defining constants with the const keywords.
Example
<?php const MESSAGE="Hello const by oraltadalafil PHP"; echo MESSAGE; ?>
OUTPUT: Hello const by oraltadalafil PHP
Constant() function in PHP
Instead of using the echo statement, we can also print the value of constants using the constant() function.
Syntax : constant (name)
<?php define("MSG", "oraltadalafil"); echo MSG, "</br>"; echo constant("MSG"); //both are similar ?>
OUTPUT:
oraltadalafil
oraltadalafil
Difference between Constant and Variables
Constant | Variable |
A constant can be defined by using the define() function. Any simple task cannot describe it. | A variable can be undefined and as well as it can be redefined easily. |
Once the constant is defined, then it can’t be redefined. | An assignment (=) operator is used to define the variable in PHP. |
Constants are those variables whose values can’t be modified throughout the program. | To declare a variable, we use the dollar ($) sign before the variable. |
There is no need for a dollar ($) sign before constant during the assignment. | Variables can be expressed anywhere in the program, but they follow variable scoping rules. |
By default, constants are global. | The value within the variable can be changed. |
A constant can be defined and accessed anywhere. | In PHP, variables can be local, global, or static. |
PHP Strings

A string is called a sequence of characters, like “Hello world!”
PHP String Functions
There are some String functions that are used to manipulate the data.
· strlen() :
In PHP the strlen() returns the length of a string.
<!DOCTYPE html> <html> <body> <?php echo strlen("Hello world!"); ?> </body> </html>
OUTPUT: 1
str_word_count() :
In PHP the str_word_count() function counts the number of words in a string.
<?php echo str_word_count("Hello world!"); ?>
OUTPUT: 2
strrev() :
In PHP the strrev() function is used to reverse the string.
<?php echo strrev("Hello world!"); ?>
OUTPUT: !dlrow olleH
strpos() :
The strpos() function searches for specific text which is within a string.
<?php echo strpos("Hello world!", "world"); ?>
OUTPUT: 6
str_replace() :
The str_replace() function replaces some characters with some other characters in the string.
<?php echo str_replace("Hero", "Honda", "Hero Honda!"); ?>
OUTPUT:Honda Honda!
1 thought on “Constant And String functions in PHP”