How to convert lowercase letters to uppercase in php

青灯夜游
Release: 2023-03-12 16:58:01
Original
5937 people have browsed it

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)
Copy after login

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;
?>
Copy after login

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)
Copy after login

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;
?>
Copy after login

The running results are as follows:

Hello world!
Hello world!
Copy after login

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)
Copy after login

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;;
?>
Copy after login

The running results are as follows:

Hello World!
Hello World!
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template