PHP strtolower

Created with Sketch.

PHP strtolower

Summary: in this tutorial, you’ll learn how to use the PHP strtolower() function to make the lowercase version of a string.

Introduction to the PHP strtolower() function

The strtolower() function accepts a string and returns a new string with all alphabetic characters converted to lowercase.

Here’s the syntax of the strtolower() function:

strtolower ( string $string ) : string

Code language: PHP (php)

The strtolower() uses the current locale to determine alphabetic characters.

To return a new string with a specific encoding with all alphabetic characters converted to lowercase, you use the mb_strtolower() function instead:

mb_strtolower ( string $string , string|null $encoding = null ) : string

Code language: PHP (php)

The following defines a lower() function that returns a new string with all characters converted lowercase:

<?php

if (!function_exists('lower')) {
function lower(string $value) : string
{
return mb_strtolower($value, 'UTF-8');
}
}

Code language: PHP (php)

PHP strtolower() function examples

The following example uses the strtolower() function to convert the string PHP to lowercase:

<?php

echo strtolower('PHP');

Code language: PHP (php)

Output:

php

Code language: PHP (php)

Summary

  • Use the strtolower() function to return a new string from a string with all characters converted to lowercase.
  • Use the mb_strtolower() for a string with a specific encoding.

Leave a Reply

Your email address will not be published. Required fields are marked *