PHP - using isset and GETs

1

Good people,

I have these 4 Gets to collect, the same leaves another page. What I intend is ...

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

    mostra valores do $_GET...

} else {

    erro.
}

How do I do this? Is it 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']) {
    
asked by anonymous 11.01.2018 / 20:42

3 answers

3

The isset() is not a function is a construct language, it has a differential that is pass N input arguments. This way 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 AND 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 the first one.

if(isset($_GET['order']) && isset($_GET['preco']) && isset($_GET['comprador'])){
    
11.01.2018 / 20:50
1

The correct one would be ...

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

You can change && to AND without problems.

    
11.01.2018 / 20:47
0

There is another way, too, is to use 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";
}

link

    
11.01.2018 / 20:52