Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bradtraversy committed Mar 18, 2022
0 parents commit 1a92be3
Show file tree
Hide file tree
Showing 44 changed files with 1,534 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
script.txt
52 changes: 52 additions & 0 deletions 01_output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php // This is a php tag. If there is no html or other content below the php, we don't need to close the php tag.

/* ------- Outputting Content ------- */

// Echo - Output strings, numbers, html, etc
echo 'Hello';
echo 123;
echo '<h1>Hello</h1>';

// print - Similar to echo, but a bit slower
print 'Hello';

// print_r - Gives a bit more info. Can be used to print arrays
print_r('Hello');
print_r([1, 2, 3]);

// var_dump - Even more info like data type and length
var_dump('Hello');
var_dump([1, 2, 3]);

// Escaping characters with a backslash
echo "Is your name O\'reilly?";

/* ------------ Comments ------------ */

// This is a single line comment

/*
* This is a multi-line comment
*
* It can be used to comment out a block of code
*/

// If there is more content after the PHP, such as this file, you do need the ending tag. Otherwise you do not.
?>

<!-- You can output any HTML that you want within a .php file -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My PHP Website</title>
</head>
<body>
<!-- You can output PHP including variables, etc -->
<h1>Hello <?php echo 'Brad'; ?></h1>
<!-- You may only drop the semi-colon after a statement when the statement is followed immediately by a closing PHP tag ?>. -->
<h1>Hello <?= 'Brad' ?></h1>
</body>
</html>
56 changes: 56 additions & 0 deletions 02_variables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/* ----- Variables & Data Types ----- */

/* --------- PHP Data Types --------- */
/*
- String - A string is a series of characters surrounded by quotes
- Integer - Whole numbers
- Float - Decimal numbers
- Boolean - true or false
- Array - An array is a special variable, which can hold more than one value
- Object - A class
- NULL - Empty variable
- Resource - A special variable that holds a resource
*/

/* --------- Variable Rules --------- */
/*
- Variables must be prefixed with $
- Variables must start with a letter or the underscore character
- variables can't start with a number
- Variables can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variables are case-sensitive ($name and $NAME are two different variables)
*/

$name = 'Brad'; // String // Can be single or double quotes
$age = 40; // Integer
$hasKids = true; // Boolean
$cashOnHand = 10.5; //Float

var_dump($cashOnHand);

/* --- Adding variables to strings -- */

// Double quotes can be used to add variables to strings
echo "$name is $age years old";

// Better to do this
echo "${name} is ${age} years old";

// Concatenate Strings

echo '<h3>' . $name . ' is ' . $age . ' years old</h3>';

// Arithmetic Operators

echo 5 + 5;
echo 10 - 6;
echo 5 * 10;
echo 10 / 2;

// Constants - Cannot be changed
define('HOST', 'localhost');
define('USER', 'root');

var_dump(HOST);
83 changes: 83 additions & 0 deletions 03_arrays.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/* ----------- Arrrays ----------- */

/*
If you need to store multiple values, you can use arrays. Arrays hold "elements"
*/

// Simple array of numbers
$numbers = [1, 2, 3, 4, 5];

// Simple array of strings
$colors = ['red', 'blue', 'green'];

// Using the array function to create an array of numbers
$numbers = [1, 2, 3, 4, 5];

// Outputting values from an array
echo $numbers[0];
echo $numbers[3] + $numbers[4];

// We can use print_r or var_dump to see the contents of an array
var_dump($numbers);

/* ------ Associative Arrays ----- */

/*
Associative arrays allow us to use named keys to identify values.
*/

$colors = [
1 => 'red',
2 => 'green',
3 => 'blue',
];

// echo $colors[1];

// Strings as keys
$hex = [
'red' => '#f00',
'green' => '#0f0',
'blue' => '#00f',
];

echo $hex['red'];
var_dump($hex);

/* ---- Multi-dimensional arrays ---- */

/*
Multi-dimansional arrays are often used to store data in a table format.
*/

// Single person
$person1 = [
'first_name' => 'Brad',
'last_name' => 'Traversy',
'email' => '[email protected]',
];

// Array of people
$people = [
$person1, // [...$person1]
[
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '[email protected]',
],
[
'first_name' => 'Jane',
'last_name' => 'Doe',
'email' => '[email protected]',
],
];

var_dump($people);

// Accessing values in a multi-dimensional array
echo $people[0]['first_name'];
echo $people[2]['email'];

// Encode to JSON
var_dump(json_encode($people));
97 changes: 97 additions & 0 deletions 04_conditionals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/* ---- Conditionals & Operators ---- */

/* ------------ Operators ----------- */

/*
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
=== Identical to
!= Not equal to
!== Not identical to
*/

/* ---------- If Statements --------- */

/*
** If Statement Syntax
if (condition) {
// code to be executed if condition is true
}
*/

$age = 20;

if ($age >= 18) {
echo 'You are old enough to vote!';
} else {
echo 'Sorry, you are too young to vote.';
}

// Dates
// $today = date("F j, Y, g:i a");

$t = date('H');

if ($t < 12) {
echo 'Have a good morning!';
} elseif ($t < 17) {
echo 'Have a good afternoon!';
} else {
echo 'Have a good evening!';
}

// Check if an array is empty
// 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.

$posts = [];

if (!empty($posts[0])) {
echo $posts[0];
} else {
echo 'There are no posts';
}

/* -------- Ternary Operator -------- */
/*
The ternary operator is a shorthand if statement.
Ternary Syntax:
condition ? true : false;
*/

// Echo based on a condition (Same as above)
echo !empty($posts[0]) ? $posts[0] : 'There are no posts';

// Assign a variable based on a condition
$firstPost = !empty($posts[0]) ? $posts[0] : 'There are no posts';

$firstPost = !empty($posts[0]) ? $posts[0] : null;

// Null Coalescing Operator ?? (PHP 7.4)
// Will return null if $posts is empty
// Always returns first parameter, unless first parameter happens to be NULL
$firstPost = $posts[0] ?? null;

var_dump($firstPost);

/* -------- Switch Statements ------- */

$favcolor = 'red';

switch ($favcolor) {
case 'red':
echo 'Your favorite color is red!';
break;
case 'blue':
echo 'Your favorite color is blue!';
break;
case 'green':
echo 'Your favorite color is green!';
break;
default:
echo 'Your favorite color is not red, blue, nor green!';
}
86 changes: 86 additions & 0 deletions 05_loops.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/* -------- Loops & Iteration ------- */

/* ------------ For Loop ------------ */

/*
** For Loop Syntax
for (initialize; condition; increment) {
// code to be executed
}
*/

for ($x = 0; $x <= 10; $x++) {
echo "Number: $x <br>";
}

/* ------------ While Loop ------------ */

/*
** While Loop Syntax
while (condition) {
// code to be executed
}
*/

$x = 1;

while ($x <= 5) {
echo "Number: $x <br>";
$x++;
}

/* ---------- Do While Loop --------- */

/*
** Do While Loop Syntax
do {
// code to be executed
} while (condition);
do...while loop will always execute the block of code once, even if the condition is false.
*/

do {
echo "Number: $x <br>";
$x++;
} while ($x <= 5);

/* ---------- Foreach Loop ---------- */

/*
** Foreach Loop Syntax
foreach ($array as $value) {
// code to be executed
}
*/

// Loop through an array

$numbers = [1, 2, 3, 4, 5];

foreach ($numbers as $x) {
echo "Number: $x <br>";
}

// Use the indexes within the loop

$posts = ['Post One', 'Post Two', 'Post Three'];

foreach ($posts as $index => $post) {
echo "${index} - ${post} <br>";
}

// Use the keys within the loop for an associative array

$person = [
'first_name' => 'Brad',
'last_name' => 'Traversy',
'email' => '[email protected]',
];

// Get Keys
foreach ($person as $key => $val) {
echo "${key} - ${val} <br>";
}
Loading

0 comments on commit 1a92be3

Please sign in to comment.