Am I able to pass and return different variables for a function?


#1

I have recently started looking into using functions as for the first time, I have some repetitive code. However after reading up on functions and understanding the basics I am unsure whether what I am after is possible. Basically:

I have five variables:

$message1, $message2, $message3, $message4, $message5

These contain formatted messages with paragraphs, bolden, etc. What I want to do is get the first paragraph of each variable and then decode, to display nicely. What I have currently is:

$message1 = preg_split('/((<\s*p\s*\/?>\s*)|(<\s*br\s*\/?>\s*)|(\s\s+)|(<\s*\/p\s*\/?>\s*))+/', $message1, -1, PREG_SPLIT_NO_EMPTY);
echo htmlspecialchars_decode($message1[0]);

(Obviously in a function I wouldn’t be using echo). But I am using this piece of code for each variable. I am trying to create a function that allows me to pass each variable through and then return the variable back to use. I have looked around and read up lots on functions but I think I have got to a point where I have completely lost the plot.

My theory (which I know is wrong, but unsure where to go from) is:

function displayMessage($message) {
     $message = preg_split('/((<\s*p\s*\/?>\s*)|(<\s*br\s*\/?>\s*)|(\s\s+)|(<\s*\/p\s*\/?>\s*))+/', $message, -1, PREG_SPLIT_NO_EMPTY);
     $message = htmlspecialchars_decode($message[0]);
     return;
}

echo displayMessage($message1);
echo displayMessage($message2);
// etc.

I know this won’t work but I have no idea how it could be done. Could someone point me in the right direction or suggest another way that I could get this done?

Thanks :slight_smile:


#2

You’re virtually there. Simply add the variable name on the end of your return statement.

return $message;

You can then echo it, or use it locally, or whatever. If you need to return more than one variable, you could return them as an array.


#3

@droopsnoot Thank you! I was confused as I thought using

return $message;

would return the same message for all of them. That’s what was confusing me. I’m chuffed to bits that I nearly had it :smiley:


#4

Then you should read about Scope (http://php.net/manual/en/language.variables.scope.php)


#5

I don’t use the parameter name within the function, if I’m going to change the value of it, which makes it clearer that it isn’t the original value you passed in. In fact I stopped doing that many years ago in a language that didn’t allow it, but I’ve kept it as a habit. So in your function I’d do something like

function displayMessage($message) {
     $newmessage = preg_split('/((<\s*p\s*\/?>\s*)|(<\s*br\s*\/?>\s*)|(\s\s+)|(<\s*\/p\s*\/?>\s*))+/', $message, -1, PREG_SPLIT_NO_EMPTY);
     return htmlspecialchars_decode($newmessage[0]);
}

so it’s really clear that what comes back is not what was passed in, while you get used to things.


#6

@Dormilich I did come across that before, but it made no sense to me. Now that I know what it’s in reference to and have something working it means a lot more. Thank you :slight_smile:

@droopsnoot That does make a lot of sense and is very logical so I’ll be able to remember that for future reference. :slight_smile: