greenheart Posted October 3, 2009 Share Posted October 3, 2009 Hello, I would like to construct a url from two strings. Here is my attempt: echo "<a href=\"" .print($string). "\"><b>"; echo .print($string2). "</b></a>\n"; The above doesn't work, can anyone please tell me how to do it? Thanks very much. Link to comment https://forums.phpfreaks.com/topic/176415-constructing-a-url-from-two-strings/ Share on other sites More sharing options...
ProXy_ Posted October 3, 2009 Share Posted October 3, 2009 you could try this: echo "<a href='" . $string . "'><b>" . $string2 . "</b></a>\n"; give that a try, if this don't work reply with errors. Link to comment https://forums.phpfreaks.com/topic/176415-constructing-a-url-from-two-strings/#findComment-929850 Share on other sites More sharing options...
greenheart Posted October 3, 2009 Author Share Posted October 3, 2009 Tried it and the error was "Parse error: syntax error, unexpected T_ECHO" This is what I tried: <?php $link = www.example.com echo '<a href="' . $link. '"><b>' .$link. '\n'; } ?> Link to comment https://forums.phpfreaks.com/topic/176415-constructing-a-url-from-two-strings/#findComment-929856 Share on other sites More sharing options...
charlesg Posted October 3, 2009 Share Posted October 3, 2009 Try this, I know it looks different this what you are used to, but it is highly optimized and will work great. Notes: echo is slightly faster than print in PHP because echo does not return a value. The commas are an alternate way to write echo statements, which are faster to process. Single quotes are faster than double quotes for PHP to process, because it has to check for variables in double quotes. You don't have to escape single quotes used inside of double quotes, and you don't have to escape double quotes used inside of single quotes. echo "<a href='", $string, "'><b>", $string2, "</b></a>"; EDIT: You will want to use the following code instead to take advantage of the quote trick echo '<a href="', $string, '"><b>', $string2, '</b></a>'; Link to comment https://forums.phpfreaks.com/topic/176415-constructing-a-url-from-two-strings/#findComment-929857 Share on other sites More sharing options...
greenheart Posted October 3, 2009 Author Share Posted October 3, 2009 Tried this too with no luck (found some mistakes) <?php $link = www.example.com echo '<a href="' . $link. '"><b>' .$link. ,'</b></a>\n'; } ?> Link to comment https://forums.phpfreaks.com/topic/176415-constructing-a-url-from-two-strings/#findComment-929859 Share on other sites More sharing options...
jon23d Posted October 3, 2009 Share Posted October 3, 2009 <?php $link = www.example.com echo '<a href="' . $link. '"><b>' .$link. '\n'; } ?> $link = "www.example.com"; <-- quotes and terminator, that fixes your error. Try just: echo "<a href='$link'><b>$link\n"; You can just use variables in your strings as long as you use double quotes. Link to comment https://forums.phpfreaks.com/topic/176415-constructing-a-url-from-two-strings/#findComment-929861 Share on other sites More sharing options...
greenheart Posted October 3, 2009 Author Share Posted October 3, 2009 Thank you Jon that was silly of me (I am new to php and programming). It's all working now. Thank you all and thanks for the tip Charles. Link to comment https://forums.phpfreaks.com/topic/176415-constructing-a-url-from-two-strings/#findComment-929865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.