Does PHP declare an unnecessary variable?

I usually do this in PHP for better readability, but I don’t know if it consumes memory or any other problems? Let's say I have this code:

$user = getUser(); // getUser() will return an array 

I could do:

 $email = $user["email"]; sendEmail($email); 

Without declaring the $ email variable, which I could do:

 sendEmail($user["email"]); 

Which one is better? Think that this is just a very simple example.

+6
source share
2 answers

Do not make the code less readable to save several bytes. And this will not save you more, even if $email is a 100 MB line, because inside PHP uses the copy on write mechanism: the contents of the variable will not be copied unless you change it.

Example:

 $a = str_repeat('x', 100000000); // Memory used ~ 100 MB $b = $a; // Memory used ~ 100 MB $b = $b . 'x'; // Memory used ~ 200 MB 
+4
source
Yes it is! The variable creates a storage area so you can use and manipulate them. if not in use, in this case memory consumption will occur. sendEmail($user["email"]); would be a better way. but this is not a good way to write codes without variables. Through a variable, you can track actual errors, if any, and also helps in scalability, readability.
0
source

All Articles