K
K
KronosHD2015-10-04 14:26:53
PHP
KronosHD, 2015-10-04 14:26:53

How to check whether there are variables at once in one if?

There is such a code. It works but...

if(!isset($_GET['id']))
  {
    exit('Отсутствует обязательный параметр "ID"');
  }

When I add another one, the script execution stops. Where is the mistake?
if(!isset($_GET['id'] or $_GET['id2']))
  {
    exit('Отсутствует обязательный параметр "ID"');
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bears, 2015-10-04
@KronosHD

This is the correct way to check two variables:

if (!isset($_GET['id']) || !isset($_GET['id2'])) {
    exit('Отсутствует обязательный параметр "ID"');
}

A
Andrey Pavlenko, 2015-10-04
@Akdmeh

Enable error reporting and be more careful.
You do this: take the value of $_GET['id']. Does it not exist? Checking id2. There is? So it returns one. We check with isset if a unit exists.
And for you you need:
if (!isset($_GET['id']) && $_GET['id2']) ...
(if both values ​​don't exist). If you want an error to pop up if at least one value does not exist, you need if (!isset($_GET['id']) || $_GET['id2']) ...
Also, !empty is better for your purpose , since for isset "0" is a completely valid value, and for empty it will return false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question