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

黄舟
黄舟 原创
2023-03-17 07:18:01 1142浏览

实例

把 "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()的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。