Dino Geek, try to help you

How to convert a string to upper and lower case in PHP?


In PHP, converting a string to either uppercase or lowercase is a common task that can be accomplished using built-in functions. This allows for case normalization in various applications, such as data validation, text formatting, or user input handling. Here’s a detailed explanation of how you can perform these conversions along with some examples.

  1. Converting a String to Uppercase
    To convert a string to uppercase in PHP, you can use the `strtoupper` function. This function takes a single string argument and returns a new string with all alphabetic characters in uppercase. Here’s the syntax:

```
string strtoupper ( string $string )
```

Example:

```
$originalString = “Hello, World!”;
$uppercaseString = strtoupper($originalString);
echo $uppercaseString; // Outputs: HELLO, WORLD!
?>
```

In this example, the string “Hello, World!” is converted to “HELLO, WORLD!”.

  1. Converting a String to Lowercase
    To convert a string to lowercase, PHP provides the `strtolower` function. This function similarly takes a single string argument and returns a new string with all alphabetic characters in lowercase. Here’s the syntax:

```
string strtolower ( string $string )
```

Example:

```
$originalString = “Hello, World!”;
$lowercaseString = strtolower($originalString);
echo $lowercaseString; // Outputs: hello, world!
?>
```

In this example, the string “Hello, World!” is converted to “hello, world!”.

  1. Working with Multibyte Strings
    If you are working with multibyte characters (such as UTF-8), you should use the multibyte string functions `mb_strtoupper` and `mb_strtolower`. These functions are part of the Multibyte String (mbstring) extension and are specifically designed to handle multibyte encodings properly.

```
string mb_strtoupper ( string $string [, string $encoding = mb_internal_encoding() ] )
string mb_strtolower ( string $string [, string $encoding = mb_internal_encoding() ] )
```

Example:

```
$originalString = “こんにちは, 世界!”;
$uppercaseString = mb_strtoupper($originalString, ‘UTF-8’);
$lowercaseString = mb_strtolower($originalString, ‘UTF-8’);

echo $uppercaseString; // Outputs: こんにちは, 世界!
echo $lowercaseString; // Outputs: こんにちは, 世界!
?>
```

Notice that the output remains the same because Japanese characters do not have uppercase or lowercase variations.

  1. Reliable Sources
    1. PHP Official Documentation:
    - [strtoupper](https://www.php.net/manual/en/function.strtoupper.php)
    - [strtolower](https://www.php.net/manual/en/function.strtolower.php)
    - [mb\_strtoupper](https://www.php.net/manual/en/function.mb-strtoupper.php)
    - [mb\_strtolower](https://www.php.net/manual/en/function.mb-strtolower.php)

1. W3Schools PHP String Reference:
- [PHP strtoupper() Function](https://www.w3schools.com/php/func_string_strtoupper.asp)
- [PHP strtolower() Function](https://www.w3schools.com/php/func_string_strtolower.asp)

By utilizing these built-in functions, developers can easily manage and transform string cases in their PHP applications. Whether working with simple ASCII text or more complex multibyte encodings, PHP provides the necessary tools for effective string manipulation.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use