PHP 101: vs function variable

I am creating a global file to store items that will be reused on my website. What are the differences between these two lines of code? Is it “better” than another?

It:

$logo = "img/mainlogo.jpg"; 

vs this:

 function logo() { echo "img/mainlogo.jpg"; } 
+4
source share
5 answers

Functions are good.

I see that function logo() better than $logo . echo does not take up much memory, but $logo does. Despite the fact that function logo() takes something, it will be handled by PHP with a very proprietary garbage collector. You can also use these functions to not overuse the allocated memory.

  • memory_get_peak_usage();
  • memory_get_usage();

Explanation:

At the end of the function used, PHP clears the used memory at least more efficiently than if it had not used the function. If you use a recursive code or something similar that uses memory intensively by putting the code in a function or method, then when closing a function / method, the memory used for this function will be reset much more efficiently than resetting variables in a loop by itself.

Source: 7 Tips for Preventing Out of Memory PHP

+1
source

You should make the code clear and readable and split into html and php. Performance gains are negligible ...

 <?php ... $logo = "img/mainlogo.jpg"; ... ?> ... <img src="<?= $logo ?>" alt="logo"> ... 
+4
source

Of the two functions you proposed, the function is the best choice. But to be rude honest, this is what constants are for:

 defined('MAIN_LOGO') || define('MAIN_LOGO','img/mainlogo.jpg'); 

Suppose you are working on a site that should support multiple languages, then you can just use the same trick:

 defined('CLIENT_LOCALE') || define('CLIENT_LOCATE',$whereverYouGetThisFrom); defined('MAIN_LOGO') || define('MAIN_LOGO','img/mainlogo_'.CLIENT_LOCALE.'.jpg'); //if language is EN, mainlogo_EN.jpg will be used, if lang is ES, mainlogo_ES.jpg, etc... 

In addition, a constant, once defined, cannot be overridden (the key in the name, of course). Also: since there are still a lot of C-things in PHP that are under the hood, and you noted this performance question, you might be wondering what constants are very similar to C macros, which are much faster than regular function calls, or even C ++ built-in functions (even if they were really compiled as built-in functions).

In any case, if you have a ton of these things that you want to centralize, consider creating a pair of ini files for your project and analyze them on some global object

+2
source

The main purpose of the function is to avoid repeating the code and complete a specific task. Based on this definition, using a function only to return a value is a poor design.

In this context, I believe that code readability is better than storing a few bytes of memory. We are in 2012, the optimization is good, but this type of micro-optimization is just ridiculous. I prefer to assign a variable, it is clear and does what you expect.

+1
source

$logo = "img/mainlogo.jpg"; can be overridden in a natural way without changing the code by doing this $logo="img/newmainlogo.jpg"; , while in its first definition the function must be modified by itself.

0
source

All Articles