如何在PHP中将字符串转换为大写?

PHP- strtoupper()

该方法在任何情况下都将字符串作为参数,并返回大写字符串。

<?php
	$str = 'hello friends';
	echo strtoupper($str) 
?>

输出结果

HELLO FRIENDS

其他功能:

PHP-ucfirst()

此函数将字符串的首字母转换为大写。

<?php
echo ucfirst("hello friend");
//输出:你好朋友
?>

PHP-ucwords()

此函数将所有单词的每个首字符转换为大写。

<?php
echo ucwords("how are you");
//输出:你好吗?
?>

完整的代码(HTML和PHP)

<html>
	<head>
		<title> PHP code to get textbox value and print it in Uppercase </title>
	</head>
	<body>
		<form action="">
			<input type="text" id="str" type="text" name="str" maxlength="10" size="26">

			<input type="submit" name="submit" formmethod="POST">
		</form>

		<?php
			if(isset($_POST['submit']))
			{
				$str = strtoupper($_POST['str']);
				echo "convert to upper case";
				echo $str;	
			}
		 ?>
	</body>
</html>

输出结果

PHP示例转换为大写