eldan88 Posted August 31, 2013 Share Posted August 31, 2013 Hey Guys. I have a function below. When I call it I want to pass in an <response> opening tag and a </response> closing tag as one argument, instead of having 2 arguments for the closing tag and opening tag. Is there a way where I can do this? Below is the function // The function below is the for the say voice only //@param $say_text takes a string as an argument for the say text function say_verb($response = "",$say_text) { $output = "{$response}<Say voice=\"woman\">{$say_text}{$response}</Say>"; echo $output; return $output; } Pretty much when i call say_verb() I want to some how pass <response> and </response> as one argument Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 31, 2013 Share Posted August 31, 2013 Something like: function say_verb($response = "", $say_text) { $output = !empty($response) ? "<{$response}>" : ""; $output .= "<Say voice=\"woman\">{$say_text}</Say>"; $output .= !empty($response) ? "</{$response}>" : ""; return $output; } Quote Link to comment Share on other sites More sharing options...
eldan88 Posted August 31, 2013 Author Share Posted August 31, 2013 Something like: function say_verb($response = "", $say_text) { $output = !empty($response) ? "<{$response}>" : ""; $output .= "<Say voice=\"woman\">{$say_text}</Say>"; $output .= !empty($response) ? "</{$response}>" : ""; return $output; } Okay gotchya! So do i just pass in the variable $response when i call the function? Like the following.. say_verb($response,"Hello World"); Quote Link to comment Share on other sites More sharing options...
jcbones Posted August 31, 2013 Share Posted August 31, 2013 Yes, but it doesn't have to be named $response, just pass what you want $response to be. <-might not be real clear, but that is how my mind is working today! Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 31, 2013 Share Posted August 31, 2013 Sure: say_verb('whatever', 'blah blah blah') Quote Link to comment Share on other sites More sharing options...
eldan88 Posted September 1, 2013 Author Share Posted September 1, 2013 Sure: say_verb('whatever', 'blah blah blah') Why not call it based on true of false like function say_verb($response = TRUE, $say_text) { $output = TRUE ($response) ? "<{$response}>" : ""; $output .= "<Say voice=\"woman\">{$say_text}</Say>"; $output .= TRUE ($response) ? "</{$response}>" : ""; return $output; } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 1, 2013 Share Posted September 1, 2013 Why not call it based on true of false like function say_verb($response = TRUE, $say_text) { $output = TRUE ($response) ? "<{$response}>" : ""; $output .= "<Say voice=\"woman\">{$say_text}</Say>"; $output .= TRUE ($response) ? "</{$response}>" : ""; return $output; } Did you try that? Quote Link to comment Share on other sites More sharing options...
eldan88 Posted September 1, 2013 Author Share Posted September 1, 2013 Did you try that? No but I will try that. Quote Link to comment Share on other sites More sharing options...
Irate Posted September 1, 2013 Share Posted September 1, 2013 I guess you are using XML for that, so why not use simplexml for PHP? It's quite a nice library with very handy methods and properties on your .xml file. If you want to call the string directly, why not use a <response attribute="your string" />? You can then call an eval($xmlfile->response{"attribute"}) (well, to be honest, I forgot the syntax for getting an attribute of an element... but look up the online documentation for simplexml) or whatever you need to do with the string. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted September 2, 2013 Author Share Posted September 2, 2013 I guess you are using XML for that, so why not use simplexml for PHP? It's quite a nice library with very handy methods and properties on your .xml file. If you want to call the string directly, why not use a <response attribute="your string" />? You can then call an eval($xmlfile->response{"attribute"}) (well, to be honest, I forgot the syntax for getting an attribute of an element... but look up the online documentation for simplexml) or whatever you need to do with the string. I never knew about that! I am going to look into that. Thanks for letting me know! Quote Link to comment Share on other sites More sharing options...
objnoob Posted September 2, 2013 Share Posted September 2, 2013 when you define a function, your own function, aka a user defined function, you declare any parameters (data) your function needs in order to execute and perform its duty. the parameter names you assign in your function's declaration can be used throughout your function. function myFunction($parameter_1, $parameter_2){ echo 'can use $parameter_1 once: ' . $parameter_1; echo '<br />can use $parameter_1 twice: ' . $parameter_1; echo '<br />can even change the value of $parameter_1...'; $parameter_1 = 'I\'ve changed'; echo '<br />my new value is....' . $parameter_1; } the user defined function above expects 2 paramters, so I must provide 2 arguments when calling function... $argument_1 = 'Hello'; $argument_2 = 'goodbye'; myFunction($argument_1, $argument_2); So..... if i want to create a function that uses requires a tag name, I declare the function with a parameter to accept the tag name. function createNode($nodeName, $children){ if($children){ echo "<{$nodeName}>"; foreach($children as $child) createNode($child['nodeName'], $child['children']); echo "</{$nodeName}>"; }else{ echo "<{$nodeName} />"; // no children; self closing tag } } in my user defined function above for creating nodes, i use the node name parameter 3 times .... if the node has children it is used to create the open and close tags, otherwise it is used to create a self closing tag ( no children ) Quote Link to comment 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.