Advertisement
avitoholix

PHPFlowControl

Jun 20th, 2014
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. //1. Exchange value
  2.  
  3. <!--Write an if statement that examines two integer variables and exchanges their values if the first one is greater than
  4. the second one.-->
  5.  
  6. <h1 style="font-size: 1.2em">Change Variable Value</h1>
  7. <form action="1_ExchangeValue.php" method="post">
  8.     <input type="text" name="a" value="a"><br>
  9.     <input type="text" name="b" value="b">
  10.     <input type="submit" value="Go">
  11. </form>
  12. <?php
  13. if(isset($_POST['a'],$_POST['b'])){
  14.     $a = $_POST['a'];
  15.     $b = $_POST['b'];
  16.     echo "a= ".$a.",b= ".$b.'<br>';
  17.     if($a>$b){
  18.         $c = $a;
  19.         $a = $b;
  20.         $b = $c;
  21.         echo 'Result: ';
  22.         echo "a= ".$a.",b= ".$b;
  23.     }  else {
  24.         echo 'Change value only when a>b!';
  25.     }
  26. }
  27. ?>
  28.  
  29. //2. Show sign
  30. //3. Sort numbers
  31.  
  32. <!--Sort 3 real values in descending order using nested if statements.-->
  33.  
  34. <?php
  35. $numbers = array("a" => 5, "b" => 4, "c" => 3, "d" => 2);
  36. asort($numbers);
  37. foreach ($numbers as $key => $val) {
  38.     echo "$key = $val.<br>";
  39. }
  40. ?>
  41.  
  42. //4. Point in a Circle
  43.  
  44. <!--Write an expression that checks if given point (x, y) is inside a circle K({0, 0}, 2). Examples:
  45.  
  46. x     y      inside
  47. 0     1       true
  48. -2    0       true
  49. -1    2      false
  50. 1.5  -1       true
  51. -1.5 -1.5     false
  52. 100  -30      false
  53. 0      0      true
  54. 0.2  -0.8     true
  55. 0.9  -1.93   false
  56. 1     1.655   true-->
  57.  
  58. <h1 style="font-size: 1.2em">Point in a Circle</h1>
  59. <form action="4_PointInCircle.php" method="post">
  60.     <input type="text" name="x" value="x"><br>
  61.     <input type="text" name="y" value="y">
  62.     <input type="submit" value="Go">
  63. </form>
  64. <?php
  65.   if(isset($_POST['x'],$_POST['y'])){
  66.     $x = $_POST;
  67.     $y = $_POST;
  68.     $circle = (($x*$x)+($y*$y))<=2*2;
  69.         echo $circle ? 'True':'False';
  70.    
  71.   }
  72. ?>
  73.  
  74. //5. Matrix of Numbers
  75.  
  76. <!--Write a program that reads from the console a positive integer number n (1 ≤ n ≤ 20) and prints a matrix like in the
  77. examples below. Use two nested loops. Examples:
  78. n matrix n matrix n matrix
  79.  
  80. 1 2
  81. 2
  82. 2 3 3
  83. 1 2 3
  84. 2 3 4
  85. 3 4 5
  86. 1 2 3 4
  87. 2 3 4 5
  88. 4
  89. 3 4 5 6
  90. 4 5 6 7-->
  91.  
  92. <h1 style="font-size: 1.2em">Matrix Of Numbers</h1>
  93. <form action="5_MatrixOfNumbers.php" method="post">
  94.     <input type="text" name="a" value="x"><br>
  95.     <input type="submit" value="Go">
  96. </form>
  97. <?php
  98. if(isset($_POST['a'])){
  99.     $n = $_POST['a'];
  100.     for ($row = 1; $row <= $n; $row++) {
  101.         for ($i = $row; $i < $n+$row; $i++) {
  102.             //echo ' ','<br>';
  103.             echo $i;
  104.             echo ' ';
  105.         }
  106.         echo ' '.'<br>';
  107.     }
  108. }
  109. ?>
  110.  
  111. //6. Odd and Even Product
  112. //7. TOTO
  113.  
  114. for ($i = 1; $i <=40; $i++) {
  115.     for ($j = $i+1; $j <=41; $j++) {
  116.         for ($y = $j+1; $y <=42; $y++) {
  117.             echo $i.' '.$j.' '.$y;
  118.         }
  119.     }
  120. }
  121. ?>
  122.  
  123. //8. Print Prime Numbers
  124.  
  125. <?php
  126. function prime($n){
  127. for($i=1;$i<=$n;$i++){
  128.     $counter = 0;
  129.     for($j=1;$j<=$i;$j++){
  130.         if($i % $j==0){
  131.             $counter++;
  132.                 }
  133.         }
  134.         if($counter==2){
  135.             print $i." is Prime <br>";
  136.         }
  137.     }
  138. }
  139. prime(100);
  140.        
  141. //9. Extract words
  142.  
  143. <!--Write a function that takes as argument an array, and extract all the words from that array without repeating the
  144. same ones.
  145.  
  146. Array result
  147. Nakov,
  148. Svetlin,
  149. Nakov, Pesho,
  150. Mario,
  151. Dimityr,
  152. Georgi, Mario
  153.  
  154.  
  155. Nakov, Svetlin,
  156. Pesho, Mario,
  157. Dimityr,Georgi-->
  158.  
  159.  
  160. <?php
  161. $input = array("Nakov", "Svetlin","Nakov", "Pesho", "Mario","Dimityr","Georgi","Mario");
  162. print_r(array_unique($input));
  163. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement