Jump to content

Passing an argument as opening and closing tag


eldan88

Recommended Posts

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

Link to comment
Share on other sites

 

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");

Link to comment
Share on other sites

 

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;
}
Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.