aero
aero

Reputation: 69

Why do I get a “Only variables should be assigned by reference” error with this PHP code?

I get this error:

Only variables should be assigned by reference

on this line:

$sum = &self::genchksum10($isbn);

of my function:

public function validateten($isbn) {
        $isbn = trim($isbn);
        $chksum = substr($isbn, -1, 1);
        $isbn = substr($isbn, 0, -1);
        if (preg_match('/X/i', $chksum)) { $chksum="10"; }
        $sum = &self::genchksum10($isbn);
        if ($chksum == $sum){
            return 1;
        }else{
            return 0;
        }
    }

I’m not really sure what’s wrong with it. Any help would be greatly appreciated.

Upvotes: 1

Views: 394

Answers (2)

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29975

You are trying to create a reference to the result of a function. That's not possible in PHP.

Just remove the &

Upvotes: 0

Naftali
Naftali

Reputation: 146302

An object cannot reference itself with a reference (&).

Try without it:

 $sum = self::genchksum10($isbn);

Upvotes: 4

Related Questions