Home > Article > Backend Development > How to convert string to uppercase and lowercase in php?
In website development, there is a lot of data that needs to be regular and easy for administrators to manage. Therefore, when submitting requests for some stored data, case requirements are required. However, in order to facilitate user input, we do not deliberately require users to enter uppercase or lowercase letters. Instead, when storing data, program control is used to store the input content in uppercase or lowercase letters. In this chapter, we will show you how to convert the string we input to uppercase or lowercase.
First we learn two functions: strtoupper() function and strtolower() function
Method to convert all strings into uppercase:
strtoupper() function
strtoupper() function converts the string to uppercase. The syntax is as follows:strtoupper (str )The parameter str is converted StringExampleUse the strtoupper() function to convert the following variable $str to uppercase. The code is as follows
<?php $str = "Www.Php.cn"; $str = strtoupper($str); echo $str; ?>Code running results:
Convert all strings to lowercase:
strtolower() function
strtolower() function is to All strings are converted to lowercase, the syntax is as follows:strtolower (str )The parameter str is the converted stringExampleUse the strtolower() function to convert the variable $str to lowercase , the code is as follows:
<?php $str = "WWW.PHP.CN"; $str = strtolower($str); echo $str; ?>Code running results:
Application example
The following example fills in the user The book numbers in the book information are all stored in uppercase. The code is as follows:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>字符串大小写转换</title> </head> <body> <form method="post" action="" enctype="multipart/form-data"> <p> 图书名称:<input type="text" name="name"> </p> <p> 编  号:<input type="text" name="cod"> </p> <p> 价  格:<input type="text" name="pri"> </p> <p> 数  量:<input type="text" name="num"> </p> <p> 产  地:<input type="text" name="pro"> </p> <input type="submit" name="sub" value="提交" /> </form> </body> </html> <?PHP header("content-type:text/html;charset=utf-8"); $name=$_POST['name']; $cod=$_POST['cod']; $pri=$_POST['pri']; $num=$_POST['num']; $pro=$_POST['pro']; if(isset($_POST['sub'])){ $name=strtoupper($name); $cod=strtoupper($cod); echo "<p>图书名称:$name<p/>"; echo "<p>编号:$cod<p/>"; echo "<p>价格:$pri<p/>"; echo "<p>数量:$num<p/>"; echo "<p>产地:$pro<p/>"; } ?>The code running result:
Conduct the string Case conversion is not a necessary function and can be selected according to the different requirements of each website. For example, in the backend product addition page of an e-commerce website, in order to display product models with unified specifications, you can use the two case conversion functions explained in this chapter. So it is very necessary to master these two functions. For more questions about strings, please pay attention to php string stringspecial topic
The above is the detailed content of How to convert string to uppercase and lowercase in php?. For more information, please follow other related articles on the PHP Chinese website!