lindm Posted November 24, 2009 Share Posted November 24, 2009 got the following function I need help to complete: function sign() { $num = func_num_args(); for( $i = 0; $i < $num; ++$i ) { if value of variable != '' merge to $total=xxxxx } return $total; } print sign('','hello ','how ','are you?); The output should be: hello how are you? In other words the variables with values should be joined together. Can't get the xxx of the functions working. Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/ Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 why don't you just use implode? echo implode(" ", array("hello", "how", "are", "you")); Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965028 Share on other sites More sharing options...
lindm Posted November 24, 2009 Author Share Posted November 24, 2009 Well the variables are mysql queries like: sign($row['one'],$row['two'],$row['three']); So all results with a value should be joined to a new variable. Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965042 Share on other sites More sharing options...
thebadbad Posted November 24, 2009 Share Posted November 24, 2009 As mikesta707 suggests, you can simply implode() the array returned by func_get_args() inside the function. No need for func_num_args() and the for loop. <?php function sign() { return implode('', func_get_args()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965049 Share on other sites More sharing options...
lindm Posted November 24, 2009 Author Share Posted November 24, 2009 To complicate things I need to add a html tag around each variable like: <span>row['one']</span> Not sure your suggestion works? Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965053 Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 function sign() { return "<span>".implode('</span><span>', func_get_args())."</span>"; } should work. untested though Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965066 Share on other sites More sharing options...
lindm Posted November 24, 2009 Author Share Posted November 24, 2009 Works great! But...I was hoping to avoid the span tags for variables without any value. So sign('','','test') would not return <span></span><span></span><span>test</span> but instead <span>test</span> Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965074 Share on other sites More sharing options...
thebadbad Posted November 24, 2009 Share Posted November 24, 2009 To complicate things I need to add a html tag around each variable like: <span>row['one']</span> Not sure your suggestion works? Why didn't you specify that in your original post? Would get your problem solved faster. But mikesta707's code should work. If you don't want to wrap blank vars in span tags, you can loop through each argument: <?php function sign() { $return = ''; foreach (func_get_args() as $arg) { if ($arg != '') { $return .= "<span>$arg</span>"; } } return $return; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965077 Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 ahh man, now it can't be a 1 line function function sign() { $string = ""; foreach(func_get_args() as $args){ $string .= (str_len($args) > 0) ? "<span>".$args."</span>" : ""; } return $string; } dam badbad, always beating me. Oh well, our solutions are exactly the same, but mines a little short Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965080 Share on other sites More sharing options...
lindm Posted November 24, 2009 Author Share Posted November 24, 2009 Wonderful! Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965089 Share on other sites More sharing options...
thebadbad Posted November 24, 2009 Share Posted November 24, 2009 dam badbad, always beating me. Oh well, our solutions are exactly the same, but mines a little short Mine would be even shorter (great joke potential right here) by using the ternary operator as well. And of course it can be a one line function: function sign() {$return = ''; foreach (func_get_args() as $arg) {$return .= ($arg != '') ? "<span>$arg</span>" : '';} return $return;} LOL Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965095 Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 lol. I suppose an obligatory "thats what she said" is in order Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965102 Share on other sites More sharing options...
salathe Posted November 24, 2009 Share Posted November 24, 2009 ahh man, now it can't be a 1 line function Sure it can function sign() { return vsprintf(str_repeat("<span>%s</span>", count($args = array_filter(func_get_args(), "strlen"))), $args); } Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965114 Share on other sites More sharing options...
thebadbad Posted November 24, 2009 Share Posted November 24, 2009 Clever, although it complaints that func_get_args() can't be used as a function parameter Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965118 Share on other sites More sharing options...
salathe Posted November 24, 2009 Share Posted November 24, 2009 Clever, although it complaints that func_get_args() can't be used as a function parameter Use a newer version of PHP (5.3.0 or above) or move it out of the parameter: function sign() { return ($a = func_get_args()) ? vsprintf(str_repeat("<span>%s</span>", count($args = array_filter($a, "strlen"))), $args) : ""; } Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965121 Share on other sites More sharing options...
thebadbad Posted November 24, 2009 Share Posted November 24, 2009 Ah, thought it might have something to do with the version. Quote Link to comment https://forums.phpfreaks.com/topic/182838-join-texts-of-variables/#findComment-965125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.