สมมติว่าต่อไปนี้คืออาร์เรย์ของเรา –
$marks=[45,67,89,34,98,57,77,30];
และเราต้องการผลลัพธ์แบบนี้ด้วยค่าที่เลือกเท่านั้น
45 67 89 68 98 57 77 60
ด้านบน เราคูณเครื่องหมายน้อยกว่า 40 ด้วย 2 ส่วนที่เหลือทำให้อาร์เรย์ทั้งหมดเหมือนกัน
ตัวอย่าง
รหัส PHP มีดังต่อไปนี้
<!DOCTYPE html>
<html>
<body>
<?php
$marks=[45,67,89,34,98,57,77,30];
echo "The Marks are as follows","<br>";
foreach($marks as $tempMarks){
if($tempMarks < 40){
$tempMarks=$tempMarks*2;
}
echo $tempMarks,"<br>";
}
?>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
The Marks are as follows 45 67 89 68 98 57 77 60