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.

 

 

 

Array Sorting With Single Loop

There is an unsorted array of integers. write the complete program to find the second largest no in the array without using sorting and max functions. You can only iterate(loop) over the array once.

 

 

  1. <?php
  2. echo "<br/>";
  3. $arr=array(12,52,2,35,95,17,37,42);
  4. echo "Before Sorting<br/>";
  5. print_r($arr);
  6. $tot_arr=count($arr);
  7. for($i=0;$i<$tot_arr;$i++){
  8. if(($i< ($tot_arr-1)) && $arr[$i]>$arr[$i+1]){
  9. $temp=$arr[$i];
  10. $arr[$i]=$arr[$i+1];
  11. $arr[$i+1]=$temp;
  12. $i=-1;
  13. }
  14. }
  15. echo "<br/>After Sorting<br/>";
  16. print_r($arr);
  17. ?>

 

 

Output:

 

Before Sorting
Array ( [0] => 12 [1] => 52 [2] => 2 [3] => 35 [4] => 95 [5] => 17 [6] => 37 [7] => 42 ) 
After Sorting
Array ( [0] => 2 [1] => 12 [2] => 17 [3] => 35 [4] => 37 [5] => 42 [6] => 52 [7] => 95 )

 

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

 

 

HTML Boolean Attributes

The presence of boolean attribute on html element represents it true and absence of boolean attribute on any html element represents it false.

If the attribute is present in any html element, its value must be empty or the name of the attribute in value of attribute like below:

Possible Correct Syntax:

 

  1. <tag_name attribute_name="attribute_name"></tag_name>
  2. <tag_name attribute_name="attribute_name" />
  3. <tag_name attribute_name=""></tag_name>
  4. <tag_name attribute_name="" />
  5. <tag_name attribute_name></tag_name>
  6. <tag_name attribute_name />

 

Possible Wrong Syntax:

  1. <tag_name attribute_name="true"></tag_name>
  2. <tag_name attribute_name="false"></tag_name>
  3. <tag_name attribute_name="true" />
  4. <tag_name attribute_name="false" />

 

The values “true” and “false” are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

Here are the some of boolean attribute of HTML elment.

  • seamless
  • checked
  • hidden
  • disabled
  • required

Difference between “WHERE” and ”HAVING” clause

where clause can not accept aggregate function like sum, count, etc. whereas having clause can accept the aggregate functions like sum, count, average, etc.

Let’s see it by example.

emp_salary_paid
paid_id emp_id  salary_given salary_month
1 1 1000 jan
2 2 1500 jan
3 3 2000 jan
4 1 1200 feb
5 2 1500 feb
6 3 1800 feb

 

emp_detail
emp_id emp_name
1 Sajid
2 John
3 Ahmad

 

Query:

  1. SELECT emp_detail.emp_name, emp_salary_paid.salary_month, emp_salary_paid.salary_given
  2. FROM emp_detail, emp_salary_paid
  3. WHERE emp_detail.emp_id=emp_salary_paid.emp_id

 

will produce result like this one. because no aggregate function is being used here.

emp_name salary_month salary_given
Sajid jan 1000
Sajid feb 1200
John jan 1500
John feb 1500
Ahmad jan 2000
Ahmad feb 1800
  1. SELECT emp_detail.emp_name, SUM(emp_salary_paid.salary_given) AS total_paid
  2. FROM emp_detail, emp_salary_paid
  3. GROUP BY emp_detail.emp_id
  4. WHERE SUM(emp_salary_paid.salary_given) >2000

This will produce an error, because aggregate function is being used with where clause. To overcome this, we use having clause like this one.

  1. SELECT emp_detail.emp_name, SUM(emp_salary_paid.salary_given)
  2. FROM emp_detail, emp_salary_paid
  3. GROUP BY emp_detail.emp_id
  4. HAVING SUM(emp_salary_paid.salary_given) > 2000

This will produce the correct result as follows:

emp_name total_paid
Sajid 9000
John 9000
Ahmad 9000