Home > Backend Development > PHP Tutorial > Why Does My PHP Code Throw a 'Cannot Pass Parameter 2 by Reference' Error and How Can I Fix It?

Why Does My PHP Code Throw a 'Cannot Pass Parameter 2 by Reference' Error and How Can I Fix It?

Mary-Kate Olsen
Release: 2024-12-07 06:49:12
Original
233 people have browsed it

Why Does My PHP Code Throw a

Resolving the "Cannot Pass Parameter 2 by Reference" Error in PHP

When working with PHP, you may encounter the following error:

Fatal error: Cannot pass parameter 2 by reference in /web/stud/openup/inactivatesession.php on line 13
Copy after login

This error indicates that your PHP code is attempting to pass the second parameter of a function or method by reference, but the parameter is not being correctly identified as a reference.

Understanding the Error

PHP's bind_param() method expects the second parameter to be a reference to a variable. However, in the code provided:

$update->bind_param("is", 0, $selectedDate); //LINE 13
Copy after login

The second parameter, 0, is being passed as an integer value, not a reference to a variable. This discrepancy causes the error.

Fixing the Error

To resolve this error, you need to pass a reference to a variable instead of the integer. This can be achieved by using the following code:

$a = 0;
$update->bind_param("is", $a, $selectedDate); //LINE 13
Copy after login

By assigning the integer value to a variable ($a) and then passing the reference of that variable, you ensure that the parameter is being passed by reference as required.

Additional Information

For a more thorough understanding of what is causing this error, refer to the PHP documentation on references: http://php.net/manual/en/language.references.pass.php

The above is the detailed content of Why Does My PHP Code Throw a 'Cannot Pass Parameter 2 by Reference' Error and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template