Home > Article > Backend Development > How to convert lowercase letters to uppercase in php
Conversion method: 1. Use the "strtoupper($str)" statement to convert all lowercase letters to uppercase; 2. Use the "ucfirst($str)" statement to convert the first letter to uppercase; 3. Use " ucwords($str)" statement converts the first letter of each word to uppercase.
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer
There are three ways to convert lowercase letters to uppercase in php :
strtoupper() Converts the string to uppercase
ucfirst() Converts the first letter of the string to uppercase
ucwords() Converts the first character of each word in the string to uppercase
1) strtoupper
## The #strtoupper() function can convert the letters in the string to uppercase. The syntax format is as follows:strtoupper($str)Among them, $str is a parameter of string type. This function can convert the letters in the parameter $string. to uppercase, and return the converted string. The sample code is as follows:
<?php $str = "https://www.php.cn/"; $str = strtoupper($str); echo $str; ?>The running result is as follows: ##2) ucfirst
## The #ucfirst function converts the first letter of a string to uppercase. The syntax format is as follows:
ucfirst($str)Among them, $str is the string that needs to be converted. The sample code is as follows:
<?php $str = 'hello world!'; $str = ucfirst($str); echo $str.'<br>'; $str2 = 'HELLO WORLD!'; $str2 = ucfirst(strtolower($str2)); echo $str2; ?>The running results are as follows:
Hello world! Hello world!
3) ucwords
ucwords() function can convert characters The first letter of each word in the string is converted to uppercase, and the grammatical format is as follows:
ucwords($str)The sample code is as follows:
<?php $str = 'hello world!'; $str = ucwords($str); echo $str.'<br>'; $str2 = 'HELLO WORLD!'; $str2 = ucwords(strtolower($str2)); echo $str2.'<br>'; ?>The running results are as follows:
Hello world! Hello world!Recommended learning:
phptraining
The above is the detailed content of How to convert lowercase letters to uppercase in php. For more information, please follow other related articles on the PHP Chinese website!