PHP Week 3

<?php
//takes input string and convert to lower and upper case respectively
echo "Insert input: ";
$string_user=fgets(STDIN);
$string_lower = strtolower($string_user);
$string_upper = strtoupper($string_user);
echo "\nLowercase: $string_lower \nUppercase: $string_upper\n";
?>

Fany-Savinas-Mac:PHP Fany$ php lower_upper_case.php
Insert input: I Am Awesome!

Lowercase: i am awesome!

Uppercase: I AM AWESOME!

------

<?php
//adds, substracts, multiplies, divides two input numbers on different lines
echo "Insert Initial Number: ";
$number1=trim(fgets(STDIN));
echo "Insert Final Number: ";
$number2=trim(fgets(STDIN));
$sum_total_one = $number1 + $number2;
$sum_total_two = $number1 - $number2;
$sum_total_three = $number1 * $number2;
$sum_total_four = $number1 / $number2;
echo "\n$number1 + $number2= $sum_total_one\n";
echo "$number1 - $number2= $sum_total_two\n";
echo "$number1 x $number2= $sum_total_three\n";
echo "$number1 / $number2= $sum_total_four\n\n";
?>

Fany-Savinas-Mac:PHP Fany$ php number_maths.php
Insert Initial Number: 3
Insert Final Number: 5

3 + 5= 8
3 - 5= -2
3 x 5= 15
3 / 5= 0.6

------

<?php
//echoes initial and final with an 'and'
echo "Insert Initial Input: ";
//trim() removes whitespaces added when inserting variable
$initial_noun=trim(fgets(STDIN));
echo "Insert Final Input: ";
$final_noun=fgets(STDIN);
echo "\n$initial_noun and $final_noun\n";
?>

Fany-Savinas-Mac:PHP Fany$ php two_noun.php
Insert Initial Input: Dog
Insert Final Input: Cat

Dog and Cat

------

<?php
//repeats input a specified number of times
echo "Insert Initial Input: ";
$string=trim(fgets(STDIN));
echo "Insert Repetition: ";
$repeat=trim(fgets(STDIN));
echo "\n";
echo str_repeat("$string\n",$repeat);
echo "\n";
?>

Fany-Savinas-Mac:PHP Fany$ php repeat_input.php
Insert Initial Input: Epic
Insert Repetition: 3

Epic
Epic
Epic

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

Leave a reply