PHP Week 5

<?php
//takes second half of string and places it in front of first half
echo "Input 10 characters: ";
$input = trim(fgets(STDIN));
$string_first_half = substr($input, 0, 5);
$string_second_half = substr($input, 5, 5);
echo "\n".$string_second_half.$string_first_half."\n\n";
?>

Fany-Savinas-Mac:PHP Fany$ php 10_char_rearrange.php
Input 10 characters: strawberry

berrystraw

------

<?php
echo "Insert first number: ";
$number_one = trim(fgets(STDIN));
echo "Insert second number: ";
$number_two = trim(fgets(STDIN));
echo "Insert third number: ";
$number_three =   trim(fgets(STDIN));
//if statements determine smallest number, and largest number
if ($number_one < $number_two && $number_one < $number_three) {
	echo "\nSmallest: $number_one\n";
}
elseif ($number_two < $number_one && $number_two < $number_three) {
	echo "\nSmallest: $number_two\n";
}
elseif ($number_three < $number_one && $number_three < $number_two) {
	echo "\nSmallest: $number_three\n";
}
if ($number_one > $number_two && $number_one > $number_three) {
	echo "Largest: $number_one";
}
elseif ($number_two > $number_one && $number_two > $number_three) {
	echo "Largest:  $number_two";
}
elseif ($number_three > $number_one && $number_three > $number_two) {
	echo "Largest:  $number_three";
}
echo "\n\n";
?>

Fany-Savinas-Mac:PHP Fany$ php highest_lowest_number.php
Insert first number: 23
Insert second number: 5
Insert third number: 67

Smallest: 5
Largest: 67

------

<?php
//sorts three words into alphabetical order
echo "Insert first word: ";
$word_one = trim(fgets(STDIN));
echo "Insert second word: ";
$word_two = trim(fgets(STDIN));
echo "Insert third word: ";
$word_three = trim(fgets(STDIN));
echo "\n";
$words = array($word_one, $word_two, $word_three);
sort($words);
foreach ($words as $key => $val) {
	echo $key." ".$val."\n";
}
echo "\n";
?>

Fany-Savinas-Mac:PHP Fany$ php alphabetical_order.php
Insert first word: Anne
Insert second word: Jack
Insert third word: Benny

0 Anne
1 Benny
2 Jack

This entry was posted in Computing and tagged . Bookmark the permalink.

Leave a reply