Home >Backend Development >PHP Problem >How to convert lowercase letters to uppercase in php

How to convert lowercase letters to uppercase in php

青灯夜游
青灯夜游Original
2021-09-17 17:08:395963browse

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.

How to convert lowercase letters to uppercase in php

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:

How to convert lowercase letters to uppercase in php

##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 = &#39;hello world!&#39;;
    $str = ucfirst($str);
    echo $str.&#39;<br>&#39;;
    $str2 = &#39;HELLO WORLD!&#39;;
    $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 = &#39;hello world!&#39;;
    $str = ucwords($str);
    echo $str.&#39;<br>&#39;;
    $str2 = &#39;HELLO WORLD!&#39;;
    $str2 = ucwords(strtolower($str2));
    echo $str2.&#39;<br>&#39;;
?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn