P
P
Pavel2020-04-29 13:49:29
PHP
Pavel, 2020-04-29 13:49:29

How to rewrite the code in a more simple and understandable form?

there is this snippet of code

foreach ($result as $i) {
                    if ($i['name'] == $name && $i['date'] == $day && print("<td>{$i['sub_hours']}</td>"))
                        continue 2;
                }

I don’t understand how to rewrite the code inside the if so that it is written “as usual”))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ildar Saribzhanov, 2020-04-29
@mrusklon

Well, let's say we rewrite it like this.

foreach ($any as $result) {
    foreach ($result as $i) {
        if (isCheck($i, $name, $day)) {
            print("<td>{$i['sub_hours']}</td>");

            continue 2;
        }
    }
}

/**
 * Проверка условия 
 * 
 * @param $itm
 * @param $name
 * @param $day
 *
 * @return bool
 */
function isCheck($itm, $name, $day)
{
    if ($itm['name'] != $name) {
        return false;
    }

    return ($itm['date'] == $day);
}

D
Dmitry Gordinskiy, 2020-04-29
@DmitriyGordinskiy

How to rewrite the code in a more simple and understandable form?

Do not use output functions or state-changing functions inside a condition expression.
And even more so, do not do it in conditions with 'lazy' logical operators.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question