PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $prices = [7,6,4,3,1]; $buyingPrice = 0; $soldingPrice = 0; $buyedDay = 0; if (count($prices) < 2) { echo "0"; } foreach ($prices as $index => $price) { switch ($index) { case 0: $buyingPrice = $price; break; case 1: $soldingPrice = $price; break; } echo $index . ' '; echo $price . ' '; echo $buyedDay . ' '; echo $buyingPrice . ' '; echo $soldingPrice . ' '; echo PHP_EOL; if ($buyingPrice > $price) { $buyingPrice = $price; $buyedDay = $index; } if (($index > $buyedDay) && (($price > $buyingPrice) && ($price > $soldingPrice))) { $soldingPrice = $price; } } if ( ($soldingPrice > $buyingPrice) && (($soldingPrice - $buyingPrice) > 0) ) { echo ($soldingPrice - $buyingPrice); } else { echo 0; }
Show:  
Copy Clear