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

 

 

Binary Sorting Using PHP

CODE:

  1.  
  2. <?php
  3. $a = array();
  4. //get values to swap
  5. for($b=0;$b<10;$b++)
  6. {
  7.     $a[]=rand(99,999);
  8. }
  9. echo "<div style='float: left;margin-right: 20px;'>Before Sorting<br/>";
  10. for($b=0;$b<sizeof($a);$b++)
  11. {
  12.     echo $a[$b]."<br/>";
  13. }
  14. echo "</div>";
  15. //swap started
  16. for($i=0;$i<sizeof($a);$i++)
  17. {
  18.     for($j=0;$j<sizeof($a);$j++)
  19.     {
  20.         $swap="";
  21.         if($j<(sizeof($a)-1) && $a[$j]>$a[$j+1])
  22.         {
  23.             $swap=$a[$j];
  24.             $a[$j]=$a[$j+1];
  25.             $a[$j+1]=$swap;
  26.         }
  27.     }
  28. }
  29. echo "<div style='float: left;margin-right: 20px;'>After Sorting<br/>";
  30. for($b=0;$b<sizeof($a);$b++)
  31. {
  32.     echo $a[$b]."<br/>";
  33. }
  34. echo "</div>";
  35. echo "<div style='float: left;margin-right: 20px;'>In Descending Order<br/>";
  36. for($b=(sizeof($a)-1);$b>-1;$b--)
  37. {
  38.     echo $a[$b]."<br/>";
  39. }
  40. echo "</div>";
  41. ?>
  42.  

 

OUTPUT:

Before Sorting
831
507
410
677
970
838
748
705
206
978
After Sorting
206
410
507
677
705
748
831
838
970
978
In Descending Order
978
970
838
831
748
705
677
507
410
206

Too Simple LightBox PopUp Window

Hey guys, girls,

Here I’m going to teach you too simple lightbox popup window to see it before.

Add these lines into your body tag whenever you want. If may be you can arrange it according to your requirement. But make sure to be having position:fixed; and z-index:100 or 100+;

  1. <div id="divPopUp" style="border: solid 3px #408080;position: fixed;top: 5%;left: 20%;width: 60%;height: 70%;background: #002200;colr: #d9e3e8;z-index: 101;"></div>

And then add these JavaScript code to yor head tag

  1. function hidePopUp(){
  2. $('#divPopUp').html('');
  3. $('#divPopUp').hide();
  4. }
  5. $(document).ready(function(){
  6. $('#divPopUp').hide();
  7. $('.small_image').click(function(){
  8. $('#divPopUp').html('</p><div align="center">---------------Here Add Your Tag/Content whatever you want---------------<br/><span onclick="hidePopUp()" style="cursor: pointer">CLOSE</span></div>');
  9. $('#divPopUp').show();
  10. });
  11. });

Here is a complete example of code which I have used it for image gallery section:

  1. <html>
  2. <head>
  3. <script>
  4. function hidePopUp(){
  5. $('#divPopUp').html('');
  6. $('#divPopUp').hide();
  7. }
  8. $(document).ready(function(){
  9. $('#divPopUp').hide();
  10. $('.small_image').click(function(){
  11. var largeImageSrc=$(this).attr('src');
  12. largeImageSrc=largeImageSrc.replace('small','big');
  13. $('#divPopUp').html('<div align="center"><img src="'+largeImageSrc+'"/><br/><span onclick="hidePopUp()" id="btnClose" style="cursor: pointer">CLOSE</span></div>');
  14. $('#divPopUp').show();
  15. });
  16. });
  17. </script>
  18. </head>
  19. <body>
  20. <?php
  21. echo '<ul class="gallerylist">';
  22. while($row=mysql_fetch_array($result))
  23. {
  24. echo '<li><div align="center" style="">'.$row['image_name'].'</div><img class="small_image" style="cursor:pointer;" src="admin/gallery_image/small/'.$row['image_id'].'.'.$row['image_ext'].'"/></li>';
  25. }
  26. echo '</ul>';
  27. ?>
  28. <div id="divPopUp" style="border: solid 3px #408080;position: fixed;top: 5%;left: 20%;width: 60%;height: 70%;background: #002200;color: #d9e3e8;z-index: 101;"></div>
  29. </body>
  30. </html>

 

See the Pen Light Box by AHMAD ASJAD (@ahmadasjad) on CodePen.0

Happy coding!!!!!!!