
Pascal Triangle using PHP
We often hear about the pascal triangle while we go for the interview or when we start learning any programming language. Here is a good example of pascal triangle using php language.
- <?php
-
- $lines = 5;
- $star_times = 1;
- $blank_times = $lines - $star_times;
- $pyramid_char= '<span style="color:#000;">*</span>';
- $outer_char ='<span style="color:#fff;">*</span>';
-
- for ($i = 1; $i <= $lines; $i++) {
- //Write blank line or whatever you like outside(before) the pyramid shape
- for ($blank_before = 0; $blank_before < $blank_times; $blank_before++) {
- echo $outer_char;
- }
- //Write the character to shape a pyramid
- for ($star = 1; $star <= $star_times; $star++) {
- echo $pyramid_char;
- }
- //Write blank line or whatever you like outside(after) the pyramid shape
- for ($blank_after = 0; $blank_after < $blank_times; $blank_after++) {
- echo $outer_char;
- }
- //increase the star and decrease the space
- $star_times = $star_times + 2;
- $blank_times = $blank_times -1;
- echo '<br/>';
- }
-
- ?>