Minase Posted January 4, 2009 Share Posted January 4, 2009 hy there ,i have the following code define ("test", "a value ..."); $text = 'Welcome to test ,enjoy your stay'; // the output should be $text = 'Welcome to a value... , enjoy your stay'; i tryed eval but doesnt work ... thank you Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/ Share on other sites More sharing options...
premiso Posted January 4, 2009 Share Posted January 4, 2009 $text = 'Welcome to ' . test . ',enjoy your stay'; A note, it is good practice to put constants in caps so people know that it is a constant. Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/#findComment-729423 Share on other sites More sharing options...
Minase Posted January 4, 2009 Author Share Posted January 4, 2009 i want something different.i know that way but the code look a little messy :| thx for reply. saw at someone that he used eval or something similar Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/#findComment-729425 Share on other sites More sharing options...
DarkWater Posted January 4, 2009 Share Posted January 4, 2009 Constants don't magically interpolate into strings. You'll either need to use a variable or concatenate. <?php $test = "a value ..."; $text = "Welcome to $test ,enjoy your stay"; //notice the DOUBLE QUOTES ?> <?php define("test", "a value ..."); $text = 'Welcome to ' . test . ' ,enjoy your stay.'; ?> Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/#findComment-729426 Share on other sites More sharing options...
Minase Posted January 4, 2009 Author Share Posted January 4, 2009 i dont want to use double quotes i already have lots of them and i dont want to escape characters too much Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/#findComment-729427 Share on other sites More sharing options...
jonsjava Posted January 4, 2009 Share Posted January 4, 2009 hy there ,i have the following code define ("test", "a value ..."); $text = 'Welcome to test ,enjoy your stay'; // the output should be $text = 'Welcome to a value... , enjoy your stay'; i tryed eval but doesnt work ... thank you it sets a named constant. the only way you can use that so it works would be something like <?php define ("TEST", "a value ..."); $text = "Welcome to ".TEST." ,enjoy your stay"; print $text; ?> now, to replace, I would use <?php $text = "Welcome to test ,enjoy your stay"; $text = str_ireplace("test", "a value....", $text); print $text; ?> Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/#findComment-729428 Share on other sites More sharing options...
DarkWater Posted January 4, 2009 Share Posted January 4, 2009 @jonsjava: That's going to probably be much slower than having PHP handle the interpolation. @Minase: Get over it and use double quotes or structure your code better (i.e: a templating system). Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/#findComment-729431 Share on other sites More sharing options...
Minase Posted January 4, 2009 Author Share Posted January 4, 2009 yes i will add a template system its better. thank you Link to comment https://forums.phpfreaks.com/topic/139444-solved-string-question/#findComment-729434 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.