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

How efficient is PHP in introducing functions in foreach?

WBOY
Release: 2016-10-17 09:30:02
Original
878 people have browsed it

For example, I run a foreach loop:

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

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>
Copy after login
Copy after login

Execute again:

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

Reply content:

For example, I run a foreach loop:

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

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>
Copy after login
Copy after login

Execute again:

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template