0
For example, when I need to check some certain values, I create a "method", similar to that:
public function verificaString($campo)
{
if (isset($_POST[$campo]) && (!empty($_POST[$campo]) && (is_string($_POST[$campo])))):
(string)$var = filter_var(trim($_POST[$campo]), FILTER_SANITIZE_STRING);
return $var;
else:
$var = 'Indisponível';
return $var;
endif;
}
Now thinking about this part more specifically:
if (isset($_POST[$campo]) && (!empty($_POST[$campo])
There is no way to simplify this process?
Every time I need to check if a variable is set and it has a value other than null, do I have to do just that? There’s no way to simplify this procedure?
Please review your last code, it seems that the function return
is_string
is being passed as parameter ofempty
together with the value of$_POST
.– Woss
isset is different from Empty. Empty checks for non-existence and has no value. isset checks for.
– Lucas de Carvalho
@Andersoncarloswoss corrected, thanks for the information
– Gabriel Heming
@Lucascarvalho read again what I wrote, Empty is the opposite of isset + the negation of the variable. See link: https://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty or the manual itself
– Gabriel Heming
The isset() checks if a variable is/was set and is not null. Already the Empty() check if she is empty.
– UzumakiArtanis
@lucascarvalho the
empty
is a language constructor and does exactly what was said in the reply, as is also mentioned in the documentation. Therefore it is possible to omit theisset
if used theempty
, yes.– Woss
@Uzumakiartanis had lacked a denial in the
empty
, but the written definition is still correct. See the comments above.– Gabriel Heming
Yes, that’s right :) I’m just saying it’s not the same thing. You can do the same things, but it’s not the same things.
– Lucas de Carvalho
@Lucascarvalho At no time has it been said that they are the same thing. Only that they are redundant in the code.
– Gabriel Heming
It’s just like you said @Gabrielheming. Empty with isset the way it’s used is redundancy. Just use Empty that’s enough.
– UzumakiArtanis