Olá pessoal!
Estou tentando fazer uma validação de CPF usando o método Post, porém esta dando esse erro!
Warning: Uninitialized string offset 0 in C:\xampp\htdocs\curso_php\index.php on line 14
Fatal error: Uncaught TypeError: Unsupported operand types: string * int in C:\xampp\htdocs\curso_php\index.php:14 Stack trace: #0 C:\xampp\htdocs\curso_php\index.php(24): validaPrimeiroDigito('') #1 {main} thrown in C:\xampp\htdocs\curso_php\index.php on line 14
Como resolver?
segue abaixo meu codigo:
<?php
// $erro = "";
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$cpf = limpaPost($_POST['cpf']);
function validaPrimeiroDigito($cpf){
$cpf = preg_replace( '/[^0-9]/is', '', $cpf);
$sum = 0;
for($index = 0; $index < 9; $index++){
$sum += $cpf[$index] * (10 - $index);
}
$resto = ($sum * 10) % 11;
if($resto < 10){
return $cpf[9] == $resto;
}
return $cpf[9] == 0;
}
validaPrimeiroDigito($cpf);
// function validaCpf($cpf, $erro){
// if(empty($cpf)){
// $erro = "[Erro] Campo não pode ser vazio!";
// }
// if(strlen($cpf) != 11){
// return false;
// }
// return true;
// }
}
if(isset($_POST['reset']) == 'Reset'){
$erro = '';
}
function limpaPost($valor){
$valor = trim($valor);
$valor = stripslashes($valor);
$valor = htmlspecialchars($valor);
return $valor;
}
?>