- php_sapi_name: documentation ( source)
- in_array: documentation ( source)
- array_keys: documentation ( source)
- str_split: documentation ( source)
- printf: documentation ( source)
<?php
$alfabeth = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
$comets_by_group = [
'HALLEY' => 'AMARELO',
'ENCKE' => 'VERMELHO',
'WOLF' => 'PRETO',
'KUSHIDA' => 'AZUL'
];
$sum_position_by_words = function($word) use($alfabeth) {
$result = 0;
foreach(str_split($word) as $letter) {
if(in_array($letter, $alfabeth)) {
$value_by_letter = ++array_keys($alfabeth, $letter)[0];
$result *= $value_by_letter;
}
}
return $result;
};
$complement = (php_sapi_name() != 'cli') ? "<br>\n" : "\n";
foreach($comets_by_group as $comet => $group) {
if($sum_position_by_words($comet) % 45 == $sum_position_by_words($group) % 45){
printf("The group %s of comet %s" . $complement, $comet, $group);
}
}