define( ) or not

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

define( ) or not

Post by kryles »

Hi,

I'm just wondering if using define() a lot takes up many resources? For instance I'm making fields for a registration page as such:

Code: Select all

 
 
reg.html
 
<tr>
<td>
    <?php echo LASTNAMELABEL; ?>
</td>
<td>
       <input type ............>
</td>
</tr>
 
constant.php
<?php
define("LASTNAMELABEL", "*Last Name: ");
?>
 
I'm not asking about the code syntax, I would just like to know if define is a lot more greedy than simply doing

Code: Select all

$LASTNAMELABEL = "*Last Name: ";
Or if I'm going overkill and I should just type *Last Name: directly in the html

Thanks,
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: define( ) or not

Post by Maugrim_The_Reaper »

It depends, I think using variables is more flexible since you can change their value from a set of default, by including more specific value sets on a per app, per language, basis. Using constants makes that almost impossible.

As for performance - both are quite efficient, and I doubt the difference would have a significant impact on your application.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: define( ) or not

Post by pickle »

I usually define constants for values I'm sure I don't want to change throughout the application. It also has the advantage of setting a variable that I don't have to worry about passing. In your examples, setting a constant with define() allows you to use that variable absolutely anywhere, whereas setting it traditionally means you'd have to pass that variable around.

If you're currently set up to use define(), I'd say continue. Do make sure though that all your define() statements are in one place, as it'll be hell to find the value of a particular constant if you've got define() sprinkled throughout your code.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: define( ) or not

Post by kryles »

yah I always have one file specifically for constants (creatively called constants.php :D )

Yah they are labels to identify textboxes so I don't see them changing, so I'll go with defines if there is not a noticeable performance issue.

Thanks all :drunk:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: define( ) or not

Post by Benjamin »

I ran tests on this, if I remember correctly defining constants uses less memory and is faster than setting variables or arrays.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: define( ) or not

Post by Christopher »

kryles wrote:I'm just wondering if using define() a lot takes up many resources? For instance I'm making fields for a registration page as such:

I'm not asking about the code syntax, I would just like to know if define is a lot more greedy than simply doing

Or if I'm going overkill and I should just type *Last Name: directly in the html
I'll be the one to ask the Premature Optimization questions: Are you running out of "resources" (I am guessing either CPU or memory)?

You should use defines for what they are intended for, which is to provide read-only values for your scripts. If you are having performance problems then switching var to defines, or back, will hardly be among the solutions.
(#10850)
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: define( ) or not

Post by kryles »

I guess that might be a reasonable assumption. The php will already know how much memory to set aside, whereas with variables it has to calcualte on the fly....maybe?

No performance issues, was more just wanting to code using good practices.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: define( ) or not

Post by Christopher »

PHP will allocate and free all memory on each request. The idea behind avoiding Premature Optimization is that you should focus first on good design and then optimize later as needed. The reason is that optimizing early can narrow your design options and in the long run can produce both a poorer design and poorer performance. All this does not mean that you should not be aware of performance issues; but they are usually contained in best practices anyway.
(#10850)
Post Reply