欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php把字符串中的首字符转换为大写的函数ucfirst()

程序员文章站 2022-03-25 21:33:14
...

实例

把 "hello" 的首字符转换为大写:

<?php
echo ucfirst("hello world!");
?>

定义和用法

ucfirst() 函数把字符串中的首字符转换为大写。

相关函数:

  • lcfirst() - 把字符串中的首字符转换为小写

  • ucwords() - 把字符串中每个单词的首字符转换为大写

  • strtoupper() - 把字符串转换为大写

  • strtolower() - 把字符串转换为小写

语法

ucfirst(string)
参数 描述
string 必需。规定要转换的字符串。

技术细节

返回值: 返回已转换的字符串。
PHP 版本: 4+

该函数演示了如何通过ucfirst 函数将字符串首字符转换成大写

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> ucfirst.php </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
 <body>
<?php
//原来首字符小写
$foo = 'hello world!';
$foo2 = ucfirst($foo); //输出 Hello world!
echo $foo . "<br>";
echo $foo2 . "<br>";
//首字符大写,则不改变
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); //输出 HELLO WORLD!
echo $bar . "<br>";
//将所有字符变成小写后,再将首字符编程大写
$bar = ucfirst(strtolower($bar)); //输出 Hello world!
echo $bar . "<br>";
?>
 </body>
</html>
hello world!
Hello world!
HELLO WORLD!
Hello world!

以上就是php把字符串中的首字符转换为大写的函数ucfirst()的详细内容,更多请关注其它相关文章!