PHP - using isset and Gets

Asked

Viewed 65 times

1

Good people,

I have these 4 Gets to collect, the same come out of another page. What I want is...

"Se apanhar($_GET) order, preço, comprador" {

    mostra valores do $_GET...

} else {

    erro.
}

How do I do that? Will it be so?

if(isset($_GET['order'] || isset($_GET['preco'] || isset($_GET['comprador']) {

or so

if(isset($_GET['order'] AND isset($_GET['preco'] AND isset($_GET['comprador']) {
  • Need all valid or one?

  • I need all valid

3 answers

3

The isset() is not a function is a language Construct, it has a differential which is to pass N input arguments. Thus validation is done on all arguments if one of them is not valid isset() returns false. It is the equivalent of connecting N condition with the logical E operator (&&)

You can leave your code:

if(isset($_GET['order'], $_GET['preco'], $_GET['comprador'])){
   echo 'válido';
}else{
   echo 'inválido';
}

The code below is equivalent to the first.

if(isset($_GET['order']) && isset($_GET['preco']) && isset($_GET['comprador'])){
  • Perfect! I think only for beginners it would be cool to say something like this "isset(..,..,..) equivalent to this isset(...) && isset(...) && isset(...)", but I’m leaving +1

1

The right thing would be...

if( isset($_GET['order']) && isset($_GET['preco']) && isset($_GET['comprador']) )
  echo 'os 3 existem';
else 
  echo 'os 3 não existem';

Can change the && for AND hassle-free.

  • Okay, I really appreciate the help. D Thank you...

0

There is another way too, is to use the array_diff that will check exactly the fields

<?php

$obrigatorios = ["order", "preco", "comprador"];

if (!array_diff($obrigatorios, array_keys($_GET))) {
    echo "mostra valores do GET...";
} else {
    echo "Erro";
}

Or you can use array_diff_assoc

<?php

$obrigatorios = ["order", "preco", "comprador"];

if( !array_diff_assoc($obrigatorios, array_keys($_GET)) ) {
    echo "mostra valores do GET...";
} else {
    echo "Erro";
}

https://ideone.com/JwpTh1

  • Which of the forms presented here is the safest way to pass data?...

  • They all have the same level of safety. What can be tested is the performance of each, but even so, the difference is probably negligible.

  • In the php doc it says array_diff returns an array, because the validation works?

  • 1

    @Francisco Se $_GET possess exactly the three keys, the function array_diff will return a array empty, which is a falsy value, that is, considered false when treated in a boolean operation. How was used the !, the value becomes true.

  • 1

    That being said, this is a limitation on this solution that should be explicit in the answer: if there are other keys, the code will indicate error, even if the three desired values exist.

  • @Silva is basically with @Andersoncarloswoss mentioned, but to get around this you can use the array_diff_assoc

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.