etrader Posted August 12, 2011 Share Posted August 12, 2011 I want to introduce some variable into Wordpress functions, but it does not work when defining the string outside the function. Consider this example function: function bbp_get_topic_edit_link( $args = '' ) { $defaults = array ( 'id' => 0, 'link_before' => '', 'link_after' => '', 'edit_text' => __( 'Edit', 'bbpress' ) ); This does not work when I introduce a string as (when string to replace "edit" with "something") $str="SOMETHING"; function bbp_get_topic_edit_link( $args = '' ) { $defaults = array ( 'id' => 0, 'link_before' => '', 'link_after' => '', 'edit_text' => $str ); But it works when introducing the string within the function as function bbp_get_topic_edit_link( $args = '' ) { $str="SOMETHING"; $defaults = array ( 'id' => 0, 'link_before' => '', 'link_after' => '', 'edit_text' => $str ); How I can make my strings active within functions? I want to store all strings within a reference file and the include it in each page (or through header). Quote Link to comment https://forums.phpfreaks.com/topic/244589-use-a-string-within-a-function-called-from-outside/ Share on other sites More sharing options...
trq Posted August 12, 2011 Share Posted August 12, 2011 Functions accept arguments. This is how you pass data into a function. Quote Link to comment https://forums.phpfreaks.com/topic/244589-use-a-string-within-a-function-called-from-outside/#findComment-1256276 Share on other sites More sharing options...
etrader Posted August 12, 2011 Author Share Posted August 12, 2011 You mean using func_get_arg()? but how I can bring a value from outside the function into it ? Quote Link to comment https://forums.phpfreaks.com/topic/244589-use-a-string-within-a-function-called-from-outside/#findComment-1256291 Share on other sites More sharing options...
JasonLewis Posted August 12, 2011 Share Posted August 12, 2011 It's called scope. You can see that in the function declaration there is $args = '', that is a single argument. You can have more by separating them with commas. function my_cool_function($arg1, $arg2, $arg3) You can define arguments are optional by setting a default value, which is what yours is doing. You can add another argument to the function and pass in the string when you need too. Otherwise you can use the global keyword, although it's not exactly a solution but more of a bandaid to your problem. Quote Link to comment https://forums.phpfreaks.com/topic/244589-use-a-string-within-a-function-called-from-outside/#findComment-1256292 Share on other sites More sharing options...
trq Posted August 12, 2011 Share Posted August 12, 2011 function foo($something) { echo $something; } foo('this is something'); See functions. Quote Link to comment https://forums.phpfreaks.com/topic/244589-use-a-string-within-a-function-called-from-outside/#findComment-1256299 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.