Joshua Tall Guy
Joshua Tall Guy

Reputation: 227

Appending a string to the end of a URL in a variable in PHP

So I have this code for a youtube link in a wordpress widget

 $title  = "<h5 class='widget_title sidebar_widget_title'><a
 href='http://www.youtube.com/user/".$options['username']."'
 target='_blank'>".$options['title']."</a></h5>";

So I'm trying to append

?sub_confirmation=1

after my youtube user name so that a subscribe confirmation comes up but wow I have tried everything and am just not a good enough coder yet.

Upvotes: 0

Views: 909

Answers (1)

Leonard
Leonard

Reputation: 3092

You just insert the query string parameter to the string.

$title  = "<h5 class='widget_title sidebar_widget_title'><a href='http://www.youtube.com/user/".$options['username']."?sub_confirmation=1' target='_blank'>".$options['title']."</a></h5>";

The reason this is possibly confusing to you is because the author of the code you're changing used " to tell PHP that the next sequence of characters form a string, and ' to tell HTML that this is the value for the href attribute (like <a href='youtube.com'>...</a>). This works because HTML supports both " and ', and it saves you the hassle of having to escape quotes.

Personally, I'd rather use ' for PHP-strings and regular " for HTML, but that's a matter of taste.

Upvotes: 1

Related Questions