Constant And String functions in PHP

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

  1. Using define() function
  2. Using const keyword

How to Create a PHP Constant

Using define() function:

To create a constant in PHP, we use the define() function.

Syntax

  1. name: It is considered the constant name.
  2. value: It is considered as the constant value.
  3. 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("bikes", [
  "TVS",
  "Hero",
  "Honda"
]);
echo bikes[0];
?> 
</body>
</html>
OUTPUT:
 TVS

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.

Example

<?php  
const MESSAGE="Hello const by oraltadalafil PHP";  
echo MESSAGE;  
?>  
OUTPUT: Hello const by oraltadalafil 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", "oraltadalafil");  
    echo MSG, "</br>";  
    echo constant("MSG");  
    //both are similar  
?>
OUTPUT:
 oraltadalafil
 oraltadalafil

Difference between Constant and Variables

ConstantVariable
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 defining 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: 12

  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!

For more read with us, Oraltadalafil

read our previous post

1 thought on “Constant And String functions in PHP”

Leave a Comment