Jump to content
Larry Ullman's Book Forums

Using A Function On More Than One Variable


Recommended Posts

I finished PHP Visual Quick Start Guide 4th edition and now I'm on to PHP and MySQL for Dynamic Web Sites. I decided not to skip ahead to the new info and use the begining chapters as a refresher course. There's one thing I'm confused about and I admit, I really should know this.

 

I'm confused on the syntax of how to use a function on multiple variables in the same expression. For instance, if I wanted to use the strip_tags() function on the $comments, $email, and $name variables in the same expression how would that look?

 

I apologize in advance if I'm not using the right vocabulary to explain myself.

Link to comment
Share on other sites

It depends a lot on the given function, as some functions can take multiple values and/or arrays and process them all at once, whereas other functions can only take one value at a time, and you just have to call the function separately for each variable.

 

As a general rule, I recommend using the site PHP.net to get information on PHP functions. Usually, if you type "PHP function-name" into a search engine, the corresponding PHP.net page will be the first hit.

 

In the case of strip_tags, it can only take one argument at a time, so you would need to call the function three times to process all three variables. How you store the results of the function calls is also up to you.

 

For example, you could store the result of the function call for each variable in a new variable. For example:

 

$comments_strip = strip_tags($comments);
$email_strip = strip_tags($email);
$name_strip = strip_tags($name);

 

Or you could store the trimmed values back into the same variables:

 

$comments = strip_tags($comments);
$email = strip_tags($email);
$name = strip_tags($name);

 

Or if you want, you can output the results to the screen in a string:

 

echo strip_tags($name) . ', who has the email address ' . strip_tags($email) . ', wrote the following comment:<br>' . strip_tags($comments);

 

Or you could store them in an array:

 

$strip_arr = array(strip_tags($comments), strip_tags($email), strip_tags($name));

 

Or, if this is the sort of thing you will have to do a lot, you could write your own function that extends the strip_tags function, allowing you to strip the tags off of multiple variables and/or arrays in one operation, and then return some sort of easy-to-deal with array.

 

Point being, there are lots of options, and because every PHP function is different, the best thing to do is check out the reference material available at PHP.net.

  • Upvote 1
Link to comment
Share on other sites

Oh, okay. So there isn't some basic syntax that I was forgetting. I had been wanting to call strip_tags for all of the variables but kept wondering if I was doing a bunch of unecessary steps. I had some vague notion that it could be done, but if every function takes a unique number of arguments then I must have been remembering one that did. Thanks that really set my head on straight.

Link to comment
Share on other sites

If you find yourself having to call the function a lot, you may want to do what I suggested, which is place all the variables in an array, and then send that array to your custom function, which can loop through the array, trim all the values, and returned all those trimmed values from another array.

  • Upvote 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...