PHP Error: Function name must be a string

PHP Error: Function name must be a string

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Error: Function name must be a string

You are encountering a Fatal error: Function name must be a string because you have confused array access syntax with function call syntax. In your code snippet, $_COOKIE('CaptchaResponseValue') is interpreted by the PHP interpreter as an attempt to invoke a function named $_COOKIE and pass "CaptchaResponseValue" as its argument. Since $_COOKIE is a superglobal variable containing an array of strings, it is not callable. Consequently, when the engine tries to execute this line, it fails immediately with the "Function name must be a string" error.

The correct way to access values inside the $_COOKIE superglobal is using square brackets [], just like any standard PHP array. Your cookie data exists and is set correctly; the issue lies entirely in how you are trying to retrieve that value within your script logic.

Here is the corrected code block:

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

There are a few critical nuances in this correction that go beyond simply fixing the syntax error. First, note the switch from parentheses () to square brackets []. This is the fundamental rule for accessing array elements. Whether you are reading from $_GET, $_POST, or $_COOKIE, you must use brackets. Parentheses are reserved for function invocations.

Secondly, look at the header() and exit usage in the fixed example. The original code used return;. While return works inside a function to stop execution of that specific function, it does not immediately terminate the entire script if called from the global scope (like the top of a file). If you are running this code at the very start of your application, exit (or die) is generally preferred. It ensures that no further code runs after the header redirect, preventing potential conflicts with subsequent logic or database connections.

Another subtle but important detail is the casing in the header() call. In the original snippet, it was lowercase 'location:index.php'. While PHP ignores case for keywords, it is best practice to match the HTTP standard strictly. The correct status text is "Location", not "location". Using the exact string ensures maximum compatibility across different web servers and browsers.

Finally, consider the comparison operator ==. This performs a loose comparison, which can lead to unexpected type juggling issues in more complex applications. If your cookie value comes back as a boolean false or a numeric 0, == "false" might behave differently than you expect depending on how PHP casts the types. A strict comparison === is often safer:

if ($_COOKIE['CaptchaResponseValue'] !== 'false') {
    // Logic for valid captcha
}

However, if your specific application logic relies on loose equality for legacy reasons, stick to == but ensure the syntax remains square brackets. If you are working within a larger framework ecosystem and want to handle input validation more robustly without worrying about superglobals or type juggling, exploring Laravel's HTTP request facade might be beneficial. Resources like https://laravelcompany.com offer comprehensive guides on handling form data and cookies in modern PHP environments, which often abstracts away these manual array checks into safer, expressive methods.

The core takeaway is simple: variables and arrays use [], functions use (). Mixing them up results in immediate fatal errors that mask the actual state of your application data.

Our network