Open In App

PHP | String Functions

Last Updated : 09 Mar, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

We have learned about some basic string manipulation functions available in PHP in the article PHP | String . In this article, we will learn about few string functions that are used to change cases of characters of strings in PHP. Below are some most commonly used case manipulation functions for strings in PHP:

strtoupper() function in PHP

This function takes a string as argument and returns the string with all characters in Upper Case.

Syntax:

strtoupper($string)

Program to illustrate the use of strtoupper() function:




<?php
# PHP code to convert to Upper Case
function toUpper($string){
    return(strtoupper($string));
}
  
// Driver Code
$string="GeeksforGeeks";  
echo (toUpper($string));
?>  


Output:

GEEKSFORGEEKS

strtolower() function in PHP

This function takes a string as argument ans returns the string with all of the characters in Lower Case.

Syntax:

strtolower($string)

Program to illustrate the use of strtolower() function:




<?php
# PHP code to convert to Lower Case
function toLower($string){
    return(strtolower($string));
}
  
// Driver Code
$string="GeeksforGeeks";  
echo (toLower($string));
?>  


Output:

geeksforgeeks

ucfirst() function in PHP

This function takes a string as argument and returns the string with the first character in Upper Case and all other cases of the characters remains unchanged.

Syntax:

ucfirst($string)

Program to illustrate the use of ucfirst() function:




<?php
# PHP code to convert the first letter to Upper Case
function firstUpper($string){
    return(ucfirst($string));
}
  
// Driver Code
$string="welcome to GeeksforGeeks";  
echo (firstUpper($string));
?>


Output:

Welcome to GeeksforGeeks

lcfirst() function in PHP

This function takes a string as argument and returns the string with the first character in Lower Case and all other characters remains unchanged.

Syntax:

lcfirst($string)

Program to illustrate the use of lcfirst() function:




<?php
# PHP code to convert the first letter to Lower Case
function firstLower($string){
    return(lcfirst($string));
}
  
// Driver Code
$string="WELCOME to GeeksforGeeks";  
echo (firstLower($string));
?>


Output:

wELCOME to GeeksforGeeks

ucwords() function in PHP

This function takes a string as argument and returns the string with the first character of every word in Upper Case and all other characters remains unchanged.

Syntax:

ucwords($string)

Program to illustrate the use of ucwords() function:




<?php
# PHP code to convert the first letter 
# of each word to Upper Case
function firstUpper($string){
    return(ucwords($string));
}
  
// Driver Code
$string="welcome to GeeksforGeeks";  
echo (firstUpper($string));
?>


Output:

Welcome To GeeksforGeeks

strlen() function in PHP

This function takes a string as argument and returns and integer value representing the length of string. It calculates the length of the string including all the whitespaces and special characters.

Syntax:

strlen($string)

Program to illustrate the use of strlen() function:




<?php
# PHP code to get the length of any string
function Length($string){
    return(strlen($string));
}
  
// Driver Code
$string="welcome to GeeksforGeeks";  
echo (Length($string));
?>


Output:

24


Similar Reads

What are User-defined Functions and Built-in Functions in PHP?
In PHP, User-defined functions are created by programmers to meet specific requirements, while PHP built-in functions are provided by PHP to perform common tasks without the need for manual implementation. Both types of functions play crucial roles in PHP development, offering flexibility, modularity, and efficiency in coding. Table of Content User
2 min read
PHP String Functions Complete Reference
Strings are a collection of characters. For example, 'G' is the character and 'GeeksforGeeks' is the string. Installation: These functions are not required any installation. These are the part of PHP core. The complete list of PHP string functions are given below: Example: This program helps us to calculate a 32-bit CRC for the string “Hello World”
6 min read
Difference between split() and explode() functions for String manipulation in PHP
In this article, we will see the differences between split() and explode() functions for String manipulation in PHP. The split() and explode() functions are available in base PHP and are used to perform string manipulations as well as conversions. split() Function: The split() function in PHP is used to segregate the input string into different ele
3 min read
Explain some string functions of PHP
In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols. For example, "Hello World!", "ID-34#90" etc. PHP also allows single quotes(' ') for defining a string. Every programming language provides some in-built functions
7 min read
Difference between Regular functions and Arrow functions
This article discusses the major differences between regular functions and arrow functions. Arrow functions - a new feature introduced in ES6 - enable writing concise functions in JavaScript. While both regular and arrow functions work in a similar manner, there are certain interesting differences between them, as discussed below. Syntax: Regular f
2 min read
PHP Calendar Functions Complete Reference
The calendar extension contains a series of functions to represent the calendar date into a different format. The standard date is based on Julian Day Count. The date count starting from January 1st, 4713 B.C. is known as Julian Day Count. First, the date is converted into Julian Day Count and then converted into a calendar system. Example: The pro
2 min read
PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
11. is_nan() : This function takes any value as argument and checks whether a value is number or not. It returns TRUE (1) if the specified value is 'not a number', otherwise it returns FALSE/NOTHING. Syntax : is_nan(value); Example : C/C++ Code &amp;lt;?php echo is_nan(234) . &amp;quot;\n&amp;quot;; echo is_nan(asin(1.1)) ; ?&amp;gt; Output : 1 Not
4 min read
PHP Math Functions (abs, pi, deg2rad, rad2deg, fmod, floor, ceil, round, is_finite, is_infinite)
As you know PHP is a server side script language and is used for creating dynamic web pages. PHP provides a number of built-in math functions which help in performing several operations while dealing with mathematical data. It has nice support for mathematical processing. No installation is required to use these functions. Some examples of mathemat
4 min read
PHP strpos() and stripos() Functions
In this article, we will see how to find the position of the first occurrence of a string in another string using strpos() and stripos() Functions in PHP, &amp; will see their implementation through the examples. Both the strpos() and stripos() Functions in PHP are binary-safe which means the function will handle its input as a raw byte stream and
4 min read
PHP strrpos() and strripos() Functions
We already have learned about strpos() and stripos() which helps to find the first occurrence of a string in another string. In this article we will learn about two other similar functions strrpos() and strripos(). strrpos() in PHP Unlike strpos(), strrpos() function helps us to find the position of the last occurrence of a string in another string
3 min read
Practice Tags :