Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Codecademy;
- //Built-in functions
- //reverse string
- echo strrev(".pu ti peeK .taerg gniod er'uoY");
- //to lower case
- echo strtolower("SOON, tHiS WILL Look NoRmAL.");
- //repeat string for certain no. of times
- echo str_repeat("\nThere's no place like home.\n", 3);
- $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.";
- // count the no. of times the substring in 2nd parameter appears in the string (1st parameter)
- echo substr_count($essay_one, "really");
- echo substr_count($essay_two, "obvious"); //this includes "obviously" because the function is not concerned with which characters come before or after the string
- // using absolute value function
- function calculateDistance($first, $second){
- return abs($first - $second);
- }
- //using round function
- function calculateTip($meal){
- return round($meal*1.18);
- }
- echo calculateDistance(-1,99);
- echo calculateTip(100);
- // Random numbers generator
- echo getrandmax(); //to get the largest possible random integer
- echo "\n";
- echo rand(); // get number between 0 and the max above
- echo "\n";
- echo rand(1,52); // get number within the range (1 and 52 inclusive)
- // using str_pad() to understand PHP documentation
- $a = 29;
- $b = "You did it!";
- $c = STR_PAD_BOTH;
- $d = "*~*";
- echo str_pad($b, $a, $d, $c);
- // using other built-in functions
- function convertToShout($say){
- return strtoupper($say) . "!";
- }
- function tipGenerously($meal){
- return ceil($meal*1.2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement