diff --git a/README.md b/README.md index 7c787c3..1f13f91 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,35 @@ -`#php` `#basics` `#master-in-software-development` +## PHP Intro & Basics -# PHP Basics +In this repository you will find all the basic syntax and part of the basic concepts, in addition, you will find basic code snippets which are frequently used in PHP. -

- Version -

+## Prerequisites +It is important that before starting you have an environment capable of running PHP. +If you want, you can download XAMPP, this software will allow you to have an entire environment at your disposal to carry out your developments. +This software brings you together everything you need like: web technologies ,web server, php interpreter and a MariaDB database. -> 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. -## Index +## Fork +- [Forked from assembler-institute/php-basics](https://github.com/assembler-institute/php-basics.git) -- [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 +\* PHP, XAMPP, VSCODE -## 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) +- [What can PHP do?-2](https://www.w3schools.com/php/default.asp) +- [DateTime class](https://www.youtube.com/watch?v=TP1jXmcrWIs) - [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) + +## Author + +Dayan Álvarez Martínez + diff --git a/arrays.php b/arrays.php new file mode 100644 index 0000000..2ab129f --- /dev/null +++ b/arrays.php @@ -0,0 +1,83 @@ +Simple array composed of text strings: (array() or [])
"); + +echo ('method: array("Ho", "Ho", "Ho", "Ho")'); +echo ("
"); +echo ('method: ["He", "He", "He", "He"]'); + +echo ("
"); + +# Simple array consisting of whole numbers and decimal numbers + +echo ("Simple array consisting of whole numbers and decimal numbers: 1, 2.3, 3.5, -4 (array() or [])
"); + +echo ('method: array(1, 2.3, 3.5, -4)'); +echo ("
"); +echo ('method: [1, 2.3, 3.5, -4]'); + +echo ("
"); + +# Multidimensional array + +echo ("Multidimensional array:
"); + +$cars = array ( + array("Volvo",22,18), + array("BMW",15,13), + array("Saab",5,2), + array("Land Rover",17,15) + ); + + echo '
'; print_r($cars); echo '
'; + +echo ("
"); + +# Obtain the length of an array + +echo ("Obtain the length of an array (count()).
"); + +$cars = array ( + array("Volvo",22,18), + array("BMW",15,13), + array("Saab",5,2), + array("Land Rover",17,15) + ); + +echo ("Length: " . count($cars)); + +echo ("
"); + +# Obtain the combination of two arrays + +echo ("Obtain the combination of two arrays(array_merge()).
"); + +$array1 = array("color", "red", 2, 4); +$array2 = array("a", "b", "color", "shape", 4); +$result = array_merge($array1, $array2); +echo '
1-'; print_r($array1); echo '
'; +echo '
2-'; print_r($array2); echo '
'; +echo '
Merged-'; print_r($result); echo '
'; + +echo ("
"); + +# Obtain the last element of an array + +echo ("Obtain the last element of an array(end()).
"); + +$array1 = array("color", "red", 2.32, "cars"); +echo '
'; print_r($array1); echo '
'; +echo ("the las element is: " . end($array1)); + +echo ("
"); + +# Add a new element to the array + +echo ("Add a new element to the array(array_push()).

"); + +$stack = array("orange", "banana"); +echo '
before: '; print_r($stack); echo '
'; +array_push($stack, "apple", "raspberry"); +echo '
after: '; print_r($stack); echo '
'; +?> \ No newline at end of file diff --git a/conditionals.php b/conditionals.php new file mode 100644 index 0000000..76773b3 --- /dev/null +++ b/conditionals.php @@ -0,0 +1,74 @@ +Conditionals"); + +# If current day is Monday + +echo ("Current day condition: "); +if(date("D") === "Mon") { + echo ("We are on Monday"); +}else{ + echo ("It is not Monday"); +} + +echo ("
"); + +# If current month is October + +echo ("Current month condition: "); +if(date("M") === "Oct") { + echo ("We are on October"); + +}else{ + echo date("M"); +} + +echo ("
"); + + +# Current minute condition + +echo ("Current Minute condition: "); + +if (date("i") < 10) { + echo ("The current minute is less than 10"); +}else if (date("i") > 15) { + echo ("The current minute is more than 15"); +}else { + echo ("Does not meet any conditions"); +} + +echo ("
"); + + +# Day of the week + + +echo ("Day of the week with switch: "); + + + +switch (date("w")) { + case 0: + echo ("Today it is Sunday"); + break; + case 1: + echo ("Today it is Monday"); + break; + case 2: + echo ("Today it is Tuesday"); + break; + case 3: + echo ("Today it is Wednesday"); + break; + case 4: + echo ("Today it is Thursday"); + break; + case 5: + echo ("Today it is Friday"); + break; + case 6: + echo ("Today it is Saturday"); + break; + +} +?> \ No newline at end of file diff --git a/dates.php b/dates.php new file mode 100644 index 0000000..c692318 --- /dev/null +++ b/dates.php @@ -0,0 +1,32 @@ +Dates"); + +# Date Time class + +$dateClass = new DateTime(); +print("Y-m-d format: "); +echo $dateClass->format("Y-m-d"); + +echo ("
"); + +# Current date +print("Current date in any format: "); +echo $dateClass->format("y-M-l"); + +echo ("
"); + +print("Current day: "); +echo $dateClass->format("l"); + +echo ("
"); + +print("Current month in numerical: "); +echo $dateClass->format("m"); + +echo ("
"); + +print("Current minute leading zeros: "); +echo $dateClass->format("00:i"); + + +?> \ No newline at end of file diff --git a/functions.php b/functions.php new file mode 100644 index 0000000..ce2bd96 --- /dev/null +++ b/functions.php @@ -0,0 +1,89 @@ +Function that given two numbers returns the sum of both.
"); + + +function sum($x, $y) { + return $x + $y; +} +echo '
'; print_r("function sum(x, y) {
+    return x, y; 
}"); echo '
'; +echo ("returned: " . sum(4, 6)); + +echo ("
"); + +# Function that given two numbers returns the multiplication of both + +echo("Function that given two numbers returns the multiplication of both.
"); + + +function multiplication($x, $y) { + return $x * $y; +} +echo '
'; print_r("function sum(x, y) {
+    return x * y; 
}"); echo '
'; +echo ("returned: " . multiplication(4, 6)); + +echo ("
"); + +# Function that given two numbers returns the division of both + + +echo("Function that given two numbers returns the division of both +.
"); + + +function division($x, $y) { + return $x * $y; +} +echo '
'; print_r("function sum(x, y) {
+    return x * y; 
}"); echo '
'; +echo ("returned: " . division(4, 6)); + +echo ("
"); + +# Function that given two numbers and an operation (add, multiply or divide), returns the result of that operation + + +echo("Function that given two numbers and an operation (add, multiply or divide), returns the result of that operation.
"); + +function xOperation($y, $x, $operation) { + if ($operation === "+") { + return $y + $x; + }else if ($operation === "-") { + return $y - $x; + }else if ($operation === "*") { + return $y * $x; + }else if ($operation === "/") { + return $y / $x; + }else if ($operation === "%") { + return $y % $x; + }else if ($operation === "**") { + return $y ** $x; + } + +} + +echo '
'; print_r("function operation() {
+    if (operation = `+`) {
+        return y + x;
+      }else if (operation = `-`) {
+          return y - x;
+      }else if (operation = `*`) {
+          return y * x;
+      }else if (operation = `/`) {
+          return y / x;
+      }else if (operation = `%`) {
+          return y % x;
+      }else if (operation = `**`) {
+          return y ** x;
+      }
+}"); echo '
'; + +echo ("returned: " . xOperation(4, 5, "+")); + +?> + + diff --git a/iterators.php b/iterators.php new file mode 100644 index 0000000..8a7df66 --- /dev/null +++ b/iterators.php @@ -0,0 +1,43 @@ +Iterators"); + +# For loop example + +$number = 0; +echo "for loop: "; +for ($i = 0; $i <= 5; $i++) { + echo "$i"; +} +echo"
"; + +# Foreach loop example + +echo "Foreach loop:
"; +$colors = array("red", "green", "blue", "yellow"); + +foreach ($colors as $value) { + echo "$value
"; +} + +echo"
"; + +# While loop example + +echo "While loop: "; +$integer = 0; +while($integer <= 5){ + echo "$integer"; + $integer++; +} + +echo"
"; + +# Do while loop example + +echo "Do while loop:
"; +$x = 1; +do { + echo "The number is: $x
"; + $x++; +} while ($x <= 5); +?> \ No newline at end of file diff --git a/maths.php b/maths.php new file mode 100644 index 0000000..c4d1e44 --- /dev/null +++ b/maths.php @@ -0,0 +1,46 @@ +Maths"); +echo ("
"); + +# Absolute value + + +$absolute = abs(5.6); +echo ("Absolute value of: 5.6
"); +echo("$absolute"); + +echo ("
"); + +# Rounded to the highest integer + +$roundedHight = ceil(5.6); +echo ("Rounded value to highest integer of: 5.6
"); +echo("$roundedHight"); + +echo ("
"); + +# Highest value of a series of values + +echo ("Highest value of a serie: 9, 4, 7, 1, 2
"); +$maxValue = max(9, 4, 7, 1, 2); +echo("$maxValue"); + +echo ("
"); + +# Lower value of a series of values + +echo ("Lower value of a serie: 9, 4, 7, 1, 2
"); +$loweValue = min(9, 4, 7, 1, 2); +echo("$loweValue"); + +echo ("
"); + +# Random number + +echo ("Random number from 50 to 500:
"); +$randomNumber = rand(50, 500); +echo("$randomNumber"); + + +?> \ No newline at end of file diff --git a/operators.php b/operators.php new file mode 100644 index 0000000..8b523c9 --- /dev/null +++ b/operators.php @@ -0,0 +1,79 @@ +Operators"); + +# Variables + +$a = 3.3; +$b = 4.3; + +# Aritmetic operators + +echo ("Aritmetics operators:
"); +$sum = $a + $b; +var_dump($sum); +echo ("
"); + +$rest = $a - $b; +var_dump($rest); +echo ("
"); + +$multi = $a * $b; +var_dump($multi); +echo ("
"); + +$div = $a / $b; +var_dump($div); +echo ("
"); + +$remainder = $a % $b; +var_dump($remainder); +echo ("
"); + + +# Comparison operators + +echo ("Comparison operators:
"); +var_dump($a == $b); +echo ("
"); + +var_dump($a != $b); +echo ("
"); + +var_dump($a < $b); +echo ("
"); + +var_dump($a > $b); +echo ("
"); + +var_dump($a <= $b); +echo ("
"); + +var_dump($a >= $b); +echo ("
"); + +# Logical operators + +echo ("Logical operators:
"); +if($a == 4 && $b == 4.3) { + var_dump("true"); +}else { + var_dump("false"); +} +echo ("
"); + + +if($a == 4 || $b == 4.3) { + var_dump("true"); +}else { + var_dump("false"); +} +echo ("
"); + +if(!$a) { + var_dump("true"); +}else { + var_dump("false"); +} +echo ("
"); + +?> \ No newline at end of file diff --git a/phpinfo.php b/phpinfo.php new file mode 100644 index 0000000..6480abf --- /dev/null +++ b/phpinfo.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/print.php b/print.php new file mode 100644 index 0000000..f927c05 --- /dev/null +++ b/print.php @@ -0,0 +1,22 @@ +Print"); + +# print example + +print "print: Hello Assembler
"; +echo"
"; + +# echo example + +echo "echo: Hello Assembler, ", "we are enjoying this!!"; +echo "
"; + +# print_r example + +$test = [1,4,5,7, [3, 4, 5]]; + +echo "print_r: "; +echo '
'; print_r($test); echo '
'; + + +?> \ No newline at end of file diff --git a/strings.php b/strings.php new file mode 100644 index 0000000..d74fbb2 --- /dev/null +++ b/strings.php @@ -0,0 +1,95 @@ +Text string:
"); +echo("(Example text string)"); + +echo ("
"); + +# Text string that interpret variables +echo ("Text string that interpret variables:
"); + +$textExample = ("Text string in variable"); +echo ("$textExample"); + +echo ("
"); + +# Concatenate a previously declared variable in a text string + +echo ("Concatenate a previously declared variable in a text string: (. or .=)
"); +echo("(Example text string) ". $textExample); + +echo ("
"); + +# Replace text in a string "case sensitive" + +echo ("Replace text in a string `case sensitive` (str_replace()):
"); +$phrase = "I like MUsic, EAt and SpOrts."; +$hobbies = array("MUsic", "EAt", "SpOrts"); +$hobbies2 = array("Cinema", "Gaming", "Travel"); +echo ("Original text: $phrase
"); + +$newPhrase = str_replace($hobbies, $hobbies2, $phrase); +echo ("Replaced text: $newPhrase"); + +echo ("
"); + +# Replace text in a string "case insensitive" + +echo ("Replace text in a string `case insensitive` (str_ireplace()):
"); +$phrase = "I like MUsic, eAt and SpOrts."; +$hobbies = array("mUsic", "Eat", "spOrts"); +$hobbies2 = array("Cinema", "Gaming", "Travel"); +echo ("Original text: $phrase
"); + +$newPhrase = str_ireplace($hobbies, $hobbies2, $phrase); +echo ("Replaced text: $newPhrase"); + +echo ("
"); + +# Write a text N times + +echo ("Write a text N times (str_repeat()):
"); +$text = str_repeat("Hello Assembler
", 10); +echo ("$text"); + +echo ("
"); + +# Length of a text string + +echo(" length of a text string `Keep pushing!` (strlen()):
"); +$text = strlen("Keep pushing!"); +echo ("letters: $text"); + +echo ("
"); + +# First occurrence of a text within a text string + +echo(" First occurrence of a text within a text string `Hello world` (strpos():
"); +$text = "Hello World"; +$findLetter = "l"; +$textChange = strpos($text, $findLetter); +echo ("index: $textChange"); + +echo ("
"); + +# Allows a text string to be capitalized + +echo(" Allows a text string to be capitalized `InCompreHeNsibilIties` (strtoupper() and strtolower():
"); + +$text = "InCompreHeNsibilIties"; + +echo("Uperrcase: ".strtoupper($text)); +echo ("
"); +echo ("Lowercase: " .strtolower($text)); + +echo ("
"); + +# obtain a text substring from a given position + +echo(" Obtain a text substring from a given position `Tutankhamun` (substr():
"); + +$text = "Tutankhamun"; +$textPosition = substr($text, 6); +echo("position: $textPosition") + +?> \ No newline at end of file diff --git a/types.php b/types.php new file mode 100644 index 0000000..fb20cb1 --- /dev/null +++ b/types.php @@ -0,0 +1,48 @@ +Data types"); + +# Boolean + +$typeBool = true; +var_dump($typeBool); +echo ("
"); + +# Integer + +$integer = 1; +var_dump($integer); +echo ("
"); + +# Float + +$float = 1.1; +var_dump($float); +echo ("
"); + +# String + +$string = "1"; +var_dump($string); +echo ("
"); + +# Array + +$array = array(1); +var_dump($array); +echo ("
"); + +# Object + +class typeBool{}; +$object = new typeBool; +var_dump($object); +echo ("
"); + +# NULL + +$null = NULL; +var_dump($null); +echo ("
"); + +?> \ No newline at end of file