In today’s topic, we will discuss Constant and String functions in PHP. Previously we describe the various topic of PHP like what is php?, datatypes in php, Installation Of XAMPP In Any Windows, Variables in PHP, etc. If you haven’t seen it, hurry up!
Constants in PHP
In PHP, once Constants are defined they cannot be changed or undefined. Constant in PHP is like a variable.
In PHP, there are two ways to create constant
- 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(name, value, case-insensitive)
- 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.
Let’s create a constant with a case-sensitive name:
<!DOCTYPE html> <html> <body> <?php // case-sensitive constant name define("WELCOME", "Hello GeektoCode.com!"); echo WELCOME; ?> </body> </html>
OUTPUT: Hello GeektoCode.com!
Let’s create a constant with a case-insensitive name:
<!DOCTYPE html> <html> <body> <?php // case-insensitive constant name define("WELCOME", "Hello GeektoCode.com!", true); echo welcome; ?> </body> </html>
PHP Constant Arrays
In PHP7, by using the define() function we can create an Array constant.
<!DOCTYPE html> <html> <body> <?php define("bikes", [ "TVS", "Hero", "Honda" ]); echo bikes; ?> </body> </html>
Using const keyword:
The keyword const is used to create a constant in PHP. The const keyword defines constants at the time of compilation. The constants which are defined using the const keywords are case-sensitive.
<?php const MESSAGE="Hello const by GeektoCode PHP"; echo MESSAGE; ?>
OUTPUT: Hello const by GeektoCode PHP
Constant() function in PHP
We can also print the value of constants using the constant() function instead of using the echo statement.
Syntax : constant (name)
<?php define("MSG", "GeektoCode"); echo MSG, "</br>"; echo constant("MSG"); //both are similar ?>
OUTPUT: GeektoCode GeektoCode
Difference between Constant and Variables
|A constant can be defined by using the define() function. It cannot be described by any simple task.||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.|
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>
· 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!"); ?>
· 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"); ?>
· str_replace() :
The str_replace() function replaces some characters with some other characters in the string.
<?php echo str_replace("Hero", "Honda", "Hero Honda!"); ?>