PHP Error: Function name must be a string

Php

Php Problem Overview


I added the following lines to the top of my PHP code, but it throws an error:

> Fatal error: Function name must be a string in /home/reg.php on line 2

<?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?

Php Solutions


Solution 1 - Php

It should be $_COOKIE['name'], not $_COOKIE('name')

$_COOKIE is an array, not a function.

Solution 2 - Php

Using parenthesis in a programming language or a scripting language usually means that it is a function.

However $_COOKIE in 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'.

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.

For this, you use the isset function which basically checks if the variable is set or not.

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:

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 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.

A function could look like this: _myFunction _myFunction123 myFunction and in many other patterns as well, but mixing it with characters like $ and % will not work.

Solution 3 - Php

Try square braces with your $_COOKIE, not parenthesis. Like this:

<?php
if ($_COOKIE['CaptchaResponseValue'] == "false")
{
    header('Location: index.php');
    return;
}
?>

I also corrected your location header call a little too.

Solution 4 - Php

It will be $_COOKIE['CaptchaResponseValue'], not $_COOKIE('CaptchaResponseValue')

Solution 5 - Php

If you wanna ascertain if cookie is set...use

if (isset($_COOKIE['cookie']))

Solution 6 - Php

In PHP.js, $_COOKIE is a function ;-)

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;
}

via http://phpjs.org/functions/setcookie:509

Solution 7 - Php

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.

Solution 8 - Php

<?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);
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRKhView Question on Stackoverflow
Solution 1 - PhpNiyazView Answer on Stackoverflow
Solution 2 - PhpFilip EkbergView Answer on Stackoverflow
Solution 3 - PhpAsaphView Answer on Stackoverflow
Solution 4 - PhpShahadat AtomView Answer on Stackoverflow
Solution 5 - PhpShankar R10NView Answer on Stackoverflow
Solution 6 - PhpPJ BrunetView Answer on Stackoverflow
Solution 7 - Phprare_arrayView Answer on Stackoverflow
Solution 8 - PhpprasanthView Answer on Stackoverflow