php的输出显示乱码

php的输出显示乱码

1、index.php:

你好,<?php echo htmlspecialchars($_POST['name']);?>。
你 <?php echo (int)$_POST['age']; ?> 岁了。

2、index.html

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
	<title>测试</title>
</head>
<body>
	<form action="../api/index.php" method="post">
		 <p>姓名: <input type="text" name="name" /></p>
		 <p>年龄: <input type="text" name="age" /></p>
		 <p><input type="submit" /></p>
	</form>
</body>
</html>

3、显示效果如下:



4、解决方法:

a、直接写个配置的php文件,如config.php,内容如下

<?php
header("Content-type:text/html;charset=utf-8");
?>

然后在index.php文件前面引入config.php文件,修改后的index.php文件如下:

<?php
include_once("../config.php");
?>
你好,<?php echo htmlspecialchars($_POST['name']);?>。
你 <?php echo (int)$_POST['age']; ?> 岁了。

这样就显示正常了。

b、或者在index.php里面的<html>标签里面<head>标签里面加上下面这句:

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> 



相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...