JCBiggar
JCBiggar

Reputation: 2497

Can I use a PHP string Multiple Times

I was wondering if I could using a string multiple times in my php to do something like this:

$image = "Image One Here";
$title = "Title One Here";
$link = "Link One Here";

$image = "Image Two Here";
$title = "Title Two Here";
$link = "Link Two Here";

$image = "Image Three Here";
$title = "Title Three Here";
$link = "Link Three Here";


foreach(condition here) {
 echo $image;
 echo '<br/>';
 echo $title;
 echo '<br/>';
 echo $link;
}

Any idea on how to accomplish this? Thanks in advance!

Upvotes: 0

Views: 47

Answers (4)

user3462511
user3462511

Reputation:

This way you can do.

$image[] = array('Image one here','Image two here');
$title[] = array("Title One Here","Title Two Here");
$link[] = array("Link One Here","Link Two Here");

foreach($image as $value)
{
    echo $image[$value];
    echo $title[$value];
    echo $link[$value];
}

Upvotes: 3

saman khademi
saman khademi

Reputation: 842

try using array

$image=array('One','Two','Three');
$link=array('One','Two','Three');
$title=array('One','Two','Three');
foreach($title as $key)
{
    echo $image[$key].'<br/>';
    echo $link[$key].'<br/>';
    echo $title[$key].'<br/>';
}

Simple and short

Upvotes: 1

CMPS
CMPS

Reputation: 7769

Here's an example with for and foreach

$images = array('image1','image2','image3');
$titles = array('title','title2','title3');
$links = array('link1','link2','link3');

for($i=0; $i<count($images);$i++){
echo $images[i]. "<br/>" . $titles[i] . "<br/>" . $links[i]."</br>";
}

or with the foreach()

foreach($images as $key => $value){
echo $images[$key]. "<br/>" . $titles[$key] . "<br/>" . $links[$key]."</br>";
}

Upvotes: 1

Krish R
Krish R

Reputation: 22721

Hope it helps,

    $image[] = "Image One Here";
    $title[] = "Title One Here";
    $link[] = "Link One Here";

    $image[] = "Image Two Here";
    $title[] = "Title Two Here";
    $link[] = "Link Two Here";

    $image[] = "Image Three Here";
    $title[] = "Title Three Here";
    $link[] = "Link Three Here";


    foreach($image as $key=>$img) {
        echo $img;
        echo '<br/>';
        echo $title[$key];
        echo '<br/>';
        echo $link[$key];
    }

Upvotes: 1

Related Questions