PHP 错误:函数名必须是字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1611709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Error: Function name must be a string
提问by RKh
I added the following lines to the top of my PHP code, but it throws an error:
我在 PHP 代码的顶部添加了以下几行,但它抛出了一个错误:
Fatal error: Function name must be a string in /home/reg.php on line 2
致命错误:函数名称必须是第 2 行 /home/reg.php 中的字符串
<?php
if ($_COOKIE('CaptchaResponseValue') == "false")
{
header('location:index.php');
return;
}
?>
I tried: $_COOKIE("CaptchaResponseValue"). The cookie is successfully set and is available. Why is it giving me an error when I am using $_COOKIE?
我试过:$_COOKIE("CaptchaResponseValue")。cookie设置成功,可用。为什么我在使用时报错$_COOKIE?
回答by Niyaz
It should be $_COOKIE['name'], not $_COOKIE('name')
应该是$_COOKIE['name'],不是$_COOKIE('name')
$_COOKIEis an array, not a function.
$_COOKIE是一个数组,而不是一个函数。
回答by Filip Ekberg
Using parenthesis in a programming language or a scripting language usually means that it is a function.
在编程语言或脚本语言中使用括号通常意味着它是一个函数。
However $_COOKIEin php is not a function, it is an Array. To access data in arrays you use square braces ('[' and ']') which symbolize which index to get the data from. So by doing $_COOKIE['test']you are basically saying: "Give me the data from the index 'test'.
但是$_COOKIE在 php 中不是一个函数,它是一个Array。要访问数组中的数据,您可以使用方括号('[' 和 ']'),它表示从哪个索引获取数据。因此,通过这样做,$_COOKIE['test']您基本上是在说:“从索引‘测试’中给我数据。
Now, in your case, you have two possibilities: (1) either you want to see if it is false--by looking inside the cookie or (2) see if it is not even there.
现在,在您的情况下,您有两种可能性:(1)要么您想查看它是否为假——通过查看 cookie 内部,要么(2)查看它是否甚至不存在。
For this, you use the issetfunction which basically checks if the variable is set or not.
为此,您使用isset函数,它基本上检查变量是否已设置。
Example
例子
if ( isset($_COOKIE['test'] ) )
And if you want to check if the value is false and it is set you can do the following:
如果您想检查该值是否为 false 并且已设置,您可以执行以下操作:
if ( isset($_COOKIE['test']) && $_COOKIE['test'] == "false" )
One thing that you can keep in mind is that if the first test fails, it wont even bother checking the next statement if it is AND( &&).
您可以记住的一件事是,如果第一个测试失败,它甚至不会检查下一个语句是否为AND( &&)。
And to explain why you actually get the error "Function must be a string", look at this page. It's about basic creation of functions in PHP, what you must remember is that a function in PHP can only contain certain types of characters, where $is not one of these. Since in PHP $represents a variable.
并解释为什么您实际上会收到错误“函数必须是字符串”,请查看此页面。这是关于 PHP 中函数的基本创建,您必须记住的是,PHP 中的函数只能包含某些类型的字符,其中$不是其中之一。因为在 PHP 中$代表一个变量。
A function could look like this: _myFunction _myFunction123 myFunctionand in many other patterns as well, but mixing it with characters like $ and % will not work.
一个函数可能看起来像这样:_myFunction _myFunction123 myFunction以及在许多其他模式中,但将它与诸如 $ 和 % 之类的字符混合将不起作用。
回答by Asaph
Try square braces with your $_COOKIE, not parenthesis. Like this:
尝试使用方括号$_COOKIE,而不是括号。像这样:
<?php
if ($_COOKIE['CaptchaResponseValue'] == "false")
{
header('Location: index.php');
return;
}
?>
I also corrected your location header call a little too.
我也更正了您的位置标题调用。
回答by Shankar R10N
If you wanna ascertain if cookie is set...use
如果您想确定是否设置了 cookie...使用
if (isset($_COOKIE['cookie']))
回答by Shahadat Atom
It will be $_COOKIE['CaptchaResponseValue'], not $_COOKIE('CaptchaResponseValue')
会$_COOKIE['CaptchaResponseValue'],不会$_COOKIE('CaptchaResponseValue')
回答by PJ Brunet
In PHP.js, $_COOKIE isa function ;-)
在 PHP.js 中,$_COOKIE是一个函数 ;-)
function $_COOKIE(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length).replace(/\+/g, '%20'));
}
return null;
}
回答by Waldhorn
A useful explanation to how braces are used (in addition to Filip Ekberg's useful answer, above) can be found in the short paper Parenthesis in Programming Languages.
关于如何使用大括号的有用解释(除了上面 Filip Ekberg 的有用答案)可以在短文Parenthesis in Programming Languages 中找到。
回答by prasanth
<?php
require_once '../config/config.php';
require_once '../classes/class.College.php';
$Response = array();
$Parms = $_POST;
$Parms['Id'] = Id;
$Parms['function'] = 'DeleteCollege';
switch ($Parms['function']) {
case 'InsertCollege': {
$Response = College::InsertCollege($Parms);
break;
}
case 'GetCollegeById': {
$Response = College::GetCollegeById($Parms['Id']);
break;
}
case 'GetAllCollege': {
$Response = College::GetAllCollege();
break;
}
case 'UpdateCollege': {
$Response = College::UpdateCollege($Parms);
break;
}
case 'DeleteCollege': {
College::DeleteCollege($Parms['Id']);
$Response = array('status' => 'R');
break;
}
}
echo json_encode($Response);
?>

