PHPize Online / SQLize Online  /  SQLtest Online

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