Current location: Hot Scripts Forums » Programming Languages » PHP » Constants, define(), and HTML


Constants, define(), and HTML

Reply
  #1 (permalink)  
Old 01-09-05, 06:27 AM
Amulet Amulet is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Southern NJ
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Question Constants, define(), and HTML

I want to set up some constants using PHP's define() function to use in HTML pages. I thought I knew how to do this, but I'm missing something. I assumed you could simply create a file of defines, let's say it's called "global.php," and might look like this:

PHP Code:

define('_1WEEK','1 week');

define('_2WEEKS','2 weeks');
define('_30DAYS','30 days');
define('_EMAIL','E-mail address'); 
And in a separate HTML file, I thought it was supposed to be done something like this:

(Imagine this text is in an HTML file)
Wherever you see the constant name, it would be replaced by the definition, like if I type {_1WEEK}, you'll see it replaced with the text defined for that name, so it would say "1 week" there instead.

I have tried several things, but I don't seem to be able to do it. Basically, I just want to be able to create a separate file of constants, call it to my HTML pages, and have it replace the constant name with the text I define it as. Do I need some sort of small script to get this to work? It seemed simple enough, but I'm just guessing from seeing it used in some CMS scripts (like PHP-Nuke). Can someone tell me how to do this, and what I use (such as php include() or require() ) to call the file? Thanks.
__________________
Jennifer (aka Amulet)
http://www.SassyDevil.com/

Last edited by Amulet; 01-09-05 at 06:29 AM. Reason: [Code] didn't make the text look right
Reply With Quote
  #2 (permalink)  
Old 01-09-05, 09:26 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
ok Jennifer, in order to use the constants you need to have them inside php tags in php executable file..
using constants in pure html files (like file.html) will not work unless you configure the server to parse it as php file..
also you have to be sure that the constant name is not inside any quotes! meaning this is wrong:
PHP Code:

echo "it is _1WEEK"// will print: it is _1WEEK 

the right thing is:
PHP Code:

echo 'it is '_1WEEK// will print: it is 1 week 

doing so inside html (in a .php file) would look like:
PHP Code:

<?php

include_once('my_constants.php');
?>
<html>
<head></head>
<body>
it is <?php echo _1WEEK?>, so whatever!
</body>
</html>
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 01-09-05, 01:45 PM
JBChris JBChris is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Or you could return the page as a string ($page) and use:
PHP Code:

str_replace('{_1WEEK}''1 week'$page); 

To do this you'll have to know how to open and work with files though. If you need help, ask away.
Reply With Quote
  #4 (permalink)  
Old 01-10-05, 04:33 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
Originally Posted by JBChris
Or you could return the page as a string ($page) and use:
PHP Code:

str_replace('{_1WEEK}''1 week'$page); 

To do this you'll have to know how to open and work with files though. If you need help, ask away.
with this, you are doubling the work needed!
why do you define a constant then put it in a page then str_replace() it!?
str_replace() it from the begining ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #5 (permalink)  
Old 01-10-05, 05:11 AM
Amulet Amulet is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Southern NJ
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, I think I know what to do. I forgot to mention that my HTML files are saved as .php files, but I was trying to differentiate web page files from the php definitions.
__________________
Jennifer (aka Amulet)
http://www.SassyDevil.com/
Reply With Quote
  #6 (permalink)  
Old 01-10-05, 04:55 PM
JBChris JBChris is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Yeah, that's what I was saying. I guess I didn't make that clear.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
html tutoral thefrtman HTML/XHTML/XML 5 04-27-09 10:25 AM
formmail problem gscraper Perl 12 08-27-04 03:06 AM


All times are GMT -5. The time now is 05:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.