Constant And String functions in PHP

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 phpInstallation 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

  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

define(name, value, case-insensitive)

  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 GeektoCode.com!");
echo WELCOME;
?> 

</body>
</html>
 OUTPUT: Hello GeektoCode.com!

Let’s create a constant with a case-insensitive name:

Example

<!DOCTYPE html>
<html>
<body>

<?php
// case-insensitive constant name
define("WELCOME", "Hello GeektoCode.com!", true);
echo welcome;
?> 

</body>
</html>
 OUTPUT: GeektoCode.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 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

ConstantVariable
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.

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!

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry