3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $max = 50; //Fonction qui retourne les multiples d'un diviseur <= max //Logique de calculs $memoryF = memory_get_usage(); function multiplesDe($diviseur, $max) { $result = array(); for($i=0; $i<=$max; $i+=$diviseur) { $result[] = $i; //L'ensemble des résultats dont chargés en mémoire par la fonction } return $result; } //Logique d'affichage //var_dump(multiplesDe(7,$max)); $multiplesDeSept = multiplesDe(7,$max); //Ici se trouve le gros stockage mémoire! foreach(multiplesDe(7,$max) as $i => $multiples) { echo "$i => $multiples<br />"; } echo memory_get_usage()-$memoryF,"<br />"; //var_dump($multiplesDeSept); //On implémente la classe PHP5 Iterator /* http://fr2.php.net/manual/fr/class.iterator.php abstract public mixed current ( void ) OK abstract public scalar key ( void ) OK abstract public void next ( void ) OK abstract public void rewind ( void ) OK (implémentée) abstract public boolean valid ( void ) OK */ $memoryI = memory_get_usage(); class MultiplesIterator implements Iterator { protected $diviseur,$max; //parametres calculs protected $current, $indiceCurrent; public function __construct($diviseur, $max) { $this->diviseur = $diviseur; $this->max = $max; } public function rewind() { // On revient au début! $this->current = 0; $this->indiceCurrent = 0; } public function valid() { // Condition de fin de boucle! return ($this->current <= $this->max); } public function current() { // retourne la valeur courante ! return $this->current; } public function key() { // retourne l'indice de la velru courante(la clé!) return $this->indiceCurrent; } public function next() { // fait passer au prochain élément! $this->indiceCurrent += 1; $this->current += $this->diviseur; } } $iterator = new MultiplesIterator(7,$max); foreach($iterator as $i => $multiples) { echo "$i => $multiples<br />"; } //Stockage de l'ensemble des résultats dans une liste > peu gourmand. echo memory_get_usage()-$memoryI,"<br />"; $memoryG = memory_get_usage(); // on implémente la classe Generator! // Yield: Lorsque yield est atteint, la valeur qu’il spécifie est renvoyée à l’appelant, et l’exécution de la fonction est mise en pause // jusqu’à ce que l’appelant demande à ce qu’elle soit continuée. function multiplesDeG($diviseur, $max) { for($i=0,$n=0; $n<=$max; $i++,$n+=$diviseur) { yield $i => $n; //On renvoit directement la valeur à l'appelant: } } $listeMultiples = multiplesDeG(7, $max); foreach($listeMultiples as $i => $multiples) //foreach(multiplesDeG(7, $max) as $i => $multiples) fonctionne aussi! { echo "$i => $multiples<br />"; } echo memory_get_usage()-$memoryG; var_dump(multiplesDeG(7, $max) instanceof Generator);
Output for 7.0.0 - 7.1.0
0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />408<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />128<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />384bool(true)
Output for hhvm-3.10.0 - 3.12.0
0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />0<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />0<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />0bool(true)
Output for 5.6.0 - 5.6.28
0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />1872<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />816<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />408bool(true)
Output for 5.5.0 - 5.5.38
0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />1840<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />816<br />0 => 0<br />1 => 7<br />2 => 14<br />3 => 21<br />4 => 28<br />5 => 35<br />6 => 42<br />7 => 49<br />408bool(true)
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$i' (T_VARIABLE) in /in/Ocq9Q on line 88
Process exited with code 255.
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/Ocq9Q on line 88
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_VARIABLE in /in/Ocq9Q on line 88
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/Ocq9Q on line 38
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/Ocq9Q on line 38
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/Ocq9Q on line 38
Process exited with code 255.