Advertisement
sujonshekh

PHP Basics

Nov 8th, 2020
2,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1.  
  2. <?php
  3. //Assignment one
  4. //problem no one
  5. // variable means container puts data temporay wise it's will be constant also when changed !
  6. $fname = "Sujon";
  7. $lname ="Shekh";
  8.  
  9. echo "My name first name is {$fname} and my last name is {$lname}\n";
  10. printf ("My name first name is %s and my last name is %s\n",strtoupper($fname),strtoupper($lname));
  11. printf ('My name first name is %2$s and my last name is %1$s',$fname,$lname); //$fname is Arguments!!
  12. // printf can changed variable value and it's user friendly;
  13. echo "\n";
  14.  
  15. //problem no two
  16.  
  17. $birthYears = 1993;
  18. $recentYears = 2020;
  19. $birthYears2 = $recentYears - $birthYears; // this operation called operand;
  20. //number += 1 (+=Assingment operator); means number = number + 1 ;
  21. echo "My Age is {$birthYears2} years old";
  22.  
  23. echo "\n";
  24.  
  25. //Problem no three
  26.  
  27. $d = 51;
  28. $o =051;
  29. $h = 0X5D;
  30.  
  31. printf ("Various Number System is %d and %d also %d\n",$d,$o,$h);
  32. printf ("The Binary equivalent is %d to %b\n",$d,51);
  33. printf ('The Binary equivalent is %1$d to %1$b',51); //place holder!
  34. echo "\n";
  35.  
  36.  
  37. //Problem no four
  38. $product = 123.500;
  39. $product2 = 63.213;
  40. printf("Price is %07.2f,\n",$product);
  41. printf("Price is %07.2f,\n",$product2);
  42.  
  43.  
  44. //Problem no five
  45. $food = "apple";
  46. if ("Tuna" == $food || "salmon fish" == $food ||"shark" == $food ){ // "Tuna" == $food is scalar value;
  47.     echo "{$food} is Vitamine 'D'";
  48. }elseif("apple"== $food || "mango"== $food || "pineaple"==$food){
  49.     echo "{$food} is Fruit";
  50. }else{
  51.     echo "its not avaiable in our store Sorry !!!!!!!";
  52. }
  53. echo "\n";
  54.  
  55. //problem no six
  56.  
  57. $year = 1900;
  58.  
  59. if ($year % 4 ==0 &&($year % 100 !=0 || $year % 400 ==0))//it's called Nested ! //if (true && true) can print if any where false it's turn over false;
  60.     //if (false || true) can print if any where false it's turn over true;
  61.  
  62.    {
  63.     echo "$year Leap Year";
  64. }else{
  65.     echo "$year not Leap Year";
  66. }
  67.  
  68. echo "\n";
  69.  
  70. // problem no seven
  71.  
  72. $condition1 = false;
  73. $condition2 = true;
  74. $condition3 = false;
  75.  if ($condition1 && $condition2 && $condition3){
  76.     echo "Hello Sujon Shekh";
  77.  }elseif ($condition1 && $condition2){
  78.      echo "no 1";
  79.  }elseif ($condition1){
  80.      echo "no 2";
  81.  }else {
  82.     echo  "no 3";
  83.  }
  84. echo "\n";
  85.  
  86.  
  87.  
  88. // problem number Eight
  89.  
  90. $numb = 10 ;
  91.  
  92. $numberCheck = (12 == $numb) ? "Twelve" : ((10 == $numb) ? "Ten" : "A Number");  //Ternary Operator can handle many nested wise!
  93. $numberCheck2 = ($numb %2 == 0) ? "Even Number " : "Odd Number";
  94. echo $numberCheck;
  95. echo "\n";
  96. echo $numberCheck2;
  97.  
  98. echo PHP_EOL;
  99.  
  100. // Problem number nine
  101.  
  102. for ($i = 1; $i<=10; $i++){
  103.    
  104.     echo PHP_EOL;
  105.  
  106.    for( $j= 10-$i; $j >=1 ; $j--){
  107.         echo "  ";
  108.     }
  109.     for ($j = 1; $j<= $i ; $j++ ){
  110.         echo " * ";
  111.     }
  112. }
  113. echo PHP_EOL;
  114. //problem number ten
  115.  
  116. $n = 10;
  117. for ($i = $n, $factorial = 1 ; $i > 1 ; $i--) {
  118.     $factorial *= $i;
  119. }
  120. printf ("The factorial number of %d is %d",$n,$factorial);
  121.  
  122. //Problem number eleven
  123.  
  124. echo PHP_EOL;
  125. echo "Fibonacchi series : ";
  126.  
  127. $very_old = 0;
  128. $old = 1;
  129. $new = 1;
  130. for ($i=0; $i<10; $i++){
  131.    
  132.     echo $very_old. " ";
  133.    
  134.     $old = $new;
  135.     $new = $very_old + $old;
  136.     $very_old = $old;
  137. }
  138.  
  139. // Problem number tweleve !!
  140. echo PHP_EOL;
  141. $x = 51;
  142. $y = 51;
  143. $sujon = $x <=> $y; // Spacesheep operator
  144. if ($sujon == 1){
  145.    echo "{$x} is greater than {$y}";
  146. }elseif ($sujon ==0){
  147.     echo "{$x} is equales to {$y}";
  148. }elseif($sujon ==-1){
  149.     echo "{$x} is smallar than {$y}";
  150. }
  151.  
  152. //problem number thirteen
  153.  
  154. echo "\n";
  155.  
  156. $default_lat = 51.5;
  157. $default_lon = 29.5;
  158.  
  159. $user_lat ;
  160.  
  161. $lat = $user_lat ?? $default_lat; //null coales operator better than ternary operator
  162.  
  163. echo $lat;
  164.  
  165. if (isset($user_lat)){
  166.     $lat = $user_lat;
  167. }else{
  168.     $lat = $default_lat;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement