开发者

Multiple If - else in php

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 = 开发者_C百科isset($_GET['page']) ? $_GET['page'] : 0;
$subid = isset($_GET['subid']) ? $_GET['subid'] : 0;
$feat = isset($_GET['feat']) ? $_GET['feat'] : 0;


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


$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


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);


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;


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


foreach ($_GET as $key=>$value){
  if ($key === 'id' || $key === 'subid'){
    if(isset($value)) {
      $$key = $value;
    }else{
      $$key = '0';
    }
  }
}


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.


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'];


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


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜