Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Working with Variables
- <?php
- namespace Codecademy;
- $first = "Welcome to the magical world of built-in functions.";
- $second = 82137012983;
- //Write your code below:
- echo gettype( $first ), "\n";
- echo gettype( $second ), "\n";
- echo var_dump( $first ), "\n";
- echo var_dump( $second ), "\n";
- /*********************************/
- 2. String Functions
- <?php
- namespace Codecademy;
- // Write your code below:
- echo strrev(".pu ti peeK .taerg gniod er'uoY"), "\n";
- echo strtolower("SOON, tHiS WILL Look NoRmAL."), "\n";
- echo str_repeat("\nThere's no place like home.\n", 3);
- /*********************************/
- 3. Working with Substrings
- <?php
- namespace Codecademy;
- $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.";
- $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.";
- // Write your code below:
- echo substr_count( $essay_one, "really" );
- echo substr_count( $essay_two, "obvious" );
- /*********************************/
- 4. Number Functions
- <?php
- namespace Codecademy;
- // Write your code below:
- function calculateDistance( $num1, $num2 ) {
- $distance = abs($num1 - $num2);
- return $distance;
- }
- echo calculateDistance(-1, 4), "\n";
- function calculateTip( $meal_cost ) {
- $tip = ($meal_cost * 18) / 100;
- $total = $meal_cost + $tip;
- return round( $total );
- }
- echo calculateTip(100), "\n";
- echo calculateTip(88.77);
- /*********************************/
- 5. Generating Random Numbers
- <?php
- namespace Codecademy;
- // Write your code below:
- echo getrandmax();
- echo "\n";
- echo rand();
- echo "\n";
- echo rand(1,52);
- /*********************************/
- 6. Documentation
- <?php
- namespace Codecademy;
- $a = 29;
- $b = "You did it!";
- $c = STR_PAD_BOTH;
- $d = "*~*";
- // Write your code below:
- echo str_pad($b, $a, $d, $c);
- /*********************************/
- 7.Finding Functions
- <?php
- namespace Codecademy;
- // Write your code below:
- function convertToShout( $shout ) {
- return strtoupper($shout) . "!\n";
- }
- echo convertToShout("woah there, buddy");
- echo convertToShout("i just don't know");
- function tipGenerously( $meal ) {
- $tip = ( $meal * 20 ) / 100;
- $total = ceil( $meal + $tip );
- return $total;
- }
- echo tipGenerously(982.27);
- function calculateCircleArea( $diameter ) {
- return pi() * ( $diameter/2 )**2;
- }
- echo calculateCircleArea(29);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement