K
K
kimqar2022021-08-10 09:14:31
PHP
kimqar202, 2021-08-10 09:14:31

Php operator substr and assignment return empty variable?

There is a cycle

foreach($array_pages as $v)
{
    $quantity_elements++;
    if (!empty($v['URL'])) {
        $vUrl = substr($v['URL'], -1);
        if ($vUrl != '/') {
            $v['URL'] = $v['URL'] + '/';
            print_r($v['URL']);
        }
    }
}

at the end, the $v['URL'] variable returns the / sign.
Initially, the $v['URL'] variable contains a link (for example, www.site.ru/news)
Next comes the condition where it is checked whether there is a slash at the end, and if so, it should return a link with a / at the end, but it returns just the / sign, as if $v['URL'] is empty. Where is the mistake?

An example of how it should display if there is no sign at the end /
www.site.ru/news/
If there is a sign / at the end of the link, then the condition does not work and goes further

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2021-08-10
@kimqar202

In php, the concatenation operation is ., not +.

V
Vlad Saenko, 2021-08-10
@Domovik93

The answer is already there, but for the future to the author, wardump variables and there will be fewer questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question