Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
php-functions/index.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
139 lines (134 sloc)
3.14 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>String Functions</title> | |
</head> | |
<body> | |
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
?> | |
<p> | |
<?php | |
// Get the length of your own name | |
// and print it to the screen! | |
$length = strlen("David"); | |
print $length; | |
?> | |
</p> | |
<p> | |
<?php | |
$myname = "brandon"; | |
// Get a partial string from within your own name | |
// and print it to the screen! | |
$partial = substr($myname, 0,5); | |
print $partial; | |
?> | |
</p> | |
<p> | |
<?php | |
// Make your name upper case and print it to the screen: | |
$uppercase = strtoupper($myname); | |
print $uppercase; | |
?> | |
</p> | |
<p> | |
<?php | |
// Make your name lower case and print it to the screen: | |
print strtolower("Brandon"); | |
print strtoupper("brandon"); | |
?> | |
</p> | |
<p> | |
<?php | |
// Print out the position of a letter that is in | |
// your own name | |
// Check for a false value of a letter that is not | |
// in your own name and print out an error message | |
$charspot = strpos("brandon","a"); | |
echo $charspot; | |
?> | |
</p> | |
<p> | |
<?php | |
if (strpos("brandon","h") === false) { | |
print "Sorry, no 'h' in 'brandon'"; | |
} | |
?> | |
</p> | |
<p> | |
<?php | |
// Try rounding a floating point number to an integer | |
// and print it to the screen | |
$round = round(M_PI); | |
print $round; | |
?> | |
</p> | |
<p> | |
<?php | |
// Try rounding a floating point number to 3 decimal places | |
// and print it to the screen | |
$round_decimal = round(M_PI, 4); | |
print $round_decimal; | |
?> | |
</p> | |
<?php | |
// Use your knowledge of strlen(), substr(), and rand() to | |
// print a random character from your name to the screen. | |
$name = "brandon"; | |
$nameLen = strlen($name); | |
print substr($name, rand(0, $nameLen)-1, 1); | |
?> | |
</p> | |
<p> | |
<?php | |
// Create an array and push 5 elements on to it, then | |
// print the number of elements in your array to the screen | |
$fav_foods = array(); | |
array_push($fav_foods, "pizza"); | |
array_push($fav_foods, "tacos"); | |
array_push($fav_foods, "pasta"); | |
array_push($fav_foods, "sushi"); | |
array_push($fav_foods, "steak"); | |
print count($fav_foods); | |
?> | |
</p> | |
<p> | |
<?php | |
// Create an array with several elements in it, | |
// then sort it and print the joined elements to the screen | |
$array = array(1, 5, 9, 3, 7); | |
sort($array); | |
print join(", ", $array); | |
?> | |
</p> | |
<p> | |
<?php | |
// Reverse sort your array and print the joined elements to the screen | |
$array = array(1, 5, 9, 3, 7); | |
rsort($array); | |
print join(", ", $array); | |
?> | |
</p> | |
<p> | |
<?php | |
// Create an array and push on the names | |
// of your closest family and friends | |
$friends = array(); | |
array_push($friends, "Joe"); | |
array_push($friends, "Ryan"); | |
array_push($friends, "Eddy"); | |
array_push($friends, "Tim"); | |
array_push($friends, "Tyler"); | |
// Sort the list | |
sort($friends); | |
// Randomly select a winner! | |
$random = rand(0,count($friends) -1); | |
$winner = join($friends); | |
$winner = $friends[rand(0,5)]; | |
// Print the winner's name in ALL CAPS | |
print strtoupper($winner); | |
?> | |
</p> | |
</body> | |
</html> |