Sha256: 26dbc31323264750830d2633fcb5f273543131feead9af61e6976a06348ce4d7

Contents?: true

Size: 572 Bytes

Versions: 275

Compression:

Stored size: 572 Bytes

Contents

<?php

function isValid($candidate)
{
    $sanitizedCandidate = str_replace(" ", "", $candidate) ;

    if (strlen($sanitizedCandidate) <= 1 || !ctype_digit($sanitizedCandidate)) {
        return false ;
    }

    $reversseCandidate = strrev($sanitizedCandidate) ;
    $sum = 0 ;

    for ($i = 1; $i < strlen($reversseCandidate); $i+=2) {
        $digit = 2 * intval($reversseCandidate{$i}) ;

        if ($digit > 9) {
            $digit -= 9 ;
        }

        $sum += $digit ;
        $sum += intval($reversseCandidate{$i-1}) ;
    }

    return $sum % 10 == 0 ;
}

Version data entries

275 entries across 275 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/php/exercises/luhn/example.php
trackler-2.2.1.158 tracks/php/exercises/luhn/example.php
trackler-2.2.1.157 tracks/php/exercises/luhn/example.php
trackler-2.2.1.156 tracks/php/exercises/luhn/example.php
trackler-2.2.1.155 tracks/php/exercises/luhn/example.php
trackler-2.2.1.154 tracks/php/exercises/luhn/example.php
trackler-2.2.1.153 tracks/php/exercises/luhn/example.php
trackler-2.2.1.152 tracks/php/exercises/luhn/example.php
trackler-2.2.1.151 tracks/php/exercises/luhn/example.php
trackler-2.2.1.150 tracks/php/exercises/luhn/example.php
trackler-2.2.1.149 tracks/php/exercises/luhn/example.php
trackler-2.2.1.148 tracks/php/exercises/luhn/example.php
trackler-2.2.1.147 tracks/php/exercises/luhn/example.php
trackler-2.2.1.146 tracks/php/exercises/luhn/example.php
trackler-2.2.1.145 tracks/php/exercises/luhn/example.php
trackler-2.2.1.144 tracks/php/exercises/luhn/example.php
trackler-2.2.1.143 tracks/php/exercises/luhn/example.php
trackler-2.2.1.142 tracks/php/exercises/luhn/example.php
trackler-2.2.1.141 tracks/php/exercises/luhn/example.php
trackler-2.2.1.140 tracks/php/exercises/luhn/example.php