B
B
Bodya_idiot2020-05-05 18:11:28
PHP
Bodya_idiot, 2020-05-05 18:11:28

Why does return not return the value of a variable?

In short, I wrote a function that, using parsing, determines how many pages with restaurants are on the site, and the function works properly, I checked every iteration of it and everything works, but as soon as it comes to IF and it is executed, it costs return $ max, but when it does not return the value of the variable, please help, I have been working on this function for the 2nd day

<?php
$page = 0;

function GetMaxPage($page) // C помощью этой фунции я узнаю какая макс. страничка на сайте с ресторанами
{
    $subject = file_get_contents('https://restoran.kz/restaurant?page=' . $page);
    $pattern = '/<a.+?href="(\/restaurant\?page=[0-9]+){0,1}(.{0,10}){0,1}">([0-9]+)<\/a>/u';
    $result = [];
    preg_match_all($pattern, $subject, $result);

    $max = max($result[3]); // Присваиваю макс значение с массива result
    if ($page == $max) {
        return $max;  // Вот тут и проблема, он не возвращает значение, а если там написать echo то он исправно выводит
    } else {
        $page = $max;
        GetMaxPage($page); 
    }
}
$MaxPage = GetMaxPage($page);
echo $MaxPage;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-05-05
@Bodya_idiot

Because you have only 1 return, for the second condition you also need to output:

if ($page == $max) {
    return $max;
} else {
    $page = $max;
    return GetMaxPage($page); // тут добавлен return
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question