Function Declaration vs Function Expression

You guys several times will have seen the function in two syntax.

 

    1)Function Declaration

  1. function func_name(arg_1, arg_2,..., arg_n){
  2.     .............
  3.     ...........
  4. }

    2)Function Expression

  1. var func_name;
  2. func_name=function(){
  3.     ....................
  4.     .............
  5. };
  6.  

 

Here are the differences

  • Function expression can’t be called before its definition vs Function declaration can.

 

 

 

Find no of time a character used in string using PHP

  1. function char_in_string($string){
  2. $char_array=str_split($string);
  3. for($i=0;$i<sizeof($char_array);$i++){
  4. $no_of_char[$char_array[$i]]=0;
  5. for($j=0;$j<sizeof($char_array);$j++){
  6. if($char_array[$i]==$char_array[$j])
  7. {
  8. $no_of_char[$char_array[$i]]++;
  9. }
  10. }
  11. }
  12. return $no_of_char;
  13. }

 

Now show its structure using var_dump()

var_dump(char_in_string("Hello Ahmad Asjad"));

Its result will look something like this.

array (size=12)
  'H' => int 1
  'e' => int 1
  'l' => int 2
  'o' => int 1
  ' ' => int 2
  'A' => int 2
  'h' => int 1
  'm' => int 1
  'a' => int 2
  'd' => int 2
  's' => int 1
  'j' => int 1