In general there are no problems.
Can whether the past type has semantics of passage by reference. Obviously in this case the change will affect the variable that was used as an argument, if a variable was used instead of a literal, which would then have no problems because obviously it could not be used elsewhere, but in this case there was more the passage of the reference and not by reference.
In a kind string a reference is passed and does not copy the data as occurs with a number for example, but the passage is not by reference, so any change in it, does not reflect the variable used in the call.
Then nothing changes at all. Actually your example seems very interesting to reassign a value to the same parameter variable because in the background is the same given.
I will not go into the merit of the parameter name being bad because it is an example, but in a condition style question this can be important. If you’re using meaningful names, then assigning to another variable can make a difference in readability, having names that indicate what that text is. If the parameter was called $texto
, could call the new variable $texto_formatado
to make it clear that the content there had the change. Give meaning to the content.
Reinforcement that in simple cases so this type of readability is exaggerated and does not make sense, but can do in more complex code. Although some will say that more complex codes is already bad practice.
I know it’s just an example but since we’re on the subject, in which case the variable is even needed, this function would only need to have the return
with the expression. That’s why I said it depends on the context whether you need to give readability or not. In general I think you don’t have to keep wanting to document all operations by creating intermediate variables, I only do this when it’s really confusing, but it’s taste.
Of course, if you need to use the original value of the parameter other times, you cannot discard it. And it would be silly to avoid discarding save this value in another variable and reassign a new value in this parameter.
In case the variable is global and it has some other utility, then it would give a huge problem, I do not believe that there is any problem, it depends on the scope of the project.
– MarceloBoni
@Marcelobonifazio, using global variable, by itself, is already a bad practice. In this case then...
– Wallace Maxters
It’s never a problem if you have conviction of what you’re doing.
– Guilherme Lautert