abdfahim Posted September 2, 2007 Share Posted September 2, 2007 I have a php file like, for example File1.php: $str="this is static"; echo $str; Now I want to include this file in another file (file2.php), but I want to change the value of $str (in file1.php) from file2.php. Is this possible? Link to comment https://forums.phpfreaks.com/topic/67610-dynamically-change-text-within-a-php-file/ Share on other sites More sharing options...
The Little Guy Posted September 2, 2007 Share Posted September 2, 2007 file1.php $str="this is static"; echo $str; include 'file2.php'; file2.php $str = "<br>this isn't static"; echo $str; final result: this is static this isn't static Link to comment https://forums.phpfreaks.com/topic/67610-dynamically-change-text-within-a-php-file/#findComment-339641 Share on other sites More sharing options...
Fadion Posted September 2, 2007 Share Posted September 2, 2007 Guess u need that for a kind of config file. U cant change the variable's value directly but can use the file handling capabilities of php. In the example i used constants instead of variables. The code will change the value of 'John' to 'Harry' in constant 'NAME'. config file: <?php constant('NAME', 'John'); constant('SURNAME', 'Smith'); ?> change config variables <?php $handle = fopen('config.php', 'w+'); $newValue = 'Harry'; while(!feof($handle)){ $line = fgets($handle, 4096); if(strstr($line, "constant('NAME',")){ $line = "constant('NAME', '{$newValue}');"; } $output .= $line; } fwrite($handle, $output); fclose($handle); ?> Surely there may be a simpler method, but thats what i came with. Link to comment https://forums.phpfreaks.com/topic/67610-dynamically-change-text-within-a-php-file/#findComment-339644 Share on other sites More sharing options...
abdfahim Posted September 2, 2007 Author Share Posted September 2, 2007 The Little Guy .. I don't need to echo two lines. I need to echo only "this is static" or this is not static" depending on situation. hi GuiltyGear, thanx v much .. I think I need exactly what u wrote. Thanx. Link to comment https://forums.phpfreaks.com/topic/67610-dynamically-change-text-within-a-php-file/#findComment-339662 Share on other sites More sharing options...
Fadion Posted September 2, 2007 Share Posted September 2, 2007 Ok glad it helped. If u dont have any other question or problem, mark the topic as solved by pressing 'Topic Solved' in the lower right buttons. Link to comment https://forums.phpfreaks.com/topic/67610-dynamically-change-text-within-a-php-file/#findComment-339666 Share on other sites More sharing options...
abdfahim Posted September 2, 2007 Author Share Posted September 2, 2007 Hi, GuiltyGear .. first thing, I cant replace any line. I copy-paste ur code and it deletes whole contents of the config.php file, i.e. after running 2nd file, config.php becomes blank. I can't debug it because I never use file handling. 2nd thing is, can't I do something like $var=urlencode("config.php") and then changing $var? I don't want to rewrite main file because I guess it takes long time as I may have to rewrite that file many times. So if I can take the contents of the file in a variable and change the variable, I think that will be better. Is that possible? Link to comment https://forums.phpfreaks.com/topic/67610-dynamically-change-text-within-a-php-file/#findComment-339724 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.