Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method method, a getOrDefault() getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';
formatting
Source Link
Tim Lytle
  • 251
  • 1
  • 4

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';

Array Merge

If you're just setting defaults, you could use array_merge():

$defaults = array(
 'getvar1' => 'default setting', //a default value
 'getvar2' => false //now you can just test
);
$params = array_merge($defaults, $_GET); //any missing elements get the default value
echo $params['getvar1']; //will output 'default setting' if $_GET['getvar1'] was not set
if($params['getvar2']){
 //do something if $_GET['getvar2'] was set to a non-false value
}

It should be noted that this only works with things like $_GET - that are already arrays. The ternary method, a getOrDefault() function, or some enigmatic logic operator code is better suited when setting defaults not already in an array.

Logic Operators

Here's an example of using logic operators (similar to using ternary operator) from the default Zend MVC index.php:

defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Using the question's example, it would look something like this:

isset($_GET['getvar1']) || $_GET['getvar1'] = 'default value';
added logic operator example
Source Link
Tim Lytle
  • 251
  • 1
  • 4
Loading
Source Link
Tim Lytle
  • 251
  • 1
  • 4
Loading
lang-php

AltStyle によって変換されたページ (->オリジナル) /