Oihana PHP

luhn.php

Table of Contents

Functions

luhn()  : bool
Validates whether a given string is a valid Luhn code (mod 10 checksum).

Functions

luhn()

Validates whether a given string is a valid Luhn code (mod 10 checksum).

luhn(string $number[, bool $lazy = false ]) : bool

The Luhn algorithm is commonly used to validate credit card numbers, IMEI numbers, and other identification codes.

Parameters
$number : string

The input string to validate.

$lazy : bool = false

If true, non-digit characters will be removed before validation. If false, the string must contain only digits.

Tags
example
echo luhn("79927398713");           // Outputs: true (valid Luhn)
echo luhn("7992 7398 713", true);   // Outputs: true (valid Luhn, spaces removed)
echo luhn("1234");                  // Outputs: false (invalid Luhn)
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

Returns true if the input passes the Luhn check, false otherwise.


        
On this page

Search results