Unknown error type: [8] Undefined offset: 3

Hi, I am total newbie. my php code has the following lines to show links related to a current line. Each line has 2 or more (urls) from the database.

After adding more links to the DB, I added the last line of “$urls[3]”, but now when there is only to urls, it gives the error:
Unknown error type: [8] Undefined offset: 3

How can I add if, else - or something to these lines to show whatever available (2-3-4… etc.), without giving the error

$urls = explode("</br>", list($book, $chapter, $connection));
$urls[0] = str_replace("</a>", "<br>($book_abb)</a>", $urls[0]);
$urls[1] = str_replace("</a>", "<br>($book_abb)</a>", $urls[1]);
$urls[2] = str_replace("</a>", "<br>($book_abb)</a>", $urls[2]);
$urls[3] = str_replace("</a>", "<br>($book_abb)</a>", $urls[3]);
echo implode("<br/>", $urls);

Thanks,

The error says that $urls[3] does not exist!

You will have to check if each url exists and only display them if they are there.

To check if something exists, use something like if (isset($urls[1])) { echo $urls[1]; }
But, you might want to just do a loop instead of multiple IF’s.
If you want to do that, try foreach( $urls as $one_url) { handle $one_url… }

Thanks for the reply.

I tried to add if isset here but I think I put in wrong:

if (isset($urls[0])) = str_replace("", “($book_abb)”, $urls[0]);

Or how should I do foreach on the above example?

You are replacing text inside a variable. The odd code you are using can be replaced with one line.
You have URL’s, you create an array of them, you alter them, you compact them and print them.
Why not just right the replace directly. Just do this:

echo str_replace( "</a>", "<br>($book_abb)</a>", list($book, $chapter, $connection) );

This takes your list which is just TEXT and does the replacements and echos it. Much easier and faster!
( String processing is slower and converting to a text array and back into a string is not needed! )

Great… Thanks a lot.

Sponsor our Newsletter | Privacy Policy | Terms of Service