- <?php
-
- /**
- * An array function to work with its individual value whatever we want.
- * @param array $array
- * @param function $function
- */
- function anonymous_test($array, $function){
- foreach ($array as $single){
- $function($single);
- }
- }
- ?>
- <?php
-
- /**
- * An array function to work with its individual value whatever we want.
- * @param array $array
- * @param function $function
- */
- function anonymous_test($array, $function){
- foreach ($array as $single){
- $function($single);
- }
- }
- ?>
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.
Pattern:
- <?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/>';
- }
-
- ?>
- <?php
- echo '<table border="1" cellpadding="0" cellspacing="0">';
- $black = true;
- for ($i = 1; $i <= 8; $i++) {
- echo "<tr>";
- for ($j = 1; $j <= 8; $j++) {
- if ($black == true)
- echo "<td style='background: #000;height: 30px;width: 30px;'> </td>";
- else
- echo "<td style='background: #fff;height: 30px;width: 30px;'> </td>";
- $black = !$black;
- }
- $black = !$black;
- echo "</tr>";
- }
- echo "</table>";
- ?>
Output:
- <?php
- function is_armstrong($digits)
- {
- $cube_digit = 0;
- foreach ($digits_arr as $digit) {
- $cube_digit += find_cube($digit);
- }
- if ($cube_digit == $digits)
- return true;
- else
- return false;
- }
-
- function find_cube($no)
- {
- return $no * $no * $no;
- }
-
-
- ?>
or
- <?php
-
- function is_armstrong($number)
- {
- $num = $number;
- $arms = 0;
- while ($number > 1) {
- $temp = $number % 10;
- $arms = $arms + ($temp * $temp * $temp);
- $number = $number / 10;
- }
- if ($num == $arms)
- return true;
- else
- return false;
- }
-
- ?>
- <?php
- for($i=1;$i<=5;$i++){
- for($j=1;$j<=5;$j++){
- if(($i==1 || $i==5) || ($j==1 || $j==5)){
- echo "<span style='color:#000;'>*</span>";
- }
- else{
- echo "<span style='color:#fff;'>*</span>";
- }
- }
- echo "<br/>";
- }
- ?>
Output:
*****
*****
*****
*****
*****