├── README.md
├── arrays.php
├── conditionals.php
├── dates.php
├── functions.php
├── iterators.php
├── maths.php
├── operators.php
├── phpinfo.php
├── print.php
├── strings.php
└── types.php
/README.md:
--------------------------------------------------------------------------------
1 | `#php` `#basics` `#master-in-software-engineering`
2 |
3 | # PHP Basics
4 |
5 |
6 |
7 |
8 |
9 | > In this project you will learn the basic notions of the famous PHP language which is so used in the world of web development.
10 | >
11 | > 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.
12 |
13 | ## Index
14 |
15 | - [Requirements](#requirements)
16 | - [Repository](#repository)
17 | - [Technologies used](#technologies-used)
18 | - [Project delivery](#project-delivery)
19 | - [Resources](#resources)
20 |
21 | ## Requirements
22 |
23 | - Learn the basics to program in PHP
24 | - Understand what a server-side language is and what it is used for
25 |
26 | ## Repository
27 |
28 | First of all you must fork this project into your GitHub account.
29 |
30 | To create a fork on GitHub is as easy as clicking the “fork” button on the repository page.
31 |
32 |
33 |
34 | ## Technologies used
35 |
36 | \* PHP
37 |
38 | ## Project delivery
39 |
40 | To deliver this project you must follow the steps indicated in the document:
41 |
42 | - [Submitting a solution](https://www.notion.so/Submitting-a-solution-524dab1a71dd4b96903f26385e24cdb6)
43 |
44 | ## Resources
45 |
46 | - [What can PHP do?](https://www.php.net/manual/es/intro-whatcando.php)
47 | - [Sample guide for README](https://gist.github.com/Villanuevand/6386899f70346d4580c723232524d35a)
48 | - [XAMPP](https://www.apachefriends.org/es/index.html)
49 | - [How to install XAMPP on Windows](https://www.youtube.com/watch?v=h6DEDm7C37A)
50 | - [What is a web server?](https://www.youtube.com/watch?v=Yt1nesKi5Ec)
51 | - [Web server basics](https://www.youtube.com/watch?v=3VqfpVKvlxQ)
52 |
--------------------------------------------------------------------------------
/arrays.php:
--------------------------------------------------------------------------------
1 | ';
10 | print_r($shoppingList);
11 |
12 | echo '
';
13 |
14 | // Define array of whole numbers and decimals
15 | $numbersMixed = array(21.7, 3.4459, 7, 62, 42.3);
16 | print_r($numbersMixed);
17 |
18 | echo '
';
19 |
20 | // Define a multidimensional array
21 | $progLanguages = array (
22 | array ('node', 'php', 'python'),
23 | array('javascript', 'html', 'css')
24 | );
25 | var_dump($progLanguages);
26 | echo '
';
27 | print_r($progLanguages);
28 |
29 | echo '
';
30 |
31 |
32 | // Count all the values in an array
33 | echo count($shoppingList);
34 |
35 | echo '
';
36 |
37 | // Combine 2 arrays
38 | // The first way creates keys of list a and values of list b.
39 | $anotherList = array ('pasta', 'oranges', 'carrots', 'zucchini');
40 | print_r(array_combine($shoppingList, $anotherList));
41 | echo '
';
42 | // The second seems more useful, concatenating the arrays, first a then b.
43 | print_r(array_merge($shoppingList, $anotherList));
44 |
45 | echo '
';
46 |
47 | // Get the last element in array
48 | $lastItem = end($anotherList);
49 | echo $lastItem;
50 |
51 | echo '
';
52 |
53 | // Add element to beginning of an array
54 | array_unshift($anotherList, 'bread');
55 | print_r($anotherList);
56 |
57 | echo '
';
58 |
59 | // Remove element from beginning of an array
60 | array_shift($anotherList);
61 | print_r($anotherList);
62 |
63 | echo '
';
64 |
65 | // Add element to end of an array
66 | array_push($anotherList, 'bread');
67 | print_r($anotherList);
68 |
69 | echo '
';
70 |
71 | // Remove element from end of an array
72 | array_pop($anotherList);
73 | print_r($anotherList);
74 |
75 | echo '
';
76 |
77 |
78 |
--------------------------------------------------------------------------------
/conditionals.php:
--------------------------------------------------------------------------------
1 | format('N') == 1) {
10 | echo 'Today is Monday.';
11 | } else {
12 | echo 'Today is not Monday.';
13 | }
14 |
15 | echo '
';
16 |
17 | // Condition to see if current month is October.
18 | if ($today->format('n') == 10) {
19 | echo 'We are in October.';
20 | } else {
21 | echo 'We are not in October.';
22 | }
23 |
24 | echo '
';
25 |
26 | // Double condition evaluating if current minute is less than 10, and also if it's greater than 15.
27 | $min = $today->format('i');
28 | if ($min < 10) {
29 | echo 'the current minute is less than 10';
30 | } else if ($min > 15) {
31 | echo 'the current minute is more than 15';
32 | } else {
33 | echo 'does not meet any conditions';
34 | }
35 |
36 | echo '
';
37 |
38 | // Switch structure for different message each day of the week.
39 | $currentDay = $today->format('N');
40 | echo '
current day as digit is: ';
41 | echo gettype($currentDay);
42 | echo '
';
43 | switch ($currentDay) {
44 | case 1:
45 | echo 'Mondays are so blah.';
46 | break;
47 | case 2:
48 | echo "What? It's only Tuesday.";
49 | break;
50 | case 3:
51 | echo "It's Wednesday. We're halfway there.";
52 | break;
53 | case 4:
54 | echo "We're almost to Friday. Woo-hoo!";
55 | break;
56 | case 5:
57 | echo "Is today Friday already? It's Friday!";
58 | break;
59 | case 6:
60 | echo 'What to do with my Saturday?';
61 | break;
62 | case 7:
63 | echo "Oh...tomorrow's Monday.";
64 | break;
65 | }
66 |
67 | echo '
';
68 |
69 |
70 |
71 | echo '
';
--------------------------------------------------------------------------------
/dates.php:
--------------------------------------------------------------------------------
1 | format('Y-m-d');
8 |
9 | echo '
';
10 |
11 | // Current date, ATOM format
12 | $today = new DateTime('Today');
13 | echo $today->format('Y-m-d\TH:i:sP');
14 |
15 | echo '
';
16 |
17 | // Get the current day.
18 | echo $today->format('l');
19 | echo '
';
20 | echo $today->format('D');
21 |
22 | echo '
';
23 |
24 | // Get numerical representation of month, with leading 0 for single-digit months and without.
25 | echo $today->format('m');
26 | echo '
';
27 | echo $today->format('n');
28 | echo '
';
29 | // Then represented as text
30 | echo $today->format('F'); // full month in text
31 | echo '
';
32 | echo $today->format('M'); // 3 letter text
33 |
34 | echo '
';
35 | // Get the current minute with leading zeros.
36 | echo $now->format('i');
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/functions.php:
--------------------------------------------------------------------------------
1 |
';
13 |
14 | // Create function that returns multiplication of two given numbers
15 | function multiply($a, $b) {
16 | echo $a * $b;
17 | }
18 | multiply(4, 5);
19 |
20 | echo '
';
21 |
22 | // Create function that returns division of two given numbers
23 | function divide($a, $b) {
24 | echo $a / $b;
25 | }
26 | divide(18, 6);
27 |
28 | echo '
';
29 |
30 | // Create function that returns result of operation, with params for two numbers and operation type: add, multiply, divide
31 | function operate($a, $b, $opType) {
32 | $opType($a, $b);
33 | }
34 | operate(4, 5, 'divide');
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/iterators.php:
--------------------------------------------------------------------------------
1 |
';
19 |
20 | // foreach
21 | class Taxi // declare taxi object / class
22 | {
23 | public $color = 'yellow';
24 | public $location = 'local';
25 | public $price = '10eur or less';
26 | }
27 |
28 | $class = new Taxi(); // create obj / class
29 |
30 | foreach ($class as $key => $value) {
31 | print "key: $key, value: $value
";
32 | }
33 |
34 | echo '
';
35 |
36 | // while
37 | $i = 1;
38 | while ($i <= 10) {
39 | echo $i++;
40 | echo '    ';
41 | }
42 |
43 |
44 | echo '
';
45 |
46 | // do while
47 | $j = 10;
48 | do {
49 | echo $j--;
50 | echo '    ';
51 | } while ($j > 0);
--------------------------------------------------------------------------------
/maths.php:
--------------------------------------------------------------------------------
1 |
';
12 |
13 | // Round to ceiling
14 | $roundedUp = ceil(14.2);
15 | echo $roundedUp;
16 |
17 | echo '
';
18 |
19 | // Return highest in an array, or of given values
20 | $highest = max(23, 40, 6, 72, 2, 57, 12, 16, 29);
21 | echo $highest;
22 |
23 | echo '
';
24 |
25 | // Return lowest in an array, or of given values
26 | $lowest = min(23, 40, 6, 72, 2, 57, 12, 16, 29);
27 | echo $lowest;
28 |
29 | echo '
';
30 |
31 | // Return an random integer. Add arguments for min, max
32 | $randomInt = rand(1, 100);
33 | echo $randomInt;
34 |
--------------------------------------------------------------------------------
/operators.php:
--------------------------------------------------------------------------------
1 |
';
14 |
15 | // -
16 | $a = 5;
17 | $b = 10;
18 | $ansSubtract = $a - $b;
19 | echo "$a - $b = $ansSubtract";
20 |
21 | echo '
';
22 |
23 | // / *
24 | $a = 6;
25 | $b = 8;
26 | $ansMultiply = $a * $b;
27 | echo "$a * $b = $ansMultiply";
28 |
29 | echo '
';
30 |
31 | // /
32 | $a = 180;
33 | $b = 3;
34 | $ansDivide = $a / $b;
35 | echo "$a / $b = $ansDivide";
36 |
37 | echo '
';
38 |
39 | // %
40 | $a = 13;
41 | $b = 2;
42 | $ansModulo = $a % $b;
43 | echo "$a % $b = $ansModulo";
44 |
45 | echo '
';
46 |
47 |
48 |
49 | // Comparison Operators:
50 |
51 |
52 | // ==
53 | echo '
54 | var_dump(5 == ';
55 | echo "'5');";
56 | echo '
';
57 | var_dump(5 == '5');
58 |
59 | echo '
';
60 |
61 | // ===
62 | echo '
63 | var_dump(5 === ';
64 | echo "'5');";
65 | echo '
';
66 | var_dump(5 === '5');
67 |
68 | echo '
';
69 |
70 | // NOT !=
71 | echo '
72 | var_dump(5 != ';
73 | echo "'5');";
74 | echo '
';
75 | var_dump(5 != '5');
76 |
77 | echo '
';
78 |
79 | // STRICT NOt !==
80 | echo "!==
";
81 | echo '
82 | var_dump(5 !== ';
83 | echo "'5');";
84 | echo '
';
85 | var_dump(5 !== '5');
86 |
87 | echo '
';
88 |
89 | $a = 2;
90 | $b = 3;
91 |
92 | // <
93 | echo '
94 | echo var_dump(2 < 3);
';
95 | var_dump(2 < 3);
96 |
97 | echo '
';
98 |
99 | // >
100 | echo '
101 | echo var_dump(2 > 3);
';
102 | var_dump(2 > 3);
103 |
104 | echo '
';
105 |
106 | // <=
107 | echo '
108 | echo var_dump(2 <= 2);
';
109 | var_dump(2 <= 3);
110 |
111 | echo '
';
112 |
113 | // >=
114 | echo '
115 | echo var_dump(2 >= 3);
';
116 | var_dump(2 >= 3);
117 |
118 | echo '
';
119 |
120 | // Logical Operators:
121 |
122 |
123 | // &&
124 | $a = 1;
125 | $b = 2;
126 | echo 'var_dump($a > 1 && $b > 1);
';
127 | var_dump($a > 1 && $b > 1);
128 | echo '
';
129 | if ($a > 1 && $b > 1) {
130 | echo 'both greater than 1';
131 | } else {
132 | echo 'both not greater than 1';
133 | }
134 |
135 |
136 | echo '
';
137 |
138 | // ||
139 | echo 'var_dump($a > 1 || $b > 1);
';
140 | var_dump($a > 1 || $b > 1);
141 | echo '
';
142 | if ($a > 1 || $b > 1) {
143 | echo 'at least one variable is greater than 1';
144 | } else {
145 | echo 'neither variable is greater than 1';
146 | }
147 |
148 | echo '
';
149 |
150 | // xor
151 | echo 'var_dump($a > 1 xor $b > 1)
';
152 | $a = 3;
153 | var_dump($a > 1 xor $b > 1);
154 | echo '
';
155 | if ($a > 1 xor $b > 1) {
156 | echo 'one of the variables is greater than 1';
157 | } else {
158 | echo 'both of the variables are greater than 1';
159 | }
160 |
161 | echo '
';
162 |
163 | // / !
164 | echo 'var_dump(!$q)
';
165 | $q = 0;
166 | var_dump(!$q);
167 | echo '
';
168 | if (!$q) {
169 | echo 'The variable is undefined or is equal to 0';
170 | } else {
171 | echo "variable q is equal to $q";
172 | }
173 |
174 |
--------------------------------------------------------------------------------
/phpinfo.php:
--------------------------------------------------------------------------------
1 | phpinfo();
--------------------------------------------------------------------------------
/print.php:
--------------------------------------------------------------------------------
1 |
';
11 |
12 | // print
13 | print "This one, however, uses 'print'. It only takes one arguments and always returns 1.";
14 |
15 | echo '
';
16 |
17 | // print_r
18 | $list = array ('car', 'bus', 'train');
19 | // prints complex values, like an array, with print_r
20 | print_r ($list);
--------------------------------------------------------------------------------
/strings.php:
--------------------------------------------------------------------------------
1 |
';
10 |
11 | // Print text string that interprets variables.
12 | $a = 'cats';
13 | $b = 'dogs';
14 | print "It's raining $a and $b.";
15 |
16 | // Use function to concatenate strings
17 | echo '
';
18 | $c = $a . ' and ' . $b;
19 | $str = "I don't prefer one over the other. I like both " . $c . '.';
20 | print $str;
21 |
22 | echo '
';
23 |
24 | // Use function to replace text, here params are 'search for', 'replace with', 'in this string'. CASE-SENSITIVE
25 | $strTwo = str_replace('like', "don't like", $str);
26 | echo $strTwo; // I don't prefer one over the other. I don't like both cats and dogs.
27 | echo '
';
28 | echo str_replace("I don't like both cats and dogs.", '', $strTwo);
29 |
30 | echo '
';
31 |
32 | // Replace part of a string, as above, but NOT CASE-SENSITIVE
33 | echo str_ireplace("DOn't LIKE", 'like', $strTwo);
34 |
35 | echo '
';
36 |
37 | // Repeat a string a number of times.
38 | $dorothysLine = "There's no place like home.";
39 | echo str_repeat($dorothysLine, 3);
40 |
41 | echo '
';
42 |
43 | // Find string length
44 | echo strlen($dorothysLine);
45 |
46 | echo '
';
47 |
48 | // Find position of first occurrence of string within string, CASE-SENSITIVE
49 | $linePlace = strpos($dorothysLine, 'place');
50 | echo $linePlace;
51 | echo '
';
52 | // and CASE-INSENSITIVE
53 | $linePlace = stripos($dorothysLine, 'NO');
54 | echo $linePlace;
55 |
56 | echo '
';
57 |
58 | // Capitalize text string
59 | echo strtoupper($dorothysLine);
60 |
61 | echo '
';
62 |
63 | // Change string to lower case
64 | echo strtolower('CAPITAL = LOUD!');
65 |
66 | echo '
';
67 |
68 | // Get a substring from a given position
69 | echo substr($dorothysLine, 22, 4); // home
70 |
71 |
--------------------------------------------------------------------------------
/types.php:
--------------------------------------------------------------------------------
1 | $myBool = true;
';
9 | var_dump($myBool);
10 |
11 | echo '
';
12 |
13 | //integer
14 | $myInt = 430;
15 | echo '$myInt = 430;
';
16 | var_dump($myInt);
17 |
18 | echo '
';
19 |
20 | //float
21 | $myFloat = 3.141592;
22 | echo '$myFloat = 3.141592;
';
23 | var_dump($myFloat);
24 |
25 |
26 | echo '
';
27 |
28 | // string
29 | $myString = 'Wow. A string.';
30 | echo '$myString = ';
31 | echo "'Wow. A string.';
";
32 | var_dump($myString);
33 |
34 | echo '
';
35 |
36 | // array
37 | $myArray = array ('tea', 'coffee', 'juice', 'water');
38 | echo '$myArray = ';
39 | echo "('tea', 'coffee', 'juice', 'water');
";
40 | print_r($myArray);
41 | echo '
';
42 | var_dump($myArray);
43 |
44 | echo '
';
45 |
46 | // object
47 | class home // declare taxi object / class
48 | {
49 | public $type = 'apartment';
50 | public $bedrooms = '3';
51 | public $renovated = false;
52 | public $naturalLight = true;
53 | public $balconies = 2;
54 | function describe_home()
55 | {
56 | echo "A $bedrooms-bedroom $type, with $balconies balconies.";
57 | }
58 | }
59 | print_r(new home);
60 |
61 | echo '
';
62 |
63 |
64 | // NULL
65 | $myVar = NULL;
66 | var_dump($myVar); // can't echo
67 |
--------------------------------------------------------------------------------