In PHP, what is the best way to print a variable through a function?

Asked

Viewed 6,345 times

2

Let’s say I have the following function:

private function teste()
{
    return 'teste';
}

For me to print on the screen, I could just use:

echo $this->teste();

But in some cases I see that first assign to a variable and only after that print, as follows:

$variavel = $this->teste();
echo $variavel;

Creating an unnecessary copy(variable) of the function result would not be wasted processing and memory?

In which cases should we use directly and assigning to a variable?

  • 2

    Its function teste does not return anything, and even if one returns for example the string 'test', the variable inside the function would soon be released from memory once the function was over, consequently would have no problem with memory waste.

  • I ended up getting confused. The correct is to give a Return and not echo in the test function.

3 answers

3


private function teste()
{
    echo 'teste';
}

This method just prints something does not return value, so the following line makes sense

$variavel = $this->teste();

You must change the test method to the following way

private function teste()
{
    return 'teste';
}

And in relation to memory waste you would be mistaken because once the method or function reaches the first Return or at the end of your body, all the variables that are in your scope would be released from memory. Variables declared within a function are called local variables of the function. To learn more search for the scope of variables.

According to the update of the question description, you always assign a value to a variable when you intend to reuse it more than once, or also to make the code more readable.

Assigning a variable only to give it more readability wouldn’t be memory waste, because as I said the variables will only be in memory as long as the context in which it is declared is running (example a function or method). If it were a few decades ago, the answer would be yes due to the limitations of the hardware, but today this is no problem, the readability of the code is more important.

  • Confusing me when writing the function. The correct thing to do is to give a Return to a string as you said yourself.

  • Okay. Memory would not have wasted, but processing waste, making PHP store the return of the "test" function in the name variable "variable"? Then my doubt arises to print direct from the function in relation to create a new variable to assign the return to it to only after this print.

  • It’s not a function, it’s more expensive to be a method for having a private before the name.

  • Perfect. As you said yourself, "you always assign a value to a variable when you intend to reuse it more than once, or also to make the code more readable.". Exactly in use only to be readable I stopped to think if really this would betray performance waste, and PHP would have to assign a value to a variable without need. Obviously if it is used later, it would be necessary to use it. Does the variable scope have anything to do with this part of performance? There isn’t a copy of value in memory like I’m thinking?

2

private function teste()
{
    echo 'teste';
}

The test method prints a value, and returns nothing, so, echo $this->teste() is the same thing as $this->teste().

$variavel = $this->teste();
echo $variavel;

In the example above, you print test, but not by echo $variavel, but by the method because it contains echo 'teste'. in his case $variavel is null because the method did not return anything.

I made an example at ideone, you can see the variable as null.

  • I was confused when writing the question. The correct one is Return instead of echo within the test function.

1

It depends on the case, if you just print the value you have no reason to assign to a variable. Just echo $this->teste().

If the return of the method is used later for a calculation or other operations it makes sense to play the result in a variable.

$res = $this->teste();
$res = formataAlgo($res);
if($res ...) ...
echo $res;
  • Exactly that. The problem is that many people assign the return value of the function to a variable to make the code more organized when the name of the function is used in different algorithms, correcting creating a new variable with a more appropriate name. There then lies my doubt: that would be waste of processing and memory then?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.