Anonymous function example in PHP

 

  1. <?php
  2.  
  3. /**
  4. * An array function to work with its individual value whatever we want.
  5. * @param array $array
  6. * @param function $function
  7. */
  8. function anonymous_test($array, $function){
  9. foreach ($array as $single){
  10. $function($single);
  11. }
  12. }
  13. ?>

 

 

  1. <?php
  2. /**
  3. * Here we are writing a function which will print only if it's printable.
  4. */
  5. $arr = [1,2,3,['A','B'],4,5];
  6. anonymous_test($arr, function($value){
  7. if(!is_array($value) && !is_object($value)){
  8. echo $value;
  9. }
  10. });

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.

Pattern:

pascal triangle


  1. <?php
  2.  
  3. $lines = 5;
  4. $star_times = 1;
  5. $blank_times = $lines - $star_times;
  6. $pyramid_char= '<span style="color:#000;">*</span>';
  7. $outer_char ='<span style="color:#fff;">*</span>';
  8.  
  9. for ($i = 1; $i <= $lines; $i++) {
  10. //Write blank line or whatever you like outside(before) the pyramid shape
  11. for ($blank_before = 0; $blank_before < $blank_times; $blank_before++) {
  12. echo $outer_char;
  13. }
  14. //Write the character to shape a pyramid
  15. for ($star = 1; $star <= $star_times; $star++) {
  16. echo $pyramid_char;
  17. }
  18. //Write blank line or whatever you like outside(after) the pyramid shape
  19. for ($blank_after = 0; $blank_after < $blank_times; $blank_after++) {
  20. echo $outer_char;
  21. }
  22. //increase the star and decrease the space
  23. $star_times = $star_times + 2;
  24. $blank_times = $blank_times -1;
  25. echo '<br/>';
  26. }
  27.  
  28. ?>

Make a chess using PHP

  1. <?php
  2. echo '<table border="1" cellpadding="0" cellspacing="0">';
  3. $black = true;
  4. for ($i = 1; $i <= 8; $i++) {
  5. echo "<tr>";
  6. for ($j = 1; $j <= 8; $j++) {
  7. if ($black == true)
  8. echo "<td style='background: #000;height: 30px;width: 30px;'>&nbsp;</td>";
  9. else
  10. echo "<td style='background: #fff;height: 30px;width: 30px;'>&nbsp;</td>";
  11. $black = !$black;
  12. }
  13. $black = !$black;
  14. echo "</tr>";
  15. }
  16. echo "</table>";
  17. ?>

Output:

chess

 

Find Armstrong Number in PHP

  1. <?php
  2. function is_armstrong($digits)
  3. {
  4. $digits_arr = str_split($digits);
  5. $cube_digit = 0;
  6. foreach ($digits_arr as $digit) {
  7. $cube_digit += find_cube($digit);
  8. }
  9. if ($cube_digit == $digits)
  10. return true;
  11. else
  12. return false;
  13. }
  14.  
  15. function find_cube($no)
  16. {
  17. return $no * $no * $no;
  18. }
  19.  
  20. var_dump(is_armstrong(373));
  21.  
  22. ?>

or

  1. <?php
  2.  
  3. function is_armstrong($number)
  4. {
  5. $num = $number;
  6. $arms = 0;
  7. while ($number > 1) {
  8. $temp = $number % 10;
  9. $arms = $arms + ($temp * $temp * $temp);
  10. $number = $number / 10;
  11. }
  12. if ($num == $arms)
  13. return true;
  14. else
  15. return false;
  16. }
  17.  
  18. var_dump(is_armstrong(163));
  19. ?>