From 6cb0700fb625c6d3c8293957d032f0cb7ea26d92 Mon Sep 17 00:00:00 2001 From: Ynohe Date: Wed, 21 Dec 2022 10:12:27 +0000 Subject: [PATCH] First and last commit for the PHP basics. --- arrays.php | 67 +++++++++++++++++++++++++++++++++++ conditionals.php | 66 +++++++++++++++++++++++++++++++++++ dates.php | 33 ++++++++++++++++++ functions.php | 45 ++++++++++++++++++++++++ iterators.php | 43 +++++++++++++++++++++++ maths.php | 45 ++++++++++++++++++++++++ operators.php | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ phpinfo.php | 5 +++ print.php | 19 ++++++++++ strings.php | 77 ++++++++++++++++++++++++++++++++++++++++ types.php | 56 +++++++++++++++++++++++++++++ 11 files changed, 547 insertions(+) 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/arrays.php b/arrays.php new file mode 100644 index 0000000..970cfcd --- /dev/null +++ b/arrays.php @@ -0,0 +1,67 @@ +"; + +#Define a simple array consisting of whole numbers and decimal numbers + + +$numbers = array(1, 2.4, 4, 6, 7.9); + +echo "I have " . $numbers[0]; + +echo "
"; + +#Define a multidimensional array + +$brand = array ( + array("Sony","Nike"), + array("Microsoft","Adidas"), + array("Nintendo","Kinder"), + ); + +echo $brand[0][1]; + +echo "
"; + +#Execute the function that allows to obtain the length of an array + + +$game = array(1, 2, 3, 4, 5, 6, 7); + +echo sizeof($game); + +echo"
"; + +#Execute the function that allows to obtain the combination of two arrays + + +$days = array("Monday","Sunday","Friday"); +$months = array("January","May","December"); + +$year = array_combine($days,$months); +print_r($year); + +echo "
"; + +#Execute the function that once is given an array return the last element of it + + +$last = array("one", "two", "three", "four"); + +echo end($last); + +echo "
"; + +#Execute the function that once is given an array add a new element to the array in question + +$colors = array("blue","purple"); +array_push($colors,"black","yellow","orange","green"); +print_r($colors); + +?> \ No newline at end of file diff --git a/conditionals.php b/conditionals.php new file mode 100644 index 0000000..0c4f476 --- /dev/null +++ b/conditionals.php @@ -0,0 +1,66 @@ +"; + +#Create a simple condition that evaluates whether the current month is October. + +if (date('m') === "October"){ + echo "We are in October"; +}else{ + echo "Today is ". date('F'); +} + +echo "
"; + +#Create a double condition + +$currentMinute = date('i'); + +if($currentMinute < 10){ + echo "The current minute is less than 10"; +}else if($currentMinute > 15){ + echo "The current minute is more than 15"; +}else{ + echo "Does not meet any conditions"; +} + +echo "
"; + +#Create a switch type control structure to display a different message depending on the current day of the week. + +$day = date('D'); + +switch($day){ + case 'Sun'; + echo "Today is Sunday"; + break; + case 'Sat'; + echo "Today is Saturday"; + break; + case 'Fri'; + echo "Today is Friday"; + break; + case 'Thu'; + echo "Today is Thursday"; + break; + case 'Wed'; + echo "Today is Wednesday"; + break; + case 'Tue'; + echo "Today is Tuesday"; + break; + case 'Mon'; + echo "Today is Monday"; + break; +} +?> \ No newline at end of file diff --git a/dates.php b/dates.php new file mode 100644 index 0000000..480479b --- /dev/null +++ b/dates.php @@ -0,0 +1,33 @@ +"; + +$dates = date("D-M-Y"); + +echo $dates; + +echo"
"; + +$dates = date("D"); + +echo $dates; + +echo "
"; + +$dates = date("m"); + +echo $dates; + +echo "
"; + +$dates = date("i"); + +echo $dates; + + + +?> \ No newline at end of file diff --git a/functions.php b/functions.php new file mode 100644 index 0000000..bd1db84 --- /dev/null +++ b/functions.php @@ -0,0 +1,45 @@ +"; + +#Create a function that given two numbers returns the multiplication of both + +function multiply(int $a, int $b) { + + return $a * $b; + } + +echo multiply(10, 10); + +echo "
"; + +#reate a function that given two numbers returns the division of both + +function division(int $c, int $d) { + + return $c / $d; +} +echo division(20, 5); + +echo "
"; + +#Create a function that, given two numbers and an operation (add, multiply or divide), returns the result of that operation. + +function operation ($f, $g, $operation){ + if($operation == "multiply"){ + return $f * $g; + } +} + +echo operation(3, 5, "multiply"); + +?> \ No newline at end of file diff --git a/iterators.php b/iterators.php new file mode 100644 index 0000000..0147173 --- /dev/null +++ b/iterators.php @@ -0,0 +1,43 @@ +"; + +#Generate a snippet that makes use of foreach + +$drinks = array("cocacola", "fanta", "pepsi", "appletiser"); + +foreach($drinks as $choose){ + echo $choose; +} + +echo "
"; + +#Generate a snippet that uses while + +$more = 10; + +while($more < 20){ + echo $more; + $more++; +} + +echo "
"; + +#Generate a snippet that uses do while + +$less = 20; + +do{ + echo $less; + $less++; +} +while($less < 25); + + +?> \ No newline at end of file diff --git a/maths.php b/maths.php new file mode 100644 index 0000000..82dc411 --- /dev/null +++ b/maths.php @@ -0,0 +1,45 @@ +"; + +#Define a variable whose value is the result of the function that returns a rounded value to the next highest integer. + +$round = round(3.6); + +echo $round; + +echo"
"; + +#Define a variable whose value is the result of the function that returns the highest value of a series of values ​​that are received by parameter. + +$highest = max(2, 3, 1, 6, 7); + +echo $highest; + +echo"
"; + +#Define a variable whose value is the result of the function that returns the lowest value of a series of values ​​that are received by parameter. + + +$lowest = min(5, 4, 1, 6, 9); + +echo $lowest; + +echo "
"; + +#Define a variable whose value is the result of the function that returns a random number + +$random = rand(); + +echo $random; + +echo"
"; + +?> \ No newline at end of file diff --git a/operators.php b/operators.php new file mode 100644 index 0000000..a076824 --- /dev/null +++ b/operators.php @@ -0,0 +1,91 @@ +"; + +var_dump(2-1); + +echo"
"; + +var_dump(2*3); + +echo"
"; + +var_dump(10%3); + +echo"
"; + +var_dump(15/2); + +echo"

"; + +#comparison operators + +var_dump(2==2); + +echo"
"; + +var_dump(2!=1); + +echo"
"; + +var_dump(5<10); + +echo"
"; + +var_dump(10>5); + +echo"
"; + +var_dump(5<=5); + +echo"
"; + +var_dump(5>=2); + +echo"

"; + +#logical operators + +$first=true; +$second=false; + +if(($first===true) && ($second===false)){ + echo "true"; +} + +echo"
"; + +if(($first===true) || ($second===false)){ + echo "true"; +} + +echo"
"; + + +if(($first===true) and ($second===false)){ + echo "true"; +} + +echo"
"; + +if(($first===true) or ($second===false)){ + echo "true"; +} + +echo"
"; + +if(!$second){ + echo "false"; +} + +echo"
"; + +if(($first===true) xor ($second===true)){ + echo "true"; +} + +?> \ 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..dad03e7 --- /dev/null +++ b/print.php @@ -0,0 +1,19 @@ +"; + +#Generate an instruction that makes use of "print" + +print "This is an example of print
"; + + +#Generate an instruction that makes use of "print_r", it is important that you assign a complex value to analyze its potential + + +$value = [1, 2, 3, 4, 5]; +print_r($value); + +?> \ No newline at end of file diff --git a/strings.php b/strings.php new file mode 100644 index 0000000..8723109 --- /dev/null +++ b/strings.php @@ -0,0 +1,77 @@ +"; + +#Print a text string that interpret variables + +$string = "This is a variable"; + +print($string); + +echo "
"; + +#Concatenate a previously declared variable in a text string + +print($string. "/ Print a text string"); + +echo "
"; + +#Execute the function that allows you to replace text in a string + +$fruit = "My favourite fruit is Apple"; + +print_r(str_replace("Apple", "Strawberry", $fruit)); + +echo "
"; + +#Execute the function that allows you to replace text in a string + +$fruit = "My favorite fruit is Appple"; + +print_r(str_ireplace("aPPle", "Strawberry", $fruit)); + +echo "
"; + +#Execute the function that allows you to write a text N times + +$repeat = "123"; + +echo str_repeat($repeat, 10); + +echo "
"; + +#Execute the function that allows to obtain the length of a text string + +$length = "Hi, I am Yno"; + +echo strlen($length); + +echo "
"; + +#Executes the function that allows to obtain the position of the first occurrence of a text within a text string + +echo strpos("I like summer", "summer"); + +echo "
"; + +#Execute the function that allows a text string to be capitalized + +echo strtoupper("this is stroupper"); + +echo "
"; + +#Execute the function that allows you to transform a text string to lowercase + +echo strtolower("this is strtolower"); + +echo "
"; + +#Execute the function that allows to obtain a text substring from a given position + +echo substr("this is a substr",6); + +?> \ No newline at end of file diff --git a/types.php b/types.php new file mode 100644 index 0000000..473a43a --- /dev/null +++ b/types.php @@ -0,0 +1,56 @@ +"; + +#Integer + +$integer = 99; + +echo "This is a integer: $integer"; + +echo "
"; + +#Float + +$float = 2.13; + +echo "This is a float: $float"; + +echo "
"; + +#String + +$string = "Soy un String"; + +echo "This is a string: $string"; + +echo "
"; + +#Array + +$array = array("playstation", "xbox", "switch"); + +echo "This is an array: $array[0]"; + +echo "
"; + +#Object + +$object = (object)["drink1"=>"cocacola", "drink2"=>"fanta", "drink3"=>"appletiser"]; + +echo "This is an object: $object->drink3"; + +echo "
"; + +#Null + +$null = null; + + +?> \ No newline at end of file