0

My PHP code looks like that.

if(isset($_GET['id'])) {
    $id = $_GET['id'];
}
if(isset($_GET['subid'])) {
    $subid = $_GET['subid'];
}

I want to get variable if isset and set to 0 if !isset. something like that

if(isset($_GET['id'])) {
    $id = $_GET['id'];
} else $id='0';

But as you see i have multiple if's. How can i do it? I need to write else for every if?

UPDATE Ok. thx for your help. I did it. But is there any way to shorten this piece of code again?

$id = isset($_GET['id']) ? $_GET['id'] : 0;
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$subid = isset($_GET['subid']) ? $_GET['subid'] : 0;
$feat = isset($_GET['feat']) ? $_GET['feat'] : 0;

10 Answers 10

4
$id = isset($_GET['id']) ? $_GET['id'] : 0;
Sign up to request clarification or add additional context in comments.

1 Comment

@Tural, I then suggest looking at the foreach solution that Arjan has already given.
3
$id = $subid=0;

if(isset($_GET['id']))
{
$id = $_GET['id'];
}
if(isset($_GET['subid']))
{
$subid = $_GET['subid'];
}

you can initiate all variables at 0 before you run that code

2 Comments

If you're going about it that way, might as well do: $id = $subid = 0
Can't think of anyway to make it significantly shorter
2

use a helper function:

function getQSValueOrDefault($name, $default){
   if (!isset($_GET[$name]))
      return $default;
   else
      return $_GET[$name];
}

and use it like:

$id = getQSValueOrDefault('id', 0); 
$subid = getQSValueOrDefault('subid', 0);

1 Comment

This could be simplified slightly using the ?: syntax
1

The shortest way to get an input variable is the following:

$id = filter_input(INPUT_GET, 'id');

(you don't have to check if the value is set)

Other method:

// set default values if they are not set
// so that you don't have to check if they are set
$inputs = $_GET + array(
    'id' => 0,
    'subid' => 0,
);

$id = $inputs['id'];
$subid = $inputs['subid'];

Or using ternary operator:

$id = isset($_GET['id']) ? $_GET['id'] : 0;
$subid = isset($_GET['subid']) ? $_GET['subid'] : 0;

1 Comment

The first and second methods I proposed are both shorted than the code you want to shorten.
1
$id = isset($_GET['id']) ? $_GET['id'] : "0";
$subid = isset($_GET['subid']) ? $_GET['subid'] : "0";

1 Comment

you have already marked this question as answered, which it is. If you really require it to be even shorter than it already is (which will just become a mess) then you should open a new question.
1
foreach ($_GET as $key=>$value){
  if ($key === 'id' || $key === 'subid'){
    if(isset($value)) {
      $$key = $value;
    }else{
      $$key = '0';
    }
  }
}

4 Comments

That's a great way to introduce unwanted variables and should be avoided as it is a potential security risk. You could make it more secure by checking the value of $key to a list of expected variables.
I don't think so, I would like you to have a second look; I finished it now ... and by the way: 'extract' is definte much worse ...
True, you were updating your post while I was typing my comment. This seems safe, although I would probably use in_array() to check $key.
It really depends on the number of fields. If there are more fields, I would also prefer 'in_array()'.
1

If you do not want several lines like:

$id = isset($_GET['id']) ? $_GET['id'] : 0;
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$subid = isset($_GET['subid']) ? $_GET['subid'] : 0;
$feat = isset($_GET['feat']) ? $_GET['feat'] : 0;

Then this could be an alternative.

$keys = array('id', 'page', 'subid', 'feat');
foreach($keys as $key)
{
    $$key = isset($_GET[$key]) ? $_GET[$key] : 0;
}

Note that you loose the option to set different default values for the keys, or to treat different keys differently.

Comments

0

You could just declare your $id and $subid as 0 by default. And then write a simple if ( ... ) { }. Like this:

$id = 0;
$subid = 0;

if (isset($_GET['id']))
    $id = $_GET['id'];
if (isset($_GET['subid']))
    $subid = $_GET['subid'];

Comments

0

how about using extract()??

extract($_GET); // it will create all the vairables
// to set default values
$id = isset($id) ? $id : 0;
$subid = isset($subid) ? $subid : 0;

EDITED: a more elegant way

$id = $subid = 0;  // set default values
extract($_GET); // it will create all the variables and overwrite default values

1 Comment

You'll just introduce unchecked user input in your code. That's potentially dangerous and should be avoided. extract() can be useful, but not on user input.
0

You could either use the ?: operators to remove the extra keyworks to make it a single line for each

$id= isset($_GET['id']) ? $_GET['id'] : 0;
$subid=isset($_GET['subid']) ? $_GET['subid'] : 0;

You could also create a function to do the work for you.

function getOrDefault($name, $default) {
  return isset($_GET[$name]) ? $_GET[$name] : $default);
}

This would allow you to create extra wrapper functions with defaults for various types.

function getInt($name) {
  return getOrDefault($name,0);
}

function getString($name) {
  return getOrDefault($name,"");
}

then your code becomes

$id = getInt('id');
$subid = get('subid');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.