├── .gitignore
├── 01_output.php
├── 02_variables.php
├── 03_arrays.php
├── 04_conditionals.php
├── 05_loops.php
├── 06_functions.php
├── 07_array_functions.php
├── 08_string_functions.php
├── 09_superglobals.php
├── 10_get_post.php
├── 11_sanitizing_inputs.php
├── 12_cookies.php
├── 13_sessions.php
├── 14_file_handling.php
├── 15_file_upload.php
├── 16_exceptions.php
├── 17_oop.php
├── _starter_files
├── 01_output.php
├── 02_variables.php
├── 03_arrays.php
├── 04_conditionals.php
├── 05_loops.php
├── 06_functions.php
├── 07_array_functions.php
├── 08_string_functions.php
├── 09_superglobals.php
├── 10_get_post.php
├── 11_sanitizing_inputs.php
├── 12_cookies.php
├── 13_sessions.php
├── 14_file_handling.php
├── 15_file_upload.php
├── 16_exceptions.php
├── 17_oop.php
├── feedback
│ ├── about.html
│ ├── feedback.html
│ ├── img
│ │ └── logo.png
│ └── index.html
└── package.json
├── extras
├── dashboard.php
├── logout.php
└── users.txt
├── feedback
├── about.php
├── config
│ └── database.php
├── feedback.php
├── img
│ └── logo.png
├── inc
│ ├── footer.php
│ └── header.php
└── index.php
├── package-lock.json
├── package.json
├── readme.md
└── uploads
└── Abstract-PNG-Images.png
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | script.txt
--------------------------------------------------------------------------------
/01_output.php:
--------------------------------------------------------------------------------
1 | Hello';
9 |
10 | // print - Similar to echo, but a bit slower
11 | print 'Hello';
12 |
13 | // print_r - Gives a bit more info. Can be used to print arrays
14 | print_r('Hello');
15 | print_r([1, 2, 3]);
16 |
17 | // var_dump - Even more info like data type and length
18 | var_dump('Hello');
19 | var_dump([1, 2, 3]);
20 |
21 | // Escaping characters with a backslash
22 | echo "Is your name O\'reilly?";
23 |
24 | /* ------------ Comments ------------ */
25 |
26 | // This is a single line comment
27 |
28 | /*
29 | * This is a multi-line comment
30 | *
31 | * It can be used to comment out a block of code
32 | */
33 |
34 | // If there is more content after the PHP, such as this file, you do need the ending tag. Otherwise you do not.
35 | ?>
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | My PHP Website
45 |
46 |
47 |
48 | Hello
49 |
50 | Hello = 'Brad' ?>
51 |
52 |
--------------------------------------------------------------------------------
/02_variables.php:
--------------------------------------------------------------------------------
1 | ' . $name . ' is ' . $age . ' years old';
44 |
45 | // Arithmetic Operators
46 |
47 | echo 5 + 5;
48 | echo 10 - 6;
49 | echo 5 * 10;
50 | echo 10 / 2;
51 |
52 | // Constants - Cannot be changed
53 | define('HOST', 'localhost');
54 | define('USER', 'root');
55 |
56 | var_dump(HOST);
57 |
--------------------------------------------------------------------------------
/03_arrays.php:
--------------------------------------------------------------------------------
1 | 'red',
32 | 2 => 'green',
33 | 3 => 'blue',
34 | ];
35 |
36 | // echo $colors[1];
37 |
38 | // Strings as keys
39 | $hex = [
40 | 'red' => '#f00',
41 | 'green' => '#0f0',
42 | 'blue' => '#00f',
43 | ];
44 |
45 | echo $hex['red'];
46 | var_dump($hex);
47 |
48 | /* ---- Multi-dimensional arrays ---- */
49 |
50 | /*
51 | Multi-dimansional arrays are often used to store data in a table format.
52 | */
53 |
54 | // Single person
55 | $person1 = [
56 | 'first_name' => 'Brad',
57 | 'last_name' => 'Traversy',
58 | 'email' => 'brad@gmail.com',
59 | ];
60 |
61 | // Array of people
62 | $people = [
63 | $person1, // [...$person1]
64 | [
65 | 'first_name' => 'John',
66 | 'last_name' => 'Doe',
67 | 'email' => 'john@gmail.com',
68 | ],
69 | [
70 | 'first_name' => 'Jane',
71 | 'last_name' => 'Doe',
72 | 'email' => 'jane@gmail.com',
73 | ],
74 | ];
75 |
76 | var_dump($people);
77 |
78 | // Accessing values in a multi-dimensional array
79 | echo $people[0]['first_name'];
80 | echo $people[2]['email'];
81 |
82 | // Encode to JSON
83 | var_dump(json_encode($people));
84 |
85 | // Decode from JSON
86 | $jsonobj = '{"first_name":"Brad","last_name": "Traversy","email":"brad@gmail.com"}';
87 | var_dump(json_decode($jsonobj));
88 |
--------------------------------------------------------------------------------
/04_conditionals.php:
--------------------------------------------------------------------------------
1 | Greater than
10 | <= Less than or equal to
11 | >= Greater than or equal to
12 | == Equal to
13 | === Identical to
14 | != Not equal to
15 | !== Not identical to
16 | */
17 |
18 | /* ---------- If Statements --------- */
19 |
20 | /*
21 | ** If Statement Syntax
22 | if (condition) {
23 | // code to be executed if condition is true
24 | }
25 | */
26 |
27 | $age = 20;
28 |
29 | if ($age >= 18) {
30 | echo 'You are old enough to vote!';
31 | } else {
32 | echo 'Sorry, you are too young to vote.';
33 | }
34 |
35 | // Dates
36 | // $today = date("F j, Y, g:i a");
37 |
38 | $t = date('H');
39 |
40 | if ($t < 12) {
41 | echo 'Have a good morning!';
42 | } elseif ($t < 17) {
43 | echo 'Have a good afternoon!';
44 | } else {
45 | echo 'Have a good evening!';
46 | }
47 |
48 | // Check if an array is empty
49 | // The isset() function will generate a warning or e-notice when the variable does not exists. The empty() function will not generate any warning or e-notice when the variable does not exists.
50 |
51 | $posts = [];
52 |
53 | if (!empty($posts[0])) {
54 | echo $posts[0];
55 | } else {
56 | echo 'There are no posts';
57 | }
58 |
59 | /* -------- Ternary Operator -------- */
60 | /*
61 | The ternary operator is a shorthand if statement.
62 | Ternary Syntax:
63 | condition ? true : false;
64 | */
65 |
66 | // Echo based on a condition (Same as above)
67 | echo !empty($posts[0]) ? $posts[0] : 'There are no posts';
68 |
69 | // Assign a variable based on a condition
70 | $firstPost = !empty($posts[0]) ? $posts[0] : 'There are no posts';
71 |
72 | $firstPost = !empty($posts[0]) ? $posts[0] : null;
73 |
74 | // Null Coalescing Operator ?? (PHP 7.4)
75 | // Will return null if $posts is empty
76 | // Always returns first parameter, unless first parameter happens to be NULL
77 | $firstPost = $posts[0] ?? null;
78 |
79 | var_dump($firstPost);
80 |
81 | /* -------- Switch Statements ------- */
82 |
83 | $favcolor = 'red';
84 |
85 | switch ($favcolor) {
86 | case 'red':
87 | echo 'Your favorite color is red!';
88 | break;
89 | case 'blue':
90 | echo 'Your favorite color is blue!';
91 | break;
92 | case 'green':
93 | echo 'Your favorite color is green!';
94 | break;
95 | default:
96 | echo 'Your favorite color is not red, blue, nor green!';
97 | }
98 |
--------------------------------------------------------------------------------
/05_loops.php:
--------------------------------------------------------------------------------
1 | ";
16 | }
17 |
18 | /* ------------ While Loop ------------ */
19 |
20 | /*
21 | ** While Loop Syntax
22 | while (condition) {
23 | // code to be executed
24 | }
25 | */
26 |
27 | $x = 1;
28 |
29 | while ($x <= 5) {
30 | echo "Number: $x ";
31 | $x++;
32 | }
33 |
34 | /* ---------- Do While Loop --------- */
35 |
36 | /*
37 | ** Do While Loop Syntax
38 | do {
39 | // code to be executed
40 | } while (condition);
41 |
42 | do...while loop will always execute the block of code once, even if the condition is false.
43 | */
44 |
45 | do {
46 | echo "Number: $x ";
47 | $x++;
48 | } while ($x <= 5);
49 |
50 | /* ---------- Foreach Loop ---------- */
51 |
52 | /*
53 | ** Foreach Loop Syntax
54 | foreach ($array as $value) {
55 | // code to be executed
56 | }
57 | */
58 |
59 | // Loop through an array
60 |
61 | $numbers = [1, 2, 3, 4, 5];
62 |
63 | foreach ($numbers as $x) {
64 | echo "Number: $x ";
65 | }
66 |
67 | // Use the indexes within the loop
68 |
69 | $posts = ['Post One', 'Post Two', 'Post Three'];
70 |
71 | foreach ($posts as $index => $post) {
72 | echo "${index} - ${post} ";
73 | }
74 |
75 | // Use the keys within the loop for an associative array
76 |
77 | $person = [
78 | 'first_name' => 'Brad',
79 | 'last_name' => 'Traversy',
80 | 'email' => 'brad@gmail.com',
81 | ];
82 |
83 | // Get Keys
84 | foreach ($person as $key => $val) {
85 | echo "${key} - ${val} ";
86 | }
87 |
--------------------------------------------------------------------------------
/06_functions.php:
--------------------------------------------------------------------------------
1 | $num1 * $num2;
56 |
57 | echo $multiply(5, 5);
58 |
--------------------------------------------------------------------------------
/07_array_functions.php:
--------------------------------------------------------------------------------
1 | $number < 10);
62 |
63 | // Array Reduce
64 | // "carry" holds the return value of the previous iteration
65 | $sum = array_reduce($numbers, fn($carry, $number) => $carry + $number);
66 | var_dump($sum);
67 |
--------------------------------------------------------------------------------
/08_string_functions.php:
--------------------------------------------------------------------------------
1 | Hello World';
51 | echo htmlentities($string2);
52 |
53 | // Formatted Strings - useful when you have outside data
54 | // Different specifiers for different data types
55 | printf('%s is a %s', 'Brad', 'nice guy');
56 | printf('1 + 1 = %f', 1 + 1); // float
57 |
--------------------------------------------------------------------------------
/09_superglobals.php:
--------------------------------------------------------------------------------
1 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | Document
28 |
29 |
30 |
31 | Host:
32 | Document Root:
33 | System Root:
34 | Server Name:
35 | Server Port:
36 | Current File Dir:
37 | Request URI:
38 | Server Software:
39 | Client Info:
40 | Remote Address:
41 | Remote Port:
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/10_get_post.php:
--------------------------------------------------------------------------------
1 | ' . $GET['username'] . '';
10 | echo '' . $_POST['username'] . ' ';
11 | } ?>
12 |
13 |
14 | Link
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/11_sanitizing_inputs.php:
--------------------------------------------------------------------------------
1 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/12_cookies.php:
--------------------------------------------------------------------------------
1 |
33 |
34 |
49 |
--------------------------------------------------------------------------------
/14_file_handling.php:
--------------------------------------------------------------------------------
1 | File uploaded!';
28 | } else {
29 | echo 'File too large!
';
30 | }
31 | } else {
32 | $message = 'Invalid file type!
';
33 | }
34 | } else {
35 | $message = 'Please choose a file
';
36 | }
37 | }
38 | ?>
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | File Upload
47 |
48 |
49 |
50 |
57 |
58 |
--------------------------------------------------------------------------------
/16_exceptions.php:
--------------------------------------------------------------------------------
1 | getMessage(), ' ';
24 | // }
25 |
26 | // Finally block is executed regardless of whether an exception is thrown or not
27 |
28 | try {
29 | echo inverse(5) . ' ';
30 | } catch (Exception $e) {
31 | echo 'Caught exception: ', $e->getMessage(), ' ';
32 | } finally {
33 | echo 'First finally ';
34 | }
35 |
36 | try {
37 | echo inverse(0) . ' ';
38 | } catch (Exception $e) {
39 | echo 'Caught exception: ', $e->getMessage(), ' ';
40 | } finally {
41 | echo "Second finally ";
42 | }
43 |
44 |
45 | echo 'Hello World';
--------------------------------------------------------------------------------
/17_oop.php:
--------------------------------------------------------------------------------
1 | name = $name;
23 | $this->email = $email;
24 | $this->password = $password;
25 | }
26 |
27 | // Methods are functions that belong to a class.
28 | // function setName() {
29 | // $this->name = $name;
30 | // }
31 |
32 | function getName() {
33 | return $this->name;
34 | }
35 |
36 | function login() {
37 | return "User $this->name is logged in.";
38 | }
39 |
40 | // Destructor is called when an object is destroyed or the end of the script.
41 | function __destruct() {
42 | echo "The user name is {$this->name}.";
43 | }
44 | }
45 |
46 | // Instantiate a new user
47 | $user1 = new User('Brad', 'brad@gmail.com', '123456');
48 | echo $user1->getName();
49 | echo $user1->login();
50 |
51 | // Add a value to a property
52 | // $user1->name = 'Brad';
53 |
54 | var_dump($user1);
55 | // echo $user1->name;
56 |
57 | /* ----------- Inheritence ---------- */
58 |
59 | /*
60 | Inheritence is the ability to create a new class from an existing class.
61 | It is achieved by creating a new class that extends an existing class.
62 | */
63 |
64 | class employee extends User {
65 | public function __construct($name, $email, $password, $title) {
66 | parent::__construct($name, $email, $password);
67 | $this->title = $title;
68 | }
69 |
70 | public function getTitle() {
71 | return $this->title;
72 | }
73 | }
74 |
75 | $employee1 = new employee('John','johndoe@gmail.com','123456','Manager');
76 | echo $employee1->getTitle();
--------------------------------------------------------------------------------
/_starter_files/01_output.php:
--------------------------------------------------------------------------------
1 | Greater than
10 | <= Less than or equal to
11 | >= Greater than or equal to
12 | == Equal to
13 | === Identical to
14 | != Not equal to
15 | !== Not identical to
16 | */
17 |
18 | /* ---------- If & If-Else Statements --------- */
19 |
20 | /*
21 | ** If Statement Syntax
22 | if (condition) {
23 | // code to be executed if condition is true
24 | }
25 | */
26 |
27 | /* -------- Ternary Operator -------- */
28 | /*
29 | The ternary operator is a shorthand if statement.
30 | Ternary Syntax:
31 | condition ? true : false;
32 | */
33 |
34 |
35 | /* -------- Switch Statements ------- */
36 |
--------------------------------------------------------------------------------
/_starter_files/05_loops.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Leave Feedback
9 |
10 |
11 |
12 |
31 |
32 |
33 |
34 |
35 |
About
36 |
37 |
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Inventore impedit totam porro iure reiciendis autem possimus sapiente, optio, exercitationem ipsum assumenda mollitia, recusandae expedita culpa ratione voluptatem esse quos quam?
38 |
39 |
40 |
41 |
42 | Copyright © 2022
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/_starter_files/feedback/feedback.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Leave Feedback
9 |
10 |
11 |
12 |
31 |
32 |
33 |
34 |
35 |
36 |
Feedback
37 |
38 |
39 |
40 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta molestias animi earum eos dolorem repellat a quibusdam, aperiam vero repellendus voluptatibus natus deserunt sed doloribus inventore, totam labore maxime perferendis!
41 |
42 |
43 |
44 |
45 |
46 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta molestias animi earum eos dolorem repellat a quibusdam, aperiam vero repellendus voluptatibus natus deserunt sed doloribus inventore, totam labore maxime perferendis!
47 |
48 |
49 |
50 |
51 |
52 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta molestias animi earum eos dolorem repellat a quibusdam, aperiam vero repellendus voluptatibus natus deserunt sed doloribus inventore, totam labore maxime perferendis!
53 |
54 |
55 |
56 |
57 |
58 |
59 | Copyright © 2022
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/_starter_files/feedback/img/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bradtraversy/php-crash/1aff0021d3e34ecea81d52157a2cb6b9b38394db/_starter_files/feedback/img/logo.png
--------------------------------------------------------------------------------
/_starter_files/feedback/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Leave Feedback
9 |
10 |
11 |
12 |
35 |
36 |
37 |
38 |
39 |
40 |
Feedback
41 |
Leave feedback for Traversy Media
42 |
59 |
60 |
61 |
62 |
63 | Copyright © 2022
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/_starter_files/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "php-crash",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/bradtraversy/php-crash.git"
12 | },
13 | "author": "",
14 | "license": "ISC",
15 | "bugs": {
16 | "url": "https://github.com/bradtraversy/php-crash/issues"
17 | },
18 | "homepage": "https://github.com/bradtraversy/php-crash#readme",
19 | "devDependencies": {
20 | "@prettier/plugin-php": "^0.18.3",
21 | "prettier": "^2.6.0"
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/extras/dashboard.php:
--------------------------------------------------------------------------------
1 | Welcome, ' . $_SESSION['username'] . '';
6 | echo 'Logout ';
7 | } else {
8 | echo 'Welcome, Guest ';
9 | echo 'Home ';
10 | }
11 |
--------------------------------------------------------------------------------
/extras/logout.php:
--------------------------------------------------------------------------------
1 |
2 |
3 | About
4 |
5 | Lorem ipsum dolor sit amet consectetur, adipisicing elit. Inventore impedit totam porro iure reiciendis autem possimus sapiente, optio, exercitationem ipsum assumenda mollitia, recusandae expedita culpa ratione voluptatem esse quos quam?
6 |
7 |
--------------------------------------------------------------------------------
/feedback/config/database.php:
--------------------------------------------------------------------------------
1 | connect_error) {
12 | die('Connection failed: ' . $conn->connect_error);
13 | }
14 |
15 | // echo 'Connected successfully';
16 |
--------------------------------------------------------------------------------
/feedback/feedback.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
10 | Past Feedback
11 |
12 |
13 | There is no feedback
14 |
15 |
16 |
17 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/feedback/img/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bradtraversy/php-crash/1aff0021d3e34ecea81d52157a2cb6b9b38394db/feedback/img/logo.png
--------------------------------------------------------------------------------
/feedback/inc/footer.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
10 |