Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. 1. Working with Variables
  2. <?php
  3. namespace Codecademy;
  4.  
  5. $first = "Welcome to the magical world of built-in functions.";
  6.  
  7. $second = 82137012983;
  8.  
  9. //Write your code below:
  10.  
  11. echo gettype( $first ), "\n";
  12. echo gettype( $second ), "\n";
  13.  
  14. echo var_dump( $first ), "\n";
  15. echo var_dump( $second ), "\n";
  16.  
  17. /*********************************/
  18. 2. String Functions
  19. <?php
  20. namespace Codecademy;
  21.  
  22. // Write your code below:
  23.  
  24. echo strrev(".pu ti peeK .taerg gniod er'uoY"), "\n";
  25. echo strtolower("SOON, tHiS WILL Look NoRmAL."), "\n";
  26.  
  27. echo str_repeat("\nThere's no place like home.\n", 3);
  28.  
  29. /*********************************/
  30. 3. Working with Substrings
  31.  
  32. <?php
  33. namespace Codecademy;
  34.  
  35. $essay_one = "I really enjoyed the book. I thought the characters were really interesting. The plot of the novel was really engaging. I really felt the characters' emotions. They were really well-written. I really wish the ending had been different though.";
  36.  
  37. $essay_two = "Obviously this is a really good book. You obviously would not have made us read it if it wasn't. I felt like the ending was too obvious though. It was so obvious who did it right away. I think the thing with the light was obviously a metaphor for life. It would have been better if the characters were less obvious about their secrets.";
  38.  
  39. // Write your code below:
  40.  
  41. echo substr_count( $essay_one, "really" );
  42. echo substr_count( $essay_two, "obvious" );
  43.  
  44. /*********************************/
  45. 4. Number Functions
  46.  
  47. <?php
  48. namespace Codecademy;
  49.  
  50. // Write your code below:
  51.  
  52. function calculateDistance( $num1, $num2 ) {
  53. $distance = abs($num1 - $num2);
  54. return $distance;
  55. }
  56. echo calculateDistance(-1, 4), "\n";
  57.  
  58. function calculateTip( $meal_cost ) {
  59. $tip = ($meal_cost * 18) / 100;
  60. $total = $meal_cost + $tip;
  61. return round( $total );
  62. }
  63. echo calculateTip(100), "\n";
  64. echo calculateTip(88.77);
  65.  
  66. /*********************************/
  67. 5. Generating Random Numbers
  68.  
  69. <?php
  70. namespace Codecademy;
  71.  
  72. // Write your code below:
  73. echo getrandmax();
  74. echo "\n";
  75. echo rand();
  76. echo "\n";
  77. echo rand(1,52);
  78.  
  79. /*********************************/
  80. 6. Documentation
  81. <?php
  82. namespace Codecademy;
  83.  
  84. $a = 29;
  85. $b = "You did it!";
  86. $c = STR_PAD_BOTH;
  87. $d = "*~*";
  88.  
  89. // Write your code below:
  90. echo str_pad($b, $a, $d, $c);
  91.  
  92. /*********************************/
  93. 7.Finding Functions
  94. <?php
  95. namespace Codecademy;
  96.  
  97. // Write your code below:
  98.  
  99. function convertToShout( $shout ) {
  100. return strtoupper($shout) . "!\n";
  101. }
  102.  
  103. echo convertToShout("woah there, buddy");
  104. echo convertToShout("i just don't know");
  105.  
  106. function tipGenerously( $meal ) {
  107. $tip = ( $meal * 20 ) / 100;
  108. $total = ceil( $meal + $tip );
  109. return $total;
  110. }
  111.  
  112. echo tipGenerously(982.27);
  113.  
  114. function calculateCircleArea( $diameter ) {
  115. return pi() * ( $diameter/2 )**2;
  116. }
  117. echo calculateCircleArea(29);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement