Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 09-19-2007, 04:16 PM DEFINE? benifits?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Im now completely rewriting a CMS and really want it to be good (done everyone?) and am trying to get it as flexible as possible.

anyway i noticed that on alot of big scripts they seem to use DEFINE for alot of things :/ whats the benifits of using this over than jsut haveing a $var with the info or something?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 09-19-2007, 06:57 PM Re: DEFINE? benifits?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Just 2
1) Once a constant have been defined, it cannot be changed.
2) If you define a constant in a php page directly, it will be available even in a class/function where a $variable would need to be explicitly passed to the function.
Constants are not limited by a scope.

Appart from that, there are no differences in the two.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-19-2007, 07:08 PM Re: DEFINE? benifits?
coolkbk585's Avatar
Be good this Christmas!

Latest Blog Post:
KBlog has been deativated
Posts: 642
Name: Kyle
Location: Ada, MI
Trades: 0
Just for number 2 that you mentioned, I think that it would be worth it.
__________________
<?php if($Adsense_Revenue > 0): define('HAPPINES','100%'); else: define('HAPPINESS', '0%') endif; ?>
coolkbk585 is offline
Reply With Quote
View Public Profile Visit coolkbk585's homepage!
 
Old 09-19-2007, 07:26 PM Re: DEFINE? benifits?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
so would it be advised if i have site wide settings or soemthing like that to use it?

how are they used
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 07:46 PM Re: DEFINE? benifits?
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
There are other ways to gain superglobal-like scope then through constants (lookup the "singleton" pattern for example). You should only use constants for values that should never change.

Application-wide values are best kept in constants: Initiation variables (database names, paths, modes like debug mode), error codes, option names and things like that. Actual settings for the app like "Show number of users on index?" and such should be in variables. If it will ever change in the use of the app, then it should probably be in a variable.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 09-19-2007, 08:12 PM Re: DEFINE? benifits?
coolkbk585's Avatar
Be good this Christmas!

Latest Blog Post:
KBlog has been deativated
Posts: 642
Name: Kyle
Location: Ada, MI
Trades: 0
Well, you can see how define works in my signature :P

http://www.php.net/define
__________________
<?php if($Adsense_Revenue > 0): define('HAPPINES','100%'); else: define('HAPPINESS', '0%') endif; ?>
coolkbk585 is offline
Reply With Quote
View Public Profile Visit coolkbk585's homepage!
 
Old 09-20-2007, 07:59 AM Re: DEFINE? benifits?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
yea i have seen how to define i just mean to actually like use the thing after it has been defined?

like for instance if i wanted to use

define('prog_version','1.0');

how would i then show prog_version?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-20-2007, 08:25 AM Re: DEFINE? benifits?
Ultra Talker

Posts: 483
Trades: 0
The link that coolkbk585 gave you (not his signature) shows in the example EXACTLY how you would show prog_version. And it's not like it's a difficult link to come by!

Geez Dan, and here I was thinking that you had started to research things by yourself...
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-20-2007, 09:51 AM Re: DEFINE? benifits?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
PHP Code:
define('prog_version','1.0');

echo 
"your program version is ".prog_version;  // ::> right
echo "your program version is prog_version";   // ::> false, prog_version will not be interpreted 
Simply, you cannot use an define into a string.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-20-2007, 10:32 AM Re: DEFINE? benifits?
Ultra Talker

Posts: 483
Trades: 0
tripy: I don't get your post. Are you saying that that is a benefit or are you just making a statement about the fact that it doesn't get interpreted?
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-20-2007, 10:42 AM Re: DEFINE? benifits?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I'm making a fact.

I was just explaining to Dan, that he cannot use a defined value in a string like we do with a variable.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-20-2007, 11:55 AM Re: DEFINE? benifits?
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
I think define is worth while for security when RAW is concerned.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 09-20-2007, 11:59 AM Re: DEFINE? benifits?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Ok sorry i missed the link there thanks,

What do you mena by RAW?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-21-2007, 02:40 AM Re: DEFINE? benifits?
Novice Talker

Posts: 9
Trades: 0
You can use define when you have alot of php files included in each other and want to define a variable that will never change.

I personally use it alot for paths. For sample you don't want to rewrite the path of your image folder evry time you use it.

define('IMAGE_PATH','site/files/lost/somewhere/ontheserver/images/');

echo "<img src='IMAGE_PATH$image'>";

you can notice that you don't need to use $ or any " because IMAGE_PATH will be replaced by 'site/files/lost/somewhere/ontheserver/images/' before the script is compiled. Its like if you would use "replace" in a text file or str_replace on a string.

i also personally use it for option, for sample if you want to put your website on debug mode.

define('MODE','1'); // 0 = normal, 1 = debug





Last edited by Bakunin; 09-21-2007 at 02:42 AM..
Bakunin is offline
Reply With Quote
View Public Profile
 
Old 09-21-2007, 11:20 AM Re: DEFINE? benifits?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Thanks all, i was mainly curious as it seemed very few and yet alot of scritp use it, (that makes sense to me )
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to DEFINE? benifits?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46570 seconds with 12 queries