How to enable PHP mail function on Ubuntu

Here are steps to follow:

First of all install packagephp-pear if it’s already not installed. apt-get install php-pear
Then install following PEAR packages

  • pear install mail
  • pear install Net_SMTP
  • pear install Auth_SASL
  • pear install mail_mime

Then install POSTFIX

apt-get install postfix

This command will bring dialogue step by step to setup mail server.

Select Internet Site

and choose domain mail to be sent from

Try to send mail using mail() function, and you’ll receive the one you sent.

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. });

Difference between count() and sizeof() in PHP

difference between count and sizeof

According to PHP documentation, sizeof() is an alias of  count(). While searching on the web, I found and realized that count() is faster and butter than sizeof(). Here is a good description of this on stackoverflow– scroll down to the answer of Gazoris. to understand the difference between count() and sizeof() in php

According to my understanding when sizeof is an alias of count, its code might be looking something like this.

count() Function:

  1. function count($array_variable){
  2. //...........
  3. //code for counting size of array
  4. //...........
  5. return $size;
  6. }

sizeof() Function:

  1. function sizeof($array_variable){
  2. return count($array_variable);
  3. }

The above example code describes that when you are using sizeof() you are also calling the count() via backdoor. So, use of count is better.

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. ?>