JMair Posted April 1, 2009 Share Posted April 1, 2009 what I'm trying now <?php $foldername = 'ABBA'; //some folder name echo '<a href="media/music/$foldername">$foldername</a>'; ?> I hope you can see what I'm trying to do. But my return is. $foldername So how can I add a variable into this link? Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/ Share on other sites More sharing options...
chmpdog Posted April 1, 2009 Share Posted April 1, 2009 Oh this is pretty, actually really easy to do. Basically you need to tell php to "stop" echoing. You do this by removing the quotes. Here take a look: $foldername = "asdf"; echo '<a href="media/music/$foldername">'.$foldername.'</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798283 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 You need the whole string in double quotes or else the variables won't interpolate. For attributes use single quotes. echo "$foldername"; Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798284 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 Oh this is pretty, actually really easy to do. Basically you need to tell php to "stop" echoing. You do this by removing the quotes. Here take a look: $foldername = "asdf"; echo ''.$foldername.''; Your code just needs a minor adjustment to work $foldername = "asdf"; echo '' . $foldername . ''; Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798285 Share on other sites More sharing options...
Andy-H Posted April 1, 2009 Share Posted April 1, 2009 $foldername = "asdf"; echo '<a href="media/music/' . urlEncode($foldername) . '">' . stripSlashes(htmlEntities($foldername, ENT_QUOTES)) . '</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798288 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 Hehe good idea Andy-H Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798290 Share on other sites More sharing options...
Andy-H Posted April 1, 2009 Share Posted April 1, 2009 Thanks BTW, I was wondering if capitalising the characters in standard PHP functions make a script slower? Does it treat it as a seperate function, have to check the code for case-sensitivity; or are functions case-insensitive? Sorry to go off-topic but I like to use this method to make it more readable and have always pondered the above, thanks for any answers. Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798298 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 Does it treat it as a seperate function, have to check the code for case-sensitivity; or are functions case-insensitive? No functions in PHP are not case-sensitive. Being a Java developer, I tend to do the same thing. I believe this style of naming is referred to as CamelCase. Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798303 Share on other sites More sharing options...
Andy-H Posted April 1, 2009 Share Posted April 1, 2009 Thankyou Maq Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-798309 Share on other sites More sharing options...
JMair Posted April 2, 2009 Author Share Posted April 2, 2009 just a note, this works fine for names with no spaces, but if there is a space the output for this $foldername = "Pan Cakes"; echo '<a href="media/music/' . urlEncode($foldername) . '">' . stripSlashes(htmlEntities($foldername, ENT_QUOTES)) . '</a>'; is Pan+Cakes leading to a dead link. Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-799494 Share on other sites More sharing options...
premiso Posted April 2, 2009 Share Posted April 2, 2009 is Pan+Cakes leading to a dead link. Make your folder names have - or _ in them, so Pan-Cakes I take it you want to do this for SEO Reasons. That is the best way to do it. A space is not kosher as it breaks the structure to say and causes you to have to use %20 for it to work. Convert your folder names to have - or _ in them whereever there is a space, then you can simply use a str_replace on space characters and convert them to whichever character you have chosen. Quote Link to comment https://forums.phpfreaks.com/topic/152006-url-link-with-variable/#findComment-799500 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.