note 97252 rejected from function.exit by degeberg
| From: | degeberg@php.net | Date: | Sun, 11 Apr 2010 09:56:07 +0000 |
| Subject: | note 97252 rejected from function.exit by degeberg | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168120@lists.php.net to get a copy of this message | ||
Note Submitter: dev at 10e12 dot net
----
A curious note on the use of exit in PHP
While working on three different servers simultaneously I have noted an oddity coming from a regular
application dev background.
Excuse the pseudo examples
<?php
myfunction(){
$res=0;
foreach{$testArray As $key => $value){
$res = $value;
if($res==1){exit(1);}
}
return $res;
}
?>
Aught to be equivalent in it's execution to...
<?php
myfunction(){
$res=0;
foreach{$testArray As $key => $value){
if($value==1){return $value;}
}
//if you get here you didn't find the res value other than 0
return $res;
}
?>
Aught to be equivalent in it's execution to...
(This is the ONLY option that works as intended
in all my tests which means massive needless looping.)
<?php
myfunction(){
$res=0;
foreach{$testArray As $key => $value){
if($value==1){res = $value;}
}
//loop on until you get here
return $res;
}
?>
...however it is NOT.
In two out of three servers the use of Exit() acts as a Super Global and kills the whole site.
On some occasions the exit will have no effect at all and in others still... well hmmm needless to
say, this seems like randomized behavior somehow.
I didn't see any difference between PHP 5.2.6 and PHP 5.2.8 in this.