Membuat Pola Segitiga Piramid dengan Script PHP

richard maulana
Sep 5, 2018 · 1 min read

Berikut ini adalah script php membuat pola segitiga piramid menggunakan fungsi Looping:

<form action=" " method="GET">
Tinggi:<input type="text" name="nilai">
<input type="submit" name="submit" value="tampil">
</form>

<?php
if(isset($_GET['submit'])){
$Tinggi = $_GET['nilai'];

for ($i=1;$i<=$Tinggi;$i++) {
for($j=1;$j<($Tinggi*2);$j++){
if ($i==1 && $j==$Tinggi) {
echo "*";
}
elseif($i>1&&($j==($Tinggi-$i)+1)|| ($j==($Tinggi+$i)-1)){
echo "*";
}else
echo "&nbsp;";

}
echo "<br>";
}
}
?>

Dimana Outpunya seperti gambar dibawah ini :

Saat kita menginputkan angka 5 maka tinggi dari segitiga tersebut sebanyak 5 bintang
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade