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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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.'; ?> Quote Link to comment 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 Quote Link to comment 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; ?> Quote Link to comment 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). Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.