is this code improvable?
Currently my mind is under heavy pressure and I can't stay a long time focusing on one single issue, I am pretty sure that the way I made it is basic and can be improved to make it smaller and easier (more professional maybe ?) code;
<?php
$intro_id = rand(1,2);
if($intro_id == 1 && !empty($options['hidden_intro_one'])){
$hidden_intro = $options['hidden_intro_one'];
}
elseif(!empty($options['hidden_intro_two'])){
$hidden_intro = $options['hidden_intro_two'];
}
else{
//back to circle number 1
$hidden_intro = $options['hidden_intro_one'];
}
?>
Partially SOLVED : the solution was to use array_rand() function like this :
<?php
$random_intro = array($options['hidden_intro_one'],$options['hidden_intro_two']);
$hidden_intro = $random_intro[array_rand($random_intro)];
?>
But if one of the intros is left empty, it开发者_如何转开发 will appear empty when you echo the code, while I want to print the other intro (if not empty) instead...
It can certainly be 'improved'. Code can always be improved :) One easy improvement you can always do (more of a habit to teach yourself) is to add a comment about what the code is supposed to do.
Anyway, what (I think) you're trying to do is basically the same as:
$hidden_intro = $options[ array_rand( $options ) ];
<?php
$intros = array(
1 => $options['hidden_intro_one'],
2 => $options['hidden_intro_two']
);
$intro_id = rand(1,2);
$hidden_intro = empty($intros[$intro_id]) ? reset($intros) : $intros[$intro_id];
?>
Although I don't like using reset() as a way to get the first possible value, but you can customize the 'default' value process :)
Something like this maybe:
$intro = array("hidden_intro_one","hidden_intro_two");
$hidden_intro = $options[array_rand($intro)];
Or, if $options
only contains hidden_intro_one
and hidden_intro_two
you could just do:
$hidden_intro = array_rand($options);
EDIT oops, this leaves out the part where it can't be empty.
If $options
only contains hidden_intro_one
and hidden_intro_two
$hidden_intro = $options["hidden_intro_one"]; // fallback if both are empty
shuffle($options);
foreach($options as $option)
if(!empty($option)){
$hidden_intro = $option;
break;
}
Else
$hidden_intro = $options["hidden_intro_one"]; // fallback if both are empty
$intros = array("hidden_intro_one","hidden_intro_two");
shuffle($intros);
foreach($intros as $intro)
if(!empty($options[$intro])){
$hidden_intro = $options[$intro];
break;
}
This might not necessarily be better though, but it will be if you add more values.
Encapsulate what varies and for the standard problem use an existing function like array_rand
:
$intros = get_non_empty_intros_from_options($options);
$hidden_intro = array_rand($intros);
function get_non_empty_intros_from_options($options)
{
$intro_option_keys = array('one', 'two');
$intro_option_key_mask = 'hidden_intro_%s';
$intros = array();
foreach ($intro_option_keys as $intro)
{
$key = sprintf($intro_option_key_mask, $intro);
if (empty($options[$key]))
continue
;
$intros[$key] = $options[$key];
}
return $intros;
}
This method will allow you to use more than one intro.
<?php
//why not make your intro's an array in your options array
$options['hidden_intro'] = array('one', 'two');
//declare your intro variable
$hidden_intro;
//now lets loop our intro's
foreach($options['hidden_intro'] as $intro)
{
if(!empty($intro))
{
$hidden_intro = $into;
break;
}
}
if(isset($hidden_intro))
{
//use your intro here
}
?>
Just for fun, an (almost) oneliner:
$to_be_choosen_keys = array("hidden_intro_one","hidden_intro_two");
$hidden_intro = array_rand(
array_intersect_key(
$options,
array_flip($to_be_choosen_keys)
)
);
精彩评论