Home  >  Article  >  Backend Development  >  ci - Is there a problem with the CodeIgniter framework Router.php source code?

ci - Is there a problem with the CodeIgniter framework Router.php source code?

WBOY
WBOYOriginal
2016-12-05 13:44:25935browse

In the systemcoreRouter.php file of the ci framework, the starting code of line 132 is as follows:

    is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']);
    $this->_set_routing();

    // Set any routing overrides that may exist in the main index file
    if (is_array($routing))
    {
        empty($routing['controller']) OR $this->set_class($routing['controller']);
        empty($routing['function'])   OR $this->set_method($routing['function']);
    }

First of all, the first line:
is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']);
I'm not sure if it works like this Meaning, if no judgment is added to the result (true or false) of such a line of expression, what is the purpose of the result (true or false)?
In addition, in the if judgment statement, empty is used to judge the two results. I also think there is no Meaning, if you don’t want to make a judgment, do you have any thoughts on writing it like this?
Please give me some advice.

Reply content:

In the systemcoreRouter.php file of the ci framework, the starting code of line 132 is as follows:

    is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']);
    $this->_set_routing();

    // Set any routing overrides that may exist in the main index file
    if (is_array($routing))
    {
        empty($routing['controller']) OR $this->set_class($routing['controller']);
        empty($routing['function'])   OR $this->set_method($routing['function']);
    }

First of all, the first line:
is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']);
I'm not sure if it works like this Meaning, if no judgment is added to the result (true or false) of such a line of expression, what is the purpose of the result (true or false)?
In addition, in the if judgment statement, empty is used to judge the two results. I also think there is no Meaning, if you don’t want to make a judgment, do you have any thoughts on writing it like this?
Please give me some advice.

is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']);

Do you know that the symbol

&& is true + true?
In other words, the first and the second must be executed successfully before the third one will be executed. All three must be executed successfully, otherwise it will not be executed. We can change it to another way of writing:

if (is_array($routhing) && isset($routing['directory'])) {
    $this->set_directory($routing['directory']);
}

Is your thinking very clear?

Why do you do this? && Do this?

1、因为不涉及复杂的语句
2、简洁
3、直观、可读性强

// && vs ||
!isset($a) && $a=1;
echo $a;//1
isset($a) || $a =2;
echo $a;//1
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn