Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is String Function in PHP?
3.
PHP String Functions
4.
Some Examples of String functions in PHP
4.1.
1.PHP strlen() function
4.2.
2.PHP strrev() function
4.3.
3.PHP strpos() function
4.4.
4.PHP ucwords() function
4.5.
5.PHP strtoupper() function
4.6.
6.PHP strtolower() function
4.7.
7.PHP Implode() function
4.8.
8. PHP ucfirst() function
5.
Frequently Asked Questions
5.1.
What are PHP string functions?
5.2.
How many types of string functions are there in PHP?
5.3.
What is the string function?
5.4.
What is the PHP function to display string? 
6.
Conclusion
Last Updated: Sep 4, 2024
Easy

String Functions in PHP

Author Rajat Agrawal
0 upvote

Introduction

In the world of programming, a string is referred to as a data type and is often a collection of characters that may also include whitespace, numeric characters, characters, and special symbols. For example, "Hello World!" or "CodingNinjas", etc. Each programming language comes with certain built-in functions for handling strings. Several string functions in PHP helps to do several operations on strings.

PHP

What is String Function in PHP?

In PHP, a string is a data type used to store a sequence of characters. It can consist of letters, numbers, symbols, and even special characters. Strings are essential for handling textual data in PHP, and they can be enclosed in single quotes (''), double quotes (""), or heredoc syntax (<<<). They allow developers to manipulate and process text data efficiently. Here's a simple example:

// Declaring and initializing a string variable
$name = "John Doe";
// Concatenating strings
$greeting = "Hello, " . $name . "!";
// Outputting the result
echo $greeting; // Output: Hello, John Doe!

In this example, we declare a string variable $name and initialize it with the value "John Doe." Then, we use string concatenation to create another string $greeting, which combines the text "Hello, " with the value of $name. Finally, we use the echo statement to display the result: "Hello, John Doe!".

Strings in PHP are versatile and support various operations like concatenation, searching, and manipulation, making them a fundamental part of web development and data processing in PHP.

PHP String Functions

The string functions in PHP are present in the PHP core. You do not need to install these functions.

String Functions

Function

Description

addslashes()It returns a string with backslashes before the predefined characters.
addcslashes()It returns a string with backslashes before the specified characters.
chop()It removes characters or whitespace from a string's right end.
bin2hex()It converts an ASCII string to hexadecimal values.
hex2bin()It converts a string of hexadecimal values to ASCII characters.
chunk_split()It splits a string into smaller chunks.
chr()It returns a character based on an ASCII value.
convert_uudecode()It Decodes a uuencoded string.
convert_uuencode()It encodes a string using the uuencode algorithm.
convert_cyr_string()It converts a string between two different Cyrillic letter sets.
count_chars()It returns information about the characters in a string.
crypt()It will do one-way string hashing.
crc32()It calculates a 32-bit CRC for a string.
echo()It will output one or more strings.
explode()It is used to convert a string into an array.
fprintf()It's used to send a formatted string to a given output stream.
hebrev()It converts Hebrew text to visual text.
hebrevc()It converts Hebrew text to visual text and new lines (\n) into <br>.
html_entity_decode()It converts HTML entities to characters.
htmlentities()It converts characters to HTML entities.
htmlspecialchars()It converts some predefined characters to HTML entities.
htmlspecialchars_decode()It converts some predefined HTML entities to characters.
localeconv()It returns locale numeric and monetary formatting information.
ltrim()It eliminates any whitespace or extra characters from a string's left side.
rtrim()It eliminates any whitespce or extra characters from a string's right side.
implode()It returns a string from the elements of an array.
join()It acts as an alias of implode().
lcfirst()It transforms the first character of a string to lowercase.
levenshtein()It returns the Levenshtein distance between two strings.
md5()It calculates the MD5 hash of a string.
md5_file()It calculates the MD5 hash of a file.
print()It outputs one or more strings.
printf()It outputs a formatted string.
sprintf()It writes a formatted string to a variable.
sscanf()It parses input from a string according to a format.
strchr()It identifies the first instance of a string contained within another string.
strcmp()It compares two strings (case-sensitive).
strcoll()It compares two strings (locale-based string comparison).
strlen()It returns the length of a string.
strrchr()It identifies the last instance of a string contained within another string.
strrev()It reverses a string.
strripos()

It locates the position of the last occurrence of a string inside another string

(case-insensitive).

strrpos()It locates the position of the last occurrence of a string inside another string (case-sensitive).
strtolower()It converts a string to lowercase letters.
strtoupper()It converts a string to uppercase letters.
ucfirst()It transforms a string's first character from lowercase to uppercase.
ucwords()It converts the first character of each word in a string to uppercase.
wordwrap()It combines a string to a given number of characters.
str_pad()It pads a string to a new length.
str_repeat()It repeats a string for a given number of times.

Some Examples of String functions in PHP

1.PHP strlen() function

This method returns the length of the string or the total number of characters in the string, including any whitespace.

<?php

$str = "Coding Ninjas!";

// using strlen method
echo "Length of string is: ". strlen($str);

?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

Length of string is: 14

2.PHP strrev() function

We use this method to reverse a string.

<?php

$str = "Welcome to Coding Ninjas!";

 
// using strrev method
echo "Reverse: ". strrev($str);

?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

Reverse: !sajniN gnidoC ot emocleW

3.PHP strpos() function

This function is used to find any text or word within a given string.

<?php

$str = "Coding Ninjas!";

// using strpos method
echo "Position of 'Ninjas' in string: ". strpos($str, 'Ninjas');

?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

Position of 'Ninjas' in string: 7

4.PHP ucwords() function

This function is used for string formatting. The first letter or character of each word in the string is converted to uppercase using this function.

<?php

$str = "coding Ninjas!";

echo ucwords($str);

?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

Coding Ninjas!

5.PHP strtoupper() function

This method is used to convert a lowercase string to uppercase.

<?php

$str = "welcome to coding ninjas!";

echo strtoupper($str);

?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

WELCOME TO CODING NINJAS!

6.PHP strtolower() function

This method is used to convert an uppercase string to lowercase.

<?php

$str = "Welcome To Coding NiNjas!";

echo strtolower($str);

?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

welcome to coding ninjas!

7.PHP Implode() function

This method is used to create a string from the array of elements provided and join them together using the separator.

<?php

$arr = array("Coding","Ninjas!");

// <br> is used to jump to next line
echo implode(" ", $arr) . "<br>";
echo implode("-", $arr) . "<br>";

?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Coding Ninjas!
Coding-Ninjas!

8. PHP ucfirst() function

The ucfirst() function in PHP is used to convert the first character of a string to uppercase. It stands for "uppercase first" and is helpful when you want to capitalize the first letter of a word or sentence.

$name = "john doe";
$capitalizedName = ucfirst($name);
echo $capitalizedName;

Output :

John doe

Frequently Asked Questions

What are PHP string functions?

PHP string functions are built-in functions that enable manipulation and handling of textual data. They include functions for string concatenation, case conversion, length, searching, replacing, and more.

How many types of string functions are there in PHP?

PHP provides a wide range of string functions. They can be categorized into several types, including functions for string manipulation (concatenation, trimming), case conversion (strtolower, strtoupper), searching (strpos, strrpos), and replacing (str_replace, substr_replace). These functions offer flexibility and efficiency in working with textual data.

What is the string function?

A string function in PHP is a built-in function that enables developers to perform various operations on strings, such as concatenation, searching, manipulation, and case conversion. They provide essential tools for handling textual data efficiently and effectively in PHP programming.

What is the PHP function to display string? 

In PHP, the `echo` function is commonly used to display strings and other data to the output. It allows developers to print text, variables, or even HTML content directly to the web page, making it a fundamental tool for displaying information and user feedback.

Conclusion

In this article, we have extensively discussed String functions in PHP, the different string functions that are present in PHP, and the code implementation of some of the most commonly used functions. We hope you enjoyed learning about string functions in PHP.
 

Recommended problems -

Happy Coding!

Live masterclass