I want to get the result from a foreach loop, but use it outside of the foreach brackets. What is the easiest way to do this.
Currently the below is working fine:
foreach($esmc->find('p') as $paragraph){
$showparag = $paragraph->innertext. '<br/>';
echo $showparag;//Will show result of array
}
However, I would like to have this so I can add text to it (echo is outside of foreach brackets):
foreach($esmc->find('p') as $paragraph){
$showparag = $paragraph->innertext. '<br/>';
}
echo "This shows results of array as $showparag";//Contains text + array
Currently if I do the second example, it is only returning the final record in the array.
Any help would be greatly appreciated.
$string = 'This shows results of array as ';
foreach($esmc->find('p') as $paragraph){
$showparag = $paragraph->innertext. '<br/>';
$string .= $showparag;
}
echo $string;
http://php.net/manual/en/language.variables.scope.php
P.S. Despite there is a lot examples of global variables usage in the manual, you actually should not use it as it is considered as a bad practice.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다