fungsi foreach pada php

Pada PHP 4 dan keatas, ada pernyataan foreach yang bisa juga digunakan untuk mengulang data atau melakukan perulangan. Pernyataan foreach ini melibatkan array, sehingga data di simpan di array dulu baru di ulang dengan menggunakan fungsi foreach ini. Bentuk pernyataan foreach adalah sebagai berikut :

foreach (ekxpresi_array as $nilai)

pernyataan

Pada bentuk ini, nilai yang ada pada array akan diberikan ke variabel nilai secara bergantian pada setiap iterasi


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>foreach | Free Tutorial Inkubis </title>
</head>

<body>
<?php
$tutorial = array ("tutorial css", "tutorial website","tutorial seo","tutorial photosop");
foreach($tutorial as $jenisTutorial)
echo "$jenisTutorial"."<br>";

?>
</body>
</html>

hasil

tutorial css

tutorial website

tutorial seo

tutorial photosop

Tags : , ,

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

2 Comments

Leave Comment