PHP抛出“使用未定义常量"尽管字符串中有引号 [英] PHP throwing "Use of Undefined Constant" Despite Quotes in String

查看:49
本文介绍了PHP抛出“使用未定义常量"尽管字符串中有引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件,其中我正在尝试设置cookie;这是代码:

I have a PHP file wherein I'm trying to set a cookie; here is the code:

<?php
ini_set('display_errors', 1);
error_reporting(~0);
$coname = ‘logged’;
$coval = ‘false’;
setcookie($coname,$coval);
?>

Logged是cookie的名称,false是值.马上就扔了:

Logged is the name of the cookie, false is the value. Right off the bat it's throwing:

注意:使用未定义的常量"logged"(在第4行的(路径)中假定为"logged")
注意:使用未定义的常量'false'-在第5行的(path)中假定为'false'

Notice: Use of undefined constant ‘logged’ - assumed '‘logged’' in (path) on line 4
Notice: Use of undefined constant ‘false’ - assumed '‘false’' in (path) on line 5

然后似乎正在将这些字符串读取为常量.我能找到的所有资源都建议通过将字符串括在引号中来解决此问题,我尝试使用单引号和双引号都无济于事.如果有人知道错误仍然存​​在的原因,那将是巨大的帮助.谢谢!

It appears to be reading these strings as constants, then. Every resource I can find recommends solving this by enclosing the string in quotes, which I've tried with both single and double quotes to no avail. If anyone knows why the error persists, it would be a huge help. Thanks!

推荐答案

您使用的是前后引号,而不是单引号,因此PHP尝试解释'logged''false'作为未定义的常量.

You're using back- and forward-ticks instead of single quotation marks, so PHP is trying to interpret ‘logged’ and ‘false’ as constants, which aren't defined.

尝试以下方法:

<?php
ini_set('display_errors', 1);
error_reporting(~0);
$coname = 'logged';
$coval = 'false';
setcookie($coname,$coval);
?>

这篇关于PHP抛出“使用未定义常量"尽管字符串中有引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆