Connor
Connor

Reputation: 3

Concatenate parts of a URL in PHP

echo '<img src= "http://websitename.com/a".$r[0].".png">';

I have a random number generator for the purpose of picking an image randomly. The code above just uses "http://websitename.com/a" but I want it to concatenate the randomly generated number and the .png on the end. Any idea how I should make this work? Or is there a better way of doing this? Thanks, this is the first thing I've done in php so please don't murder me.

Upvotes: 0

Views: 67

Answers (2)

Button 108
Button 108

Reputation: 359

Rogue called it on the Misquote with the . operator.

I've become a big fan of using double quotes for strings with variables and then curly brackets for the variable... If double quotes are an option they're very user friendly and will save you a lot of time when you get used to it.

echo "<img src= 'http://websitename.com/a{$r[0]}.png' />";

Upvotes: 2

RogueBaneling
RogueBaneling

Reputation: 4471

echo '<img src= "http://websitename.com/a'.$r[0].'.png">';

In PHP you can concatenate strings with the . operator. Docs for reference.

Upvotes: 0

Related Questions