avitoholix

FastTrackHWintro

Jun 13th, 2014
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.54 KB | None | 0 0
  1. 1. Print current date
  2.  
  3. <!--Try to find date time function and print with echo or print functions current date.-->
  4.  
  5. <?php
  6.     echo "Today is " . date("Y/m/d") . "<br>";
  7.     echo "Today is " . date("Y.m.d") . "<br>";
  8.     echo "Today is " . date("d-m-Y") . "<br>";
  9.     echo "Today is " . date("l");  
  10. ?>
  11. 2. Type of variable
  12.  
  13. <!--Declare all possible types of variable and then, print their types with echo. You can use function gettype() that
  14. return type of given variable.-->
  15.  
  16. <?php
  17.     $data = array(1, 1., NULL, new stdClass, 'foo');
  18.     foreach ($data as $value)
  19.         {
  20.             echo gettype($value), "\n";
  21.         }
  22.   ?>
  23. 3. Rectangle Area
  24.  
  25.    <!--Write a program that enters the sides of a rectangle (two integers a and b) and calculates and prints the rectangle's
  26. area. Examples:
  27.  
  28. Input Output
  29. 7 20 140
  30. 5 12 60-->
  31.  
  32. <form action="3_ RectangleArea.php" method="post">
  33.     <input type="text" name="width" value="Width">
  34.     <input type="text" name="height" value="Height">
  35. <select name="select">
  36.     <option value="rectangle">Rectangle</option>
  37.     <option value="triangle">Triangle</option>
  38. </select>
  39.     <input type="submit" value="Go">
  40. </form>
  41. <?php
  42. if($_POST['select'] === 'rectangle')
  43. {
  44.     echo 'Result: '. $_POST['width'] * $_POST['height'];
  45.  
  46. }
  47. elseif($_POST['select'] === 'triangle')
  48. {
  49.     echo 'Result: '. $_POST['width'] * $_POST['height']/2;
  50.  
  51. }
  52. ?>
  53. 4. Triangle Area
  54.  
  55. <!--Write a program that enters 3 points in the plane (as integer x and y coordinates), calculates and prints the area of
  56. the triangle composed by these 3 points. Round the result to a whole number. In case the three points do not form
  57. a triangle, print "0" as result. Examples:
  58. Input Output Input Output Input Output
  59.  
  60. -5 10 575
  61. 25 30
  62. 60 15
  63.  
  64. 53 18 86
  65. 56 23
  66. 24 27
  67.  
  68. 1 1 0
  69. 2 2
  70. 3 3-->
  71. <h1 style="font-size: 1.2em">Area of a Triangle</h1>
  72. <form action="4_TriangleArea.php" method="post">
  73.     <input type="text" name="x1" value="Width(x1)"><br>
  74.     <input type="text" name="y1" value="Height(y1)"><br>
  75.     <input type="text" name="x2" value="Width(x2)"><br>
  76.     <input type="text" name="y2" value="Height(y2)"><br>
  77.     <input type="text" name="x3" value="Width(x3)"><br>
  78.     <input type="text" name="y3" value="Height(y3)">
  79.     <input type="submit" value="Go">
  80. </form>
  81. <?php
  82. $x1 = $_POST['x1'];
  83. $y1 = $_POST['y1'];
  84. $x2 = $_POST['x2'];
  85. $y2 = $_POST['y2'];
  86. $x3 = $_POST['x3'];
  87. $y3 = $_POST['y3'];
  88. $aBool = (($x1*($y2-$y3))+($x2*($y3-$y1))+($x3*($y1-$y2)))/2;
  89. echo 'Area:'.$aBool;
  90. ?>
  91. 5. Boolean Variable
  92.  
  93. <!--Declare a Boolean variable called isFemale and assign an appropriate value corresponding to your gender. Print it
  94. on the web page.-->
  95.  
  96. <form action="5_BooleanVariable.php" method="post">
  97.     <div>My gender is? </div>
  98.     <select name="select">
  99.          <option></option>
  100.         <option value="male">Male</option>
  101.         <option value="female">Female</option>
  102.     </select>
  103.     <input type="submit" value="Submit">
  104. </form>
  105.  
  106. <?php
  107. $select = $_POST['select'];
  108. $myGender = TRUE;
  109. if($select === 'male')
  110. {
  111.     $myGender = TRUE;
  112.     echo 'Male ';
  113.     echo $myGender;
  114. }
  115. elseif($select === 'female')
  116. {
  117.     $myGender = FALSE;
  118.     echo 'Female';
  119.     echo $myGender;
  120. }
  121. ?>
  122. 6. Quotes in Strings
  123.  
  124. <!--Declare two string variables and assign them with following value:
  125.             I asked a girl out and she said – “I don't know”. Does she mean yes or no?
  126. Do the above in two different ways: with and without using quoted strings. Print the variables to ensure that their
  127. value was correctly defined.-->
  128.  
  129.  
  130. <?php
  131.  $a = 'I asked a girl out and she said - ';
  132.  $b = "\"I don't know\". ";
  133.  $c = 'Does she mean yes or no?';
  134.  echo 'I asked a girl out and she said - '. $b . 'Does she mean yes or no?'.'<br>';
  135.  echo $a,$b,$c;
  136. ?>
  137.  
  138. 7. Type Casting
  139.  
  140. <!--You can pretend that a variable or value is of a different type by using a type cast. Try to cast floating point number
  141. to integer and then print it on the web page.-->
  142.  
  143. <?php
  144. $a = 3.2;
  145. $b = 3;
  146. echo (int)$a;
  147.  
  148. 8. Print Html
  149.  
  150. ?
  151. ?>
  152.  
  153. 9. *Divide by 2 and 9
  154.  
  155. <!--Write a Boolean expression that checks for given integer if it can be divided (without remainder) by 7 and 5 in the
  156. same time. Examples:
  157.  
  158. n Divided by 7 and 5?
  159. 17 false
  160. 0 false
  161. 10 false
  162. 7 false
  163. 18 true
  164. 72 true-->
  165.  
  166. <form action="9_DivideBy_2_ and_ 9.php" method="post">
  167.     <input type="text" name="n" value="integer">
  168.     <input type="submit" value="Go">
  169.     <input type="reset" value="Reset">
  170. </form>
  171. <?php
  172. $aBool = TRUE;
  173. $n = $_POST['n'];
  174. if($n >= 1)
  175. {
  176.     if($n % 2 == 0 || $n % 9 == 0)
  177.     {
  178.         $aBool = TRUE;
  179.         echo 'True';
  180.     }
  181.     else
  182.     {
  183.         $aBool = FALSE;
  184.         echo 'False';
  185.     }
  186. }
  187. else
  188. {
  189.     $aBool = FALSE;
  190.     echo 'False';
  191. }
  192. ?>
  193. 10. *Third Digit is 7?
  194.  
  195. <!--Write an expression that checks for given integer if its third digit from right-to-left is 7. Examples:
  196. n Third digit 7?
  197.  
  198. 5 false
  199. 701 true
  200. 9703 true
  201. 877 false
  202. 777877 false
  203. 9999799 true-->
  204. <div>Write integer:</div>
  205. <form action="10_ThirdDigitIis_7.php" method="post">
  206.     <input type="text" name="int">
  207.     <input type="submit" value="Push">
  208. </form>
  209.  
  210. <?php
  211. $n = $_POST['int'];
  212. $aBool =($n/100)%10==7;
  213. if($aBool == 7){
  214.     echo 'True';  
  215. }
  216.  else {
  217.     echo 'False';
  218. }
  219. ?>
  220. 11. **Numbers from 1 to n
  221.  
  222. <!--Write a program that reads an integer number n from the input and prints all the numbers in the interval [1..n],
  223. each on a single line. Note that you may need to use a for-loop. Examples:
  224. numbers sum numbers sum numbers sum-->
  225. <div>Write integer:</div>
  226. <form action="11_NumbersFrom1_To_ n.php" method="post">
  227.     <input type="text" name="int">
  228.     <input type="submit" value="Push">
  229. </form>
  230. <?php
  231. $n = $_POST['int'];
  232. for($i=0;$i<=$n;$i++){
  233.     echo $i.'</br>';
  234.     $sum+=$i;
  235. }
  236. echo 'Sum: '.$sum.'</br>';
  237. ?>
  238. Problem 12. ***Fibonacci Numbers
  239.  
  240. <!--Write a program that reads a number n and prints on the web page the first n members of the Fibonacci sequence
  241. (at a single line, separated by spaces) : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, .... Note that you may need to
  242. learn how to use loops. Examples:
  243.  
  244. n   comments
  245. 1     0
  246. 3     0 1 1
  247. 10    0 1 1 2 3 5 8 13 21 34-->
  248.  
  249. <div>Write integer:</div>
  250. <form action="12_FibonacciNumbers.php" method="post">
  251.     <input type="text" name="int">
  252.     <input type="submit" value="Push">
  253. </form>
  254. <?php
  255. $n = $_POST['int'];
  256. $a = 1;
  257. $b = 1;
  258. $c = 0;
  259. $counter = 0;
  260. for($i=0;$i<$n;$i++){
  261.     $counter++;
  262.     $c = $a+$b;
  263.     $a = $b;
  264.     $b = $c;
  265.     $c = $a+$b;
  266.     echo $c.'</br>';
  267.  }  
  268.  echo $n.' Item';
  269. ?>
Advertisement
Add Comment
Please, Sign In to add comment