Hi , i have one problem regarding variable's access ... i hv defined some varibale like DEFINE('_xx_USERNAME','Username'); in one file ... and in another file , i got this variable (_xx_USERNAME) as a value of another variable ... like $filedValue = '_xx_USERNAME' ; now , the problem is i want the value of _xx_USERNAME in the second file ... that is Username... by using variable $filedValue ... how could i do this ? Tarjan
not exactly sure what you want to do, but its no problem to pass variables between different files. if you have your DEFINE('USERNAME','User 1'); PHP: in a file "username.php" and need it somewhere else, just use require_once('username.php'); echo USERNAME; PHP: or if you have a function you can make important variables global: require_once('username.php'); $db = new Database(); function update_login() { global $db; //makes your Database connection available inside this function global USERNAME; //same for username $sql="UPDATE users SET last_login=NOW() WHERE username='.USERNAME.'"; execquery($sql); //... } update_login(); PHP: hope you got the idea behind...?
as far as I understand, you want the value of constant _xx_USERNAME to copied over to $filedValue variable. You can do this as follows: $filedValue = _xx_USERNAME; PHP:
DEFINE('USERNAME','User 1'); PHP: in a file "username.php" and need it somewhere else, just use require_once('username.php'); $filedValue = USERNAME; echo $filedValue; PHP: You cannot express defined constants as variables -- using the dollar sign in front of the variable name. The above example needs to be written as echo USERNAME to ensure that it displays "User 1" The variable $USERNAME could be populated with an entirely different result. Important gave you the correct solution from getting the constant value into a local variable. Just remember to include the file which contains the constant value.
Hi falcondriver, thanks for advice but my problem is little bit diffrant then you suggested .... actully i want to compare 2 files which has all most same variable's but the values are diffrant .... for e.g. 1 file has username="abc" and other has username = "bcd" ... my actul code is like ... my first file contains ... file1.php <?php DEFINE('_EZ_USERNAME','Username'); DEFINE('_EZ_PASSWORD','Password'); ?> PHP: my second file contains ... file2.php <?php DEFINE('_EZ_USERNAME','Gebruikersnaam'); DEFINE('_EZ_PASSWORD','Wachtwoord'); ?> PHP: and third file has code like ... function comp_file ($Filename){ include( $Filename); $fpEng = "/file1.php"; $lines = file($fpEng); foreach ($lines as $line_num => $line) { $line = str_replace("<?php","",$line); $line = trim($line); if ($line == ""){ next($lines); } elseif($line == "?>"){ next($lines); } else { $line = str_replace("DEFINE('","",$line); $line = str_replace("','",",",$line); $line = str_replace("');","",$line); list($fieldNm, $caption) = explode(",", $line ); /*$$fieldNm = $fieldNm; $tmp = $fieldNm ; //eval($fieldNm) eval("\$tmp = \"$tmp\";");*/ echo $caption ; echo '<input name="<?php echo $fieldNm ;?>" value="<?php //here i want value form($Filenamefile ) ; ?>" type="text" size="70">' ; } } } comp_file(file2.php); PHP: so now when i do read file2 i got $caption = 'UserName' ... and $fieldNm = '_EZ_USERNAME' by using this $fieldNm ' s value i want to use the variable of file2 .... i think this will help you more in help me out
You do not define constants for use in this manner. Constants are constants. Their value is never supposed to change. For example: define( 'ZERO', '0') This means that the constant '_EZ_USERNAME' is supposed to hold the same value across the entire program. You should be using variables to hold the values that you want to set. Variables are intended to be manipulated and changed. You are doing a whole lot of messing around to extract the assigned value of the constant/variable when all you need to do is include the file and use the variable. When you start talking about calling things a fieldname it gives me the impression you are really trying to do something database oriented. Is this the case? If you just want to create a system for putting keyword==value pairs in separate files than you are on the right track by iopening the file, parsing it, and extracting the keyword and value and then opening the another file and looking to see if it has the same keyword and then extracting the value. Getting this to work with a recursive function requires that you pass the name of the keyword that you are seeking to the function, but writing the function with a default value and action. For instance: comp_file ($Filename, $keyword=""){ == open file # if keyword is defined find the line with the keyword and extract the comma or tab separated value # otherwise locate a keyword, get its value and call the comp_file function to see if it is the next file has the keyword # do something like print out the information However, I think a database may really be where you want to go with this application. Otherwise, just have php files with $variable = value that you just keep calling -- bearing in mind the $variable will be reset to the new value unless you save it, as in $oldVariable = $variable
hi clancey, thanks for advice, but i don't want to compare those files ...,what i want to do it is ... i want to edit file2 variable (which is in different language. e.g. french), by using caption of file1's variable (its value , i defined it as a caption, which is in English). i want that mechanism by which i can do edit variables of different file using the English variables(conversion)... for that i want to use 2 file at a time... file2's variable is same as file1... only the language is different ... now i think you got my point. and can help me out ...
You could just create separate language files which use the same variable name, but which have each language's translation of the value. All you do is come up with a naming scheme and then when people are viewing your site in French, PHP calls the French file and when they are viewing the English version, PHP calls the English file. I would not use defines for this, I would just create a language pack and come up with a mechanism for PHP to know which language to use. All my sites are 100% English, so I would not know how to implement this. However, many open source projects alow for the integration of other languages. Look at how they did it.
Hi clancey , thanks for advice, i m working on open source projects too .... i got the solution for my problem .... form the http://in.php.net/constant thanks to all who have try to help me out ...