1
\$\begingroup\$

I'm trying to create a wordpress plugin but checking for the existence of a value doesn't really feel right because there's a bunch of values that I have to check:

$flickr_key = '';
if(!empty($ecom['ecom_options_flickr_api'])){
 $flickr_key = $ecom['ecom_options_flickr_api'];
}
$flickr_secret = '';
if(!empty($ecom['ecom_options_flickr_secret'])){
 $flickr_secret = $ecom['ecom_options_flickr_secret'];
}
$freebase_key = '';
if(!empty($ecom['ecom_options_freebase_api'])){
 $freebase_key = $ecom['ecom_options_freebase_api'];
}
$ebay_key = '';
if(!empty($ecom['ecom_options_ebay_api'])){
 $ebay_key = $ecom['ecom_options_ebay_api'];
}

Is there a more elegant alternative for this?

asked Apr 9, 2013 at 1:36
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

As Hugo Dozois suggested, using isset with the ternary operator is a good way to do this.

If you can assume that the options have not been set to NULL or false and you can change the $ecom variable you can use array_merge or the union operator.

// Merge with your default values here.
$ecom += array('ecom_options_flickr_api' => '',
 'ecom_options_flickr_secret' => '',
 'ecom_options_freebase_api' => '',
 'ecom_options_ebay_api' => '');
// Set the variables.
$flickr_key = $ecom['ecom_options_flickr_api']; 
$flickr_secret = $ecom['ecom_options_flickr_secret'];
$freebase_key = $ecom['ecom_options_freebase_api'];
$ebay_key = $ecom['ecom_options_ebay_api'];

If they could be NULL or false you could set them using a short ternary as Hugo Dozois mentioned. The indexes will now be set thanks to the union so you won't get Notices for undefined indexes.

Other Suggestions

  • I think your structure would be better represented as a multidimensional array:

    $ecom['options'] = array('flickr_api' => '', 'flickr_secret' => 'etc.');

  • Often intermediary variables such as $flickr_secret etc. are a bad idea IMO. I prefer to use $ecom['options']['flickr_secret'] in my code. I find aliases normally just confuse the code.

answered Apr 9, 2013 at 4:09
\$\endgroup\$
1
\$\begingroup\$

Using ternary operators to removed un-necessary lines

Well you could use the function isset with the use of ternary operators in order to make the code more concise.

I would do the following :

$flickr_key = isset($ecom['ecom_options_flickr_api']) ? $ecom['ecom_options_flickr_api'] : '' ;
$flickr_secret = isset($ecom['ecom_options_flickr_secret']) ? $ecom['ecom_options_flickr_secret']; '';
$freebase_key = isset($ecom['ecom_options_freebase_api']) ? $ecom['ecom_options_freebase_api'] : '';
$ebay_key = isset($ecom['ecom_options_ebay_api']) ? $ecom['ecom_options_ebay_api'] : '';

Reducing the code :

Well that is still ugly so you could make a function of it to reduce repetition of the array keys and to have something easier to read.

//You pass the reference of the supposed key
//Since you passe the reference, there will be no notice.
function assign(& $value $default = null){ 
 return isset($value) ? $value : $default;
}
// then use like
$flicker_key = assign($ecom['ecom_options_flickr_api'], '');

For more information see the following Stack Overflow post.

answered Apr 9, 2013 at 2:13
\$\endgroup\$
5
  • \$\begingroup\$ the second method is nice but I'm still getting notices that the index doesn't exists when I do it. \$\endgroup\$ Commented Apr 9, 2013 at 2:43
  • \$\begingroup\$ @leyasu What version of php are you using? And what do you mean by motices? I've been able to run it just fine on my localhost! \$\endgroup\$ Commented Apr 9, 2013 at 2:47
  • \$\begingroup\$ I'm using PHP version 5.4.9. I'm getting this: Notice: Undefined index: ecom_options_flickr_api \$\endgroup\$ Commented Apr 9, 2013 at 3:27
  • \$\begingroup\$ Using ternary operator with array will copy the full array before executing the statement so with a large array it will be slow. With simply ternary operator this code wil give you E_NOTICE if the index doesn't exists no matter what PHP version is it. \$\endgroup\$ Commented Apr 9, 2013 at 5:21
  • \$\begingroup\$ @PeterKiss Alright I've removed the second part, since it was throwing a notice, and that's not clean. Thank you for the feedback \$\endgroup\$ Commented Apr 9, 2013 at 12:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.