Php 为什么setCookie()在输出后工作?
我是PHP新手。 我有这样的代码Php 为什么setCookie()在输出后工作?,php,Php,我是PHP新手。 我有这样的代码 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, ma
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>Lean PHP</p>
<?php setCookie("name", "Bob"); ?>
<?= $_COOKIE['name']; ?>
</body>
</html>
文件
精益PHP
我写这段代码的目的是它会失败。我想检查在输出后调用setCookie()时收到的错误消息类型。
但它没有给我任何错误,而且运行良好。
当我在输出后调用setCookie()时,我的理解是错误的吗?您的代码没有问题。您创建了一个具有值Bob的Cookie,因此编写了value.Hi。我想我必须在任何输出之前设置Cookie。我错了吗?你是对的。首先在任何输出之前设置Cookie。