From c3d0dff5e2cd9e5fa86ef5a3d0efd2548e3fc673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaura=20Mart=C3=AD?= Date: Tue, 20 Dec 2022 21:10:19 +0100 Subject: [PATCH] push deliverable --- README.md | 58 +++------------ arrays.php | 116 +++++++++++++++++++++++++++++ conditionals.php | 131 +++++++++++++++++++++++++++++++++ dates.php | 53 ++++++++++++++ functions.php | 113 ++++++++++++++++++++++++++++ iterators.php | 85 +++++++++++++++++++++ maths.php | 116 +++++++++++++++++++++++++++++ operators.php | 131 +++++++++++++++++++++++++++++++++ phpinfo.php | 2 + print.php | 29 ++++++++ strings.php | 187 +++++++++++++++++++++++++++++++++++++++++++++++ types.php | 78 ++++++++++++++++++++ 12 files changed, 1053 insertions(+), 46 deletions(-) create mode 100644 arrays.php create mode 100644 conditionals.php create mode 100644 dates.php create mode 100644 functions.php create mode 100644 iterators.php create mode 100644 maths.php create mode 100644 operators.php create mode 100644 phpinfo.php create mode 100644 print.php create mode 100644 strings.php create mode 100644 types.php diff --git a/README.md b/README.md index 7c787c3..6668708 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,17 @@ -`#php` `#basics` `#master-in-software-development` - # PHP Basics -

- Version -

- -> In this project you will learn the basic notions of the famous PHP language which is so used in the world of web development. -> -> What distinguishes PHP from other languages ​​such as Javascript is that the code is executed on the server, generating HTML and sending it to the client. +It is a library with different features that php offers. ## Index -- [Requirements](#requirements) -- [Repository](#repository) -- [Technologies used](#technologies-used) -- [Project delivery](#project-delivery) -- [Resources](#resources) - -## Requirements - -- Learn the basics to program in PHP -- Understand what a server-side language is and what it is used for - -## Repository - -First of all you must fork this project into your GitHub account. - -To create a fork on GitHub is as easy as clicking the “fork” button on the repository page. - -Fork on GitHub - -## Technologies used - -\* PHP - -## Project delivery - -To deliver this project you must send a Pull Request as explained in the Students Handbook. Remember that the PR title must be with the format -- Solution: + NAME AND SURNAME or TEAM NAMES AND SURNAMES. -- For example: "Solution: Josep Riera", "Solution: Josep Riera, Toni Suárez, Marta Vázquez" - -## Resources - -- [What can PHP do?](https://www.php.net/manual/es/intro-whatcando.php) -- [Sample guide for README](https://gist.github.com/Villanuevand/6386899f70346d4580c723232524d35a) -- [XAMPP](https://www.apachefriends.org/es/index.html) -- [How to install XAMPP on Windows](https://www.youtube.com/watch?v=h6DEDm7C37A) -- [What is a web server?](https://www.youtube.com/watch?v=Yt1nesKi5Ec) -- [Web server basics](https://www.youtube.com/watch?v=3VqfpVKvlxQ) +- [Print](#print) +- [Iterators](#iterators) +- [Operators](#operators) +- [Dates](#dates) +- [Conditionals](#conditionals) +- [Types](#types) +- [Maths](#maths) +- [Strings](#strings) +- [Arrays](#arrays) +- [Functions](#functions) +- [Phpinfo](#phpinfo) diff --git a/arrays.php b/arrays.php new file mode 100644 index 0000000..1498af6 --- /dev/null +++ b/arrays.php @@ -0,0 +1,116 @@ + + +Types"; + +echo '
+

+$arrStr = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); + +$arrNum = [1, 4, 6, 1.4, 5.9, 5, 7]; + +$arrMult = [1, 4, 6, [4, 9, 4]]; +

+
'; +$arrStr = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); +$arrNum = [1, 4, 6, 1.4, 5.9, 5, 7]; +$arrMult = [1, 4, 6, [4, 9, 4]]; + +echo "

Length

"; + +echo '

+function lengthArr($arr) +{ + return count($arr); +} + +$resultLengthA = lengthArr($arrStr); +

'; + +function lengthArr($arr) +{ + return count($arr); +} + +$resultLengthA = lengthArr($arrStr); + +echo $resultLengthA; + +echo "

Combination Two Arrays

"; + +echo '

+function combination($arr1, $arr2) +{ + return array_combine($arr1, $arr2); +} + +$resultCombination = combination($arrNum, $arrStr); +

'; + +function combination($arr1, $arr2) +{ + return array_combine($arr1, $arr2); +} + +$resultCombination = combination($arrNum, $arrStr); + +print_r($resultCombination); + +echo "

Last Element

"; + +echo '

+function lastEl($arr) +{ + return end($arr); +} + +$resultLastEl = lastEl($arrNum); +

'; + +function lastEl($arr) +{ + return end($arr); +} + +$resultLastEl = lastEl($arrNum); + +echo $resultLastEl; + +echo "

Add Element

"; + +echo '

+function addElement($arr, $elAdd) +{ + return array_push($arr, $elAdd); + return $arr; +} + +$resultAddEl = addElement($arrStr, "Monday"); +

'; + +function addElement($arr, $elAdd) +{ + array_push($arr, $elAdd); + return $arr; +} + +$resultAddEl = addElement($arrStr, "Monday"); + +print_r($resultAddEl); diff --git a/conditionals.php b/conditionals.php new file mode 100644 index 0000000..57b4d2c --- /dev/null +++ b/conditionals.php @@ -0,0 +1,131 @@ + + +$currentDay = new DateTime();"; + +$currentDay = new DateTime(); + + +echo "

Current day = Monday

"; + +echo "

$day = $currentDay -> format('D');

"; +$day = $currentDay->format('D'); + +echo '

+if ($day == "Mon") { + echo "We are on Monday"; +} +

'; + +if ($day == 'Mon') { + echo "We are on Monday"; +} + +echo "

Current month = October

"; + +echo "

$month = $currentDay -> format('M');

"; +$month = $currentDay->format('m'); + +echo '

+if ($month == 10) { + echo "We are in October"; +} +

'; + +if ($month == 10) { + echo "We are in October"; +} + +echo "

Double condition

"; + +echo "

$currentMinute = $currentDay -> format('i');

"; +$currentMinute = $currentDay->format('i'); + +echo '

+if ($currentMinute < 10) { + echo "the current minute is less than 10"; +} elseif ($currentMinute > 15) { + echo "the current minute is more than 15"; +} else { + echo "does not meet any conditions"; +} +

'; + +if ($currentMinute < 10) { + echo "the current minute is less than 10"; +} elseif ($currentMinute > 15) { + echo "the current minute is more than 15"; +} else { + echo "does not meet any conditions"; +} + + +echo "

Switch

"; + +echo "

$dayOfWeek = $currentDay -> format('D');

"; +$dayOfWeek = $currentDay->format('D'); + +echo '

+switch ($dayOfWeek) { + case "Mon": + echo "Today is Monday"; + break; + case "Tue": + echo "Today is Tuesday"; + break; + case "Wed": + echo "Today is Wednesday"; + break; + case "Thu": + echo "Today is Thursday"; + break; + case "Fri": + echo "Today is Friday"; + break; + case "Sat": + echo "Today is Saturday"; + break; + default: + echo "Today is Sunday"; +} + +

'; + +switch ($dayOfWeek) { + case "Mon": + echo "Today is Monday"; + break; + case "Tue": + echo "Today is Tuesday"; + break; + case "Wed": + echo "Today is Wednesday"; + break; + case "Thu": + echo "Today is Thursday"; + break; + case "Fri": + echo "Today is Friday"; + break; + case "Sat": + echo "Today is Saturday"; + break; + default: + echo "Today is Sunday"; +} diff --git a/dates.php b/dates.php new file mode 100644 index 0000000..096f927 --- /dev/null +++ b/dates.php @@ -0,0 +1,53 @@ + +$today = new DateTime();"; + +$today = new DateTime(); + +echo "

Format 'Y-m-d'

"; +echo "

echo $today -> format('Y-m-d');

"; + +echo $today->format('Y-m-d'); + +echo "

Current date any format

"; +echo "

(d-m-y)

"; + +echo $today->format('d-m-y'); + +echo "

(d-m-Y)

"; + +echo $today->format('d-m-Y'); + +echo "

(d-M-Y)

"; + +echo $today->format('d-M-Y'); + +echo "

(D-m-Y)

"; + +echo $today->format('D-m-Y'); + +echo "

Current day

"; +echo "

echo $today -> format('d'), $today -> format('D');

"; +echo $today->format('d'), $today->format('D'); + +echo "

Current month

"; +echo "

echo $today -> format('m');

"; +echo $today->format('m'); + +echo "

Current minute

"; +echo "

echo $today -> format('i');

"; +echo $today->format('i'); diff --git a/functions.php b/functions.php new file mode 100644 index 0000000..04ace54 --- /dev/null +++ b/functions.php @@ -0,0 +1,113 @@ + + +Sum"; + +echo '

+function sum($num1, $num2) +{ + return $num1 + $num2; +} + +$resultSum = sum(4, 5); +

'; + +function sum($num1, $num2) +{ + return $num1 + $num2; +} + +$resultSum = sum(4, 5); + +echo $resultSum; + +echo "

Multiplication

"; + +echo '

+function multiplication($num1, $num2) +{ + return $num1 * $num2; +} + +$resultMultiplication = multiplication(4, 5); +

'; + +function multiplication($num1, $num2) +{ + return $num1 * $num2; +} + +$resultMultiplication = multiplication(4, 5); + +echo $resultMultiplication; + + +echo "

Division

"; + +echo '

+function division($num1, $num2) +{ + return $num1 / $num2; +} + +$resultDivision = division(10, 5); +

'; + +function division($num1, $num2) +{ + return $num1 / $num2; +} + +$resultDivision = division(10, 5); + +echo $resultDivision; + + +echo "

Operation

"; + +echo '

+function operation($num1, $num2, $operator) +{ + if ($operator == "+") { + return sum($num1, $num2); + } elseif ($operator == "*") { + return multiplication($num1, $num2); + } else { + return division($num1, $num2); + } +} + +$resultOperation = operation(4, 2, "/"); +

'; + +function operation($num1, $num2, $operator) +{ + if ($operator == "+") { + return sum($num1, $num2); + } elseif ($operator == "*") { + return multiplication($num1, $num2); + } else { + return division($num1, $num2); + } +} + +$resultOperation = operation(4, 2, "/"); + +echo $resultOperation; diff --git a/iterators.php b/iterators.php new file mode 100644 index 0000000..39b3f56 --- /dev/null +++ b/iterators.php @@ -0,0 +1,85 @@ + +$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
"; + +$days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; + +echo "

For

"; + +echo '

+for ($i = 0; $i < count($days); $i++) { + echo "Today is $days[$i]."; +} +

'; +for ($i = 0; $i < count($days); $i++) { + echo "

Today is $days[$i].

"; +} + +echo "

For Each

"; + +echo '

+foreach ($days as $day) { + echo "Today is $day."; +} +

'; + +foreach ($days as $day) { + echo "

Today is $day.

"; +} + +echo "

While

"; + +echo '

+$i = 0; + +while ($i < count($days)) { + echo "Today is $days[$i]."; + $i++; +} +

'; + +$i = 0; +while ($i < count($days)) { + echo "

Today is $days[$i].

"; + $i++; +} + +echo "

Do While

"; + +echo '

+$i = 0; + +do { + echo "Today is $days[$i]."; + $i++; +} while ($i < count($days)); +

+
'; + +$i = 0; +do { + echo "

Today is $days[$i].

"; + $i++; +} while ($i < count($days)); diff --git a/maths.php b/maths.php new file mode 100644 index 0000000..e6c0054 --- /dev/null +++ b/maths.php @@ -0,0 +1,116 @@ + + +Absolute Value"; + +echo '

+function absolute($num) +{ + return abs($num); +}; + +$result = absolute(5); +

'; + +function absolute($num) +{ + return abs($num); +}; + +$resultAbs = absolute(5); +echo $resultAbs; + +echo "

Rounded Value Up

"; + +echo '

+function rounded($num) +{ + return ceil($num); +}; + +$resultRound = rounded(1.3); +

'; + +function rounded($num) +{ + return ceil($num); +}; + +$resultRound = rounded(1.3); +echo $resultRound; + +echo "

Highest value

"; + +echo '

+function highest($arr) +{ + return max($arr); +} + +$arrNums = [2, 5, 6, 9, 3, 0, 4]; +$resultHigh = highest($arrNums); +

'; +function highest($arr) +{ + return max($arr); +} + +$arrNums = [2, 5, 6, 9, 3, 0, 4]; +$resultHigh = highest($arrNums); +echo $resultHigh; + +echo "

Lowest Value

"; + +echo '

+function lowest($arr) +{ + return min($arr); +} + +$arrNums = [2, 5, 6, 9, 3, 1, 4]; +$resultLow = lowest($arrNums); +

'; + +function lowest($arr) +{ + return min($arr); +} + +$arrNums = [2, 5, 6, 9, 3, 1, 4]; +$resultLow = lowest($arrNums); +echo $resultLow; + + +echo "

Random Number

"; + +echo '

+function random($min, $max) +{ + return rand($min, $max); +} + +$resultRand = random(1, 10); +

'; +function random($min, $max) +{ + return rand($min, $max); +} + +$resultRand = random(1, 10); +echo $resultRand; diff --git a/operators.php b/operators.php new file mode 100644 index 0000000..f72d757 --- /dev/null +++ b/operators.php @@ -0,0 +1,131 @@ + +Arithmetic Operators"; +echo "

var_dump(3 + 2) = "; +var_dump(3 + 2); +echo ";

"; + +echo "

var_dump(3 - 2) = "; +var_dump(3 - 2); +echo ";

"; + +echo "

var_dump(3 * 2) = "; +var_dump(3 * 2); +echo ";

"; + +echo "

var_dump(3 / 2) = "; +var_dump(3 / 2); +echo ";

"; + +echo "

var_dump(3 % 2) = "; +var_dump(3 % 2); +echo ";

"; + +echo "

Comparison Operators

"; + +echo "

var_dump(3 == 2) = "; +var_dump(3 == 2); +echo ";

"; + +echo "

var_dump(3 != 2) = "; +var_dump(3 != 2); +echo ";

"; + +echo "

var_dump(3 < 2) = "; +var_dump(3 < 2); +echo ";

"; + +echo "

var_dump(3 > 2) = "; +var_dump(3 > 2); +echo ";

"; + +echo "

var_dump(3 <= 2) = "; +var_dump(3 <= 2); +echo ";

"; + +echo "

var_dump(3 >= 2) = "; +var_dump(3 >= 2); +echo ";

"; + +echo "

Logical Operators

"; + +echo "

&& / and:

"; + +echo "

$a = 3 > 2 && 2 > 2;

"; +echo "

$b = 3 > 2 and 2 > 2;

"; + +$a = 3 > 2 && 2 > 2; +$b = 3 > 2 and 2 > 2; + +echo "

var_dump($a) = "; +var_dump($a); +echo ";

"; + +echo "

var_dump($b) = "; +var_dump($b); +echo ";

"; + +echo "

|| / or:

"; + +echo "

$c = 2 > 2 || 3 > 2;

"; +echo "

$d = 2 > 2 or 3 > 2;

"; + +$c = 2 > 2 || 3 > 2; +$d = 2 > 2 or 3 > 2; + +echo "

var_dump($a) = "; +var_dump($c); +echo ";

"; + +echo "

var_dump($b) = "; +var_dump($d); +echo ";

"; + +echo "

!(NOT):

"; + +echo "

$e = 3 > 2;

"; +echo "

$f = 2 > 2;

"; + +$e = 3 > 2; +$f = 2 > 2; + +echo "

var_dump(!$e) = "; +var_dump(!$e); +echo ";

"; + +echo "

var_dump(!$f) = "; +var_dump(!$f); +echo ";

"; + +echo "

Xor:

"; + +echo "

$g = 5 > 3;

"; +echo "

$h = 4 > 3;

"; +echo "

$i = 4 > 5;

"; + +$g = 5 > 3; +$h = 4 > 3; +$i = 4 > 5; + + +echo "

var_dump($g Xor $h) = "; +var_dump($g xor $h); +echo ";

"; + +echo "

var_dump($h Xor $i) = "; +var_dump($h xor $i); +echo ";

"; diff --git a/phpinfo.php b/phpinfo.php new file mode 100644 index 0000000..33da5b2 --- /dev/null +++ b/phpinfo.php @@ -0,0 +1,2 @@ + + body { + font-family: Georgia, 'Times New Roman', Times, serif; + padding: 2em; + } + + h2 { + color: #18978F; + margin-top: 2em; + } + +$greeting= 'Hi!';"; +echo "

$morning= 'Good Morning!';

"; +echo "

$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];


"; + +$greeting = "Hi!"; +$morning = "Good Morning!"; +$days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; + +echo "

echo

"; +echo "

echo $greeting, $morning;

"; +echo $greeting, $morning; +echo "

print

"; +echo "

print $greeting;

"; +print "$greeting"; +echo "

print_r

"; +echo "

print_r($days);

"; +print_r($days); diff --git a/strings.php b/strings.php new file mode 100644 index 0000000..ca5fc3f --- /dev/null +++ b/strings.php @@ -0,0 +1,187 @@ + + +Print"; +echo "

print 'Hi!';

"; +print "Hi!
"; + +echo '

+$morning = "Good Morning!"; + +print $morning; +

'; +$morning = "Good Morning!"; + +print $morning; + +echo "

print 'Hi!'.$morning;

"; +print "Hi!" . $morning; + +echo "

Replace

"; + +echo '

+function replaceString($search, $replace, $subject) +{ + return str_replace($search, $replace, $subject); +} + +$resultReplaced = replaceString("Morning", "Night", "Good Morning"); +

'; + +function replaceString($search, $replace, $subject) +{ + return str_replace($search, $replace, $subject); +} + +$resultReplaced = replaceString('Morning', 'Night', 'Good Morning'); +echo $resultReplaced; + +echo '

+function replaceStringNoSensitive($search, $replace, $subject) +{ + return str_ireplace($search, $replace, $subject); +} + +$resultReplacedNoSensitive = replaceStringNoSensitive("morning", "night", "Good Morning"); +

'; + +function replaceStringNoSensitive($search, $replace, $subject) +{ + return str_ireplace($search, $replace, $subject); +} + +$resultReplacedNoSensitive = replaceStringNoSensitive('morning', 'night', 'Good Morning'); +echo $resultReplacedNoSensitive; + +echo "

Repeat

"; + +echo '

+function repeat($str, $times) +{ + return str_repeat($str, $times); +} + +$resultRepeat = repeat("Hi!", 7); +

'; +function repeat($str, $times) +{ + return str_repeat($str, $times); +} + +$resultRepeat = repeat("Hi!", 7); + +echo $resultRepeat; + +echo "

Length

"; + +echo '

+function lengthStr($str) +{ + return strlen($str); +} + +$resultLength = lengthStr("Good Morning!"); +

'; +function lengthStr($str) +{ + return strlen($str); +} + +$resultLength = lengthStr("Good Morning!"); + +echo $resultLength; + +echo "

Position First Occurrence

"; + +echo '

+function position($string, $tofind) +{ + return strpos($string, $tofind); +} + +$resultPosition = position("Good Morning!", "M"); +

'; + +function position($string, $toFind) +{ + return strpos($string, $toFind); +} + +$resultPosition = position("Good Morning!", "M"); + +echo $resultPosition; + +echo "

Uppercase

"; + +echo '

+function toUpperC($str) +{ + return strtoupper($str); +} + +$resultUpp = toUpperC("Good Morning!"); +

'; + +function toUpperC($str) +{ + return strtoupper($str); +} + +$resultUpp = toUpperC("Good Morning!"); + +echo $resultUpp; + +echo "

Lowercase

"; + +echo '

+function toLowerC($str) +{ + return strtolower($str); +} + +$resultLow = toLowerC("Good Morning!"); +

'; +function toLowerC($str) +{ + return strtolower($str); +} + +$resultLow = toLowerC("Good Morning!"); + +echo $resultLow; + +echo "

Obtain a text substring

"; + +echo '

+function substring($str, $position) +{ + return substr($str, $position); +} + +$resultSubstring = substring("Good Morning!", 5); +

'; +function substring($str, $position) +{ + return substr($str, $position); +} + +$resultSubstring = substring("Good Morning!", 5); + +echo $resultSubstring; diff --git a/types.php b/types.php new file mode 100644 index 0000000..5714cd8 --- /dev/null +++ b/types.php @@ -0,0 +1,78 @@ + + +Boolean"; + +echo "

$isValid = TRUE;

"; +$isValid = TRUE; +echo $isValid; + +echo "

$isNotValid = FALSE;

"; +$isNotValid = FALSE; +echo $isNotValid; + +echo "

Integer

"; +echo "

$num = 2022;

"; +$num = 2022; +echo $num; + +echo "

Float

"; +echo "

$floatNum = 2.35;

"; +$floatNum = 2.35; +echo $floatNum; + +echo "

String

"; +echo "

$greeting= 'Hi!';

"; +$greeting = "Hi!"; +echo $greeting; + +echo "

Array

"; +echo "

$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

"; +$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); +print_r($days); + +echo "

Object

"; +echo '

+$days = (object) [ + "Monday" => 1, + "Tuesday" => 2, + "Wednesday" => 3, + "Thursday" => 4, + "Friday" => 5, + "Saturday" => 6, + "Sunday" => 7, +]; +

'; +$days = (object) [ + 'Monday' => 1, + 'Tuesday' => 2, + 'Wednesday' => 3, + 'Thursday' => 4, + 'Friday' => 5, + 'Saturday' => 6, + 'Sunday' => 7, +]; + +print_r($days); + +echo "

NULL

"; + +echo "

$isNull = NULL;

"; +$isNull = NULL; +echo $isNull;