lowlithium Posted January 21, 2014 Share Posted January 21, 2014 hi everyone, I'm struggling with this code, I want to put inside a string a var sent by a submit. I need that the var $someone changes, to the right name. This is not working, how can i do this? $layout=' <h2 class="title"><a href="#">Title</a></h2> <p class="meta">bla bla bla by <a href="#">'.$someone.'</a></p> <div class="entry"> </div>'; if($someone =="john") echo $layout; else $someone ="mary"; echo $layout; thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/285556-var-inside-a-string-var/ Share on other sites More sharing options...
.josh Posted January 21, 2014 Share Posted January 21, 2014 Is there any reason why you can't just do it like this? if ($someone !='john') $someone = 'mary'; $layout=' <h2 class="title"><a href="#">Title</a></h2> <p class="meta">bla bla bla by <a href="#">'.$someone.'</a></p> <div class="entry"> </div>'; echo $layout; Basically the point is that when you assign the value to $layout, php has already parsed the string and made the assignment. So the only way to change it is to either a) Assign the value to $layout again (the whole string including the var, like you initially did). This isn't very efficient. At best it's double coding. b) Do a search/replace of the name on $layout. In principle, this is how template engines work. Basically instead of using $someone in your $layout assignment, you'd use a unique placeholder, something like "[[someone]]" and then you'd use something like str_replace to replace "[[someone]]" with $someone. This isn't a bad method if you're actually trying to make a full template engine, but it's a bit overkill otherwise. c) Re-order your logic to have the final value of $someone when you assign it to $layout in the first place. This is the most efficient method and what you should go for (and what I did in the example above), if you aren't trying to make a full template engine out of this. On a sidenote, you should look into HEREDOC syntax for multi-line string assignment. It's a lot cleaner and easier to read than how you're currently assigning the value to $layout. For example: $layout=<<<EOS <h2 class="title"><a href="#">Title</a></h2> <p class="meta">bla bla bla by <a href="#">{$someone}</a></p> <div class="entry"> </div> EOS; Notice how in this syntax, I do not have to break out of quotes to insert a variable. Instead, I wrap the variable in curly brackets {}. You don't even have to do that in this specific example, since php understands from the surrounding content that $someone is the full var name (because $someone< isn't a valid variable name). But I like to use {} regardless, because for example if you wanted to do $someoneblahblah and really wanted php to parse for the variable $someone, well it's going to look for a variable called $someoneblahblah. So you can disambiguate with the curly brackets by doing {$someone}blahblah. In any case, point is that you don't have to worry about breaking out of quotes for vars or escaping quotes or anything, since <<<EOS and EOS; act as the string delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/285556-var-inside-a-string-var/#findComment-1466046 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.