Adam
Adam

Reputation: 623

PHP 101: variable vs function

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";
}

Upvotes: 5

Views: 1225

Answers (5)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

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

Upvotes: 2

m4t1t0
m4t1t0

Reputation: 5731

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.

Upvotes: 1

BlackCat
BlackCat

Reputation: 521

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"> 
...

Upvotes: 4

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167240

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

Upvotes: 1

rofls
rofls

Reputation: 5115

$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.

Upvotes: 1

Related Questions