Jackymamouth
Jackymamouth

Reputation: 149

Inserting variable in echo function

I have have some php code that I can't get to work ... I can't seem to find my mistake :/, I realize it's going to be a syntax error but after looking for an hour with no success I turn to you guys for help =)

Here is the code:

<?php 
    $t= number_latest_added();
    for ($n = 0; $n<$t; $n += 3) {
        $latest = latest($n);
        echo "<a class=\"example-image-link\" href=\"" .$latest. "\" data-lightbox=\"example-set\" data-title=\"De la galerie : " . $latest . "\"><div id=\"a\" style=\"background: url(" . $latest . ") 50% 50% / cover;background-size: contain;background-repeat: no-repeat;\"></div></a></br>";
    } 
?>

The problem is in getting the echo to concatenate with the variable but it isn't working ( the variable is echoed 3 times and then the text is echoed with blank instead of the variable) and I don't understand why not ... If somebody could help me see my error; it would be great!

Upvotes: 2

Views: 82

Answers (2)

Olavi Sau
Olavi Sau

Reputation: 1649

Make sure you turn on error reporting ini_set('display_errors', 'On');

In addition to that, php automatically parses variables to string, if your inside double quotes.

Example:

$var = "hello"
echo "$var world" //prints hello world

Your problem is that latest($n) doesn't return anything printable, you can find that out by simply echo'ing the value itself.

Upvotes: 0

dave
dave

Reputation: 64687

What is the function latest? From what you are saying I am guessing it is something like:

function latest($number) {
    echo $number;
}

and you need something like:

function latest($number) {
    return $number;
}

Upvotes: 4

Related Questions