Home  >  Article  >  Backend Development  >  How efficient is PHP in introducing functions in foreach?

How efficient is PHP in introducing functions in foreach?

WBOY
WBOYOriginal
2016-10-17 09:30:02847browse

For example, I run a foreach loop:

<code>foreach(func() as $row){
echo $row;
}
</code>

At this time, does func() run once or multiple times?
Do you need to use it? As mentioned on the Internet, in order to optimize efficiency, define it first:

<code>$arr=func();</code>

Execute again:

<code>foreach($arr as $row){
    echo $row;
}</code>

Reply content:

For example, I run a foreach loop:

<code>foreach(func() as $row){
echo $row;
}
</code>

At this time, does func() run once or multiple times?
Do you need to use it? As mentioned on the Internet, in order to optimize efficiency, define it first:

<code>$arr=func();</code>

Execute again:

<code>foreach($arr as $row){
    echo $row;
}</code>

According to the explanation, it should be executed once, but in order to understand the code better, it is best to find the value first and then foreach

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