5

I'm creating a global file to hold items that will be re-used throughout my website. What are the differences between these two lines of code? Is one "better" than the other?

This:

$logo = "img/mainlogo.jpg";

vs this:

function logo() {
   echo "img/mainlogo.jpg";
}
6
  • actually you save some bytes in the ram if you dont use a variable Commented Dec 9, 2012 at 17:57
  • i think second one is better Commented Dec 9, 2012 at 17:57
  • i'd change echo to return, but personally i like storing configuration together in an array or some object rather than having a separate variable for it Commented Dec 9, 2012 at 17:58
  • Why do you think the second is better? Commented Dec 9, 2012 at 18:00
  • 4
    Apples and oranges, they are not equivalent. Commented Dec 9, 2012 at 18:08

5 Answers 5

4

You should code clear and readable and split in the html and php. The performance profit is not significant...

<?php
...
$logo = "img/mainlogo.jpg";
...
?>
...
<img src="<?= $logo ?>" alt="logo"> 
...
Sign up to request clarification or add additional context in comments.

Comments

2

Of the two options you posted, the function is the better choice. But, to be brutally honest, this sort of thing is exactly what constants are for:

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

suppose you're working on a site that has to support multiple languages, then you can simply 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...

Besides, a constant, once defined cannot be redefined (clue is in the name, of course). Also: since PHP still has a lot of C-stuff going under the bonnet, and you've tagged this question performance, it might interest you that constants are much like C's macro's, which are a lot faster than regular function calls, or even C++ inline functions (even if they were indeed compiled as inline functions).

Anyway, if you have a ton of these things you want to centralize, either think of creating a couple of ini files for your project, and parse them into some sort of global object

Comments

1

Functions are good.

I see that function logo() is better than $logo. echo doesn't take much memory, but $logo does. Even though, function logo() takes something, it will be handled by PHP's very own garbage collector. You can also use these functions to ensure that you are not misusing the memory allocated.

  1. memory_get_peak_usage();
  2. memory_get_usage();

Explanation:

Upon the ending of an in use function PHP clears the memory it was using, at least more efficiently than if not using a function. If you are using recursive code or something similar that is memory intensive try putting the code into a function or method, upon closing of the function/method the memory used for the function will be garbaged much more efficiently than that of unsetting variables within the loop itself.

Source: 7 tips to prevent PHP running out of memory

8 Comments

-1, that article is full of edge cases, blatantly silly practices and micromanagement. It is not a good resource and should not be used as an authoritative source.
@Charles I took the explanation from there, okay? It is no way related to what I said. For the sake of (no plagiarism), I gave the link. If you want, I can remove it. Removing the downvote?
but it sounds like there's nothing WRONG with using a function over a variable, even if it can be seen as micromanagement.
Yeah, have been in the PHP Server Admin for years, so my suggestion would be good. :)
ideone.com/YVmnrj and ideone.com/kxjpQ1. Results are different from your statement.
|
1

$logo = "img/mainlogo.jpg"; can be redefined naturally later without changing code by doing this $logo="img/newmainlogo.jpg"; whereas the function would have to be modified itself, in its first definition.

2 Comments

You mean I couldn't just redefine the contents of the function the same as the variable?
@Adam, not at runtime.
1

The main purpose of a function is to avoid code repetition and perform a specific task. Based on that definition, using a function to only return a value is a bad design.

In that context I think is better a good readability in the code than to save several bytes of memory. We are in 2012, optimization is good but this type of micro-optimization is simply ridiculous. I prefer assigning a variable, it's clear and do what you expect.

1 Comment

Yeah, I was thinking a list of variables would be cleaner and it does feel like a misuse of functions; overkill or something...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.