orbstra Posted January 1, 2007 Share Posted January 1, 2007 Hey! I need a function in my program which returns the value of a given constant name. All my constants are in an external file. I need this function to work like, if you need to use the value of a certain constant you use option_request('TP01') . The TP01 is an option in the config file, it is a constant. this is what I have so far, it's more or less nothing, I am in very need of help.[code] function opt($num) { require_once('guav-config/config.php'); $return = const $num; return $return; }[/code]thanks Link to comment https://forums.phpfreaks.com/topic/32476-constant-value-request-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2007 Share Posted January 1, 2007 Use the defined(...) function. From the PHP manual -[code]<?php/* Note the use of quotes, this is important. This example is checking * if the string 'CONSTANT' is the name of a constant named CONSTANT */if (defined('CONSTANT')) { echo CONSTANT;}?> [/code] Edit: Sorry, I mis-read the question. See the correct answer in the following post - Link to comment https://forums.phpfreaks.com/topic/32476-constant-value-request-function/#findComment-150858 Share on other sites More sharing options...
ShogunWarrior Posted January 1, 2007 Share Posted January 1, 2007 You use the function [b]constant()[/b]e.g:[code]echo( 'The value of constant TP01 is' . constant("TP01") );[/code] Link to comment https://forums.phpfreaks.com/topic/32476-constant-value-request-function/#findComment-150860 Share on other sites More sharing options...
orbstra Posted January 1, 2007 Author Share Posted January 1, 2007 [quote author=ShogunWarrior link=topic=120590.msg494711#msg494711 date=1167678111]You use the function [b]constant()[/b]e.g:[code]echo( 'The value of constant TP01 is' . constant("TP01") );[/code][/quote]I keep gettin an error when I use my function (code below), like this:[code]//Used like:include(get::dir('theme') . '/' . get::opt('TP01') . '/head.php');//Function found later in the scriptfunction opt($num) //TODO: Finish get::opt() function{ require(get::dir('config') . 'config.php'); $return = constant($num); global $return; return $return;}[/code]this is the error I get:[code]Warning: constant() [function.constant]: Couldn't find constant TP01 in C:\Documents and Settings\Administrator\My Documents\My Projects\Web Projects\Guava\guav-includes\guav_library.php on line 224[/code]TP01 is clearly defined in config.php as[code]//The theme you would like to use (guav-template/)DEFINE("TP01", "default");[/code]thanks Link to comment https://forums.phpfreaks.com/topic/32476-constant-value-request-function/#findComment-151043 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.