Advertisement
em89

PHP Introduction

Jun 11th, 2014
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. //1. Print current date
  2. echo date("F j, Y, g:i a");
  3.  
  4. //2. Type of variable
  5. $boolVar = true;
  6. $intVar = 9;
  7. $doubVar = 1.23;
  8. $strVar = 'Coffee';
  9. $arrVar = array(3, 9, 4, 16);
  10. $nullVar = null;
  11. $resourceVar = mysql_connect();
  12. $objVar = (object) 'Ivan Ivanov';
  13. echo gettype($boolVar) . "</br>";
  14. echo gettype($intVar) . "</br>";
  15. echo gettype($doubVar) . "</br>";
  16. echo gettype($strVar) . "</br>";
  17. echo gettype($arrVar) . "</br>";
  18. echo gettype($nullVar) . "</br>";
  19. echo gettype($resourceVar) . "</br>";
  20. echo gettype($objVar);
  21.  
  22. //3. Rectangle Area
  23. $a = 5;
  24. $b = 10;
  25. $area = $a * $b;
  26. echo "Rectangle's area is: " . $area;
  27.  
  28. //4. Triangle Area
  29. $x1 = 15;
  30. $y1 = 1;
  31. $x2 = 25;
  32. $y2 = 52;
  33. $x3 = 32;
  34. $y3 = 13;
  35. $area = ($x1*($y2-$y3) + $x2*($y3-$y1) + $x3*($y1 - $y2))/2;
  36. echo "Triangles's area is: " . round(abs($area));
  37.  
  38. //5. Boolean Variable
  39. //First version.
  40. $isFemale = true;
  41. $answer = ($isFemale) ? 'True' : 'False';
  42. echo "Am I a female? -" . $answer . "</br>";
  43. //Second version.
  44. if ($isFemale == true) {
  45. echo "I am a female.";
  46. }
  47. else
  48. echo "I am not a female.";
  49.  
  50. //6. Quotes in Strings
  51. $withQuotes = "I asked a girl out and she said - \"I don't know\". Does she mean yes or no?";
  52. echo "Using quoted string: " . $withQuotes . "</br>";
  53. $withoutQuotes = <<<EOT
  54. I asked a girl out and she said - "I don't know". Does she mean yes or no?
  55. EOT;
  56. echo "Without using quoted string: " . $withoutQuotes;
  57.  
  58. //7. Type Casting
  59. $float = 2.4;
  60. echo "$float is: " . gettype($float) . "</br>";
  61. settype($float, 'integer');
  62. echo "$float is: " . gettype($float);
  63.  
  64. //8. Print Html
  65. $h = '<h1>Hello World</h1>';
  66. $p = '<p>I love Software Univerity</p>';
  67. echo htmlspecialchars($h) . "</br>";
  68. echo htmlspecialchars($p);
  69.  
  70. //9. Divide by 2 and 9
  71. $int = 2;
  72. if ($int % 2 == 0) {
  73. if ($int % 9 == 0) {
  74. echo 'true';
  75. }
  76. else
  77. echo 'false';
  78. }
  79. else
  80. echo 'false';
  81.  
  82. //10. Third Digit is 7?
  83. $int = 8143747;
  84. $result = (integer)$int / 100;
  85. if ($result % 10 == 7) {
  86. echo "true";
  87. }
  88. else
  89. echo "false";
  90.  
  91. //11. Numbers from 1 to n
  92. $n = 20;
  93. for ($i = 1; $i <= $n; $i++) {
  94. echo $i . "</br>";
  95. }
  96.  
  97. //12. Fibonacci numbers
  98. $x = 1;
  99. $y = 0;
  100. $n = 10;
  101. echo "0 ";
  102. for($i = 2; $i <= $n; $i++) {
  103. $z = $x + $y;
  104. echo $z . " ";
  105. $x = $y;
  106. $y = $z;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement