I want to know how to correctly handle the error handling for a non-mandatory variable, since if it is not informed, a Fatal Error - Undefined Index
is generated.
Context :
Through my system I register a publication (documents for user view), and create a system of folders for the user to access the data later.
Problem :
When a publisher is going to register the publication, it can inform all the fields or some (in this case my question is specifically about the fields fk_empregado
and mes
), that when there is the data register and it is not fk_empregado
or the mes
field it generates the following Notice
:
Notice: Undefined index: fk_empregado in ... on line 14 //Linha que recebe através de POST os dados do campo empregado
Notice: Undefined index: mes in ... on line 19 //Linha que recebe através de POST os dados do campo mes
So, my question is, Exactly how can I tell the script that there is no problem these two fields are undefined, and that it can continue normally (I do not know the answer, but I'm sure not is with @
)?
Page Code InsertPublication.php:
$fk_titulo = $_POST['publicacao'];
$fk_tipo = $_POST['tipo_publicacao'];
$fk_empregado = $_POST['empregado'];
$fk_empresa = $_POST['empresa_destino'];
$data_vencimento = $_POST['data_vencimento'];
$data_pagamento = $_POST['data_pagamento'];
$arqName = $_FILES['arquivo']['name'];
$mes = $_POST['mes'];
$ano = $_POST['ano'];
$valor = $_POST['valor'];
$publicante = $_SESSION['nome'];
$observacao = $_POST['observacao'];
$status = "N";
$dir = 'upload/publicacoes/' . implode('/', array_filter([$fk_empresa, $fk_tipo, $fk_titulo, $fk_empregado, $ano, $mes]))."/";