mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-03 21:37:52 +02:00
Initial commit for public repo
This commit is contained in:
26
code/arithmetic.php
Normal file
26
code/arithmetic.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
// Now that we know how to create variables, let's look at doing some math.
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
|
||||
// To add two values, use the plus symbol.
|
||||
$c = $a + $b;
|
||||
|
||||
// To subtract, use the minus symbol.
|
||||
$c = $b - $a;
|
||||
|
||||
// To multiply two values, use an asterisk.
|
||||
echo $a * $b;
|
||||
|
||||
// To divide values, use a forward slash.
|
||||
echo $b / $a;
|
||||
|
||||
// PHP uses the percent symbol for calculating the modulus of two numbers.
|
||||
// The modulus is calculated by dividing two numbers and returning the remainder of the result.
|
||||
// So, in this example, the value of $modulo will be 0.
|
||||
$modulo = 10 % 5;
|
||||
|
||||
// You can also use double asterisks to calculate a number to the power of another number.
|
||||
// The following statement will print 25.
|
||||
echo 5 ** 2;
|
26
code/arrays.php
Normal file
26
code/arrays.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
// There are two ways to create an array.
|
||||
// First, you can use the array construct to pass in values separated by commas
|
||||
// and it will return an array.
|
||||
$taskList = array('grocery store', 'change car oil');
|
||||
|
||||
// Second, you can surround the list in square brackets.
|
||||
// This style is the most common and recommended form
|
||||
// of creating an array.
|
||||
$groceryList = ['bread', 'milk', 'eggs'];
|
||||
|
||||
// PHP will automatically assign index keys for each value in an array
|
||||
// starting with 0. So, to access a value in an array you will
|
||||
// pass the key number into brackets after the variable name.
|
||||
echo $groceryList[0] . "\n";
|
||||
echo $groceryList[1] . "\n";
|
||||
|
||||
// You can also assign keys in an array using numbers or strings.
|
||||
// It is very common to create an array with string keys. The pattern
|
||||
// is called an associative array or a map.
|
||||
$car = ['make' => 'Toyota', 'model' => 'Camry'];
|
||||
|
||||
// To access the value in an associative array, just use the string key in brackets
|
||||
// after the variable name.
|
||||
echo $car['model'] . "\n";
|
25
code/basics.php
Normal file
25
code/basics.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
// In the tradition of our ancestors, let's start with a hello world program.
|
||||
// All PHP files must start with a <?php tag unless it is for a html template.
|
||||
// (We will learn about html templates later.)
|
||||
echo "Hello World!\n";
|
||||
|
||||
// There is a lot going on in this statement so let's work through it.
|
||||
|
||||
// First, the echo keyword tells PHP to output some text.
|
||||
echo 'I am some text';
|
||||
|
||||
// Second, PHP stores text in strings.
|
||||
|
||||
// To write a string, you surround letters with single or double quotes.
|
||||
// The difference between single quoted strings and double quoted strings
|
||||
// is that double quoted strings can hold special characters like \n which tells PHP to start a new line.
|
||||
'I am a string';
|
||||
"\nI am a string on a new line";
|
||||
|
||||
// Third, all lines of code in PHP must end in a semi-colon.
|
||||
echo "No semi-colon is a no-no\n";
|
||||
|
||||
// Using semi-colons means we can write multiple statements on one line.
|
||||
echo 'Hello'; echo " World\n";
|
35
code/boolean-logic.php
Normal file
35
code/boolean-logic.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
// Boolean logic is used to combine booleans to return another boolean.
|
||||
|
||||
// Using double ampersands tells PHP to check if both values are true.
|
||||
// If so, it will return true. If not, it will return false.
|
||||
$a = true;
|
||||
$b = true;
|
||||
$c = false;
|
||||
|
||||
// Returns true.
|
||||
$a && $b;
|
||||
// Returns false.
|
||||
$a && $c;
|
||||
|
||||
// Using two pipe characters checks if either value is true.
|
||||
// Then, it will return true. If both values are false, the PHP
|
||||
// returns false.
|
||||
$a = true;
|
||||
$b = false;
|
||||
$c = false;
|
||||
$d = true;
|
||||
|
||||
// Returns true.
|
||||
$a || $b;
|
||||
// Returns false.
|
||||
$b || $c;
|
||||
// Returns true.
|
||||
$a || $d;
|
||||
|
||||
// Using an exclamation point returns the value flipped.
|
||||
$d = true;
|
||||
|
||||
// Outputs false.
|
||||
echo !$d;
|
42
code/classes.php
Normal file
42
code/classes.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
// Classes allow you to define your own data types. All classes start with the
|
||||
// class keyword followed by the name of the class and opening and closing curly braces.
|
||||
class Car
|
||||
{
|
||||
}
|
||||
|
||||
// To create an instance of a class, you use the "new" keyword in front of the class name
|
||||
// with parentheses.
|
||||
$car = new Car();
|
||||
|
||||
// A class can define attributes and methods. An attribute is a piece of data
|
||||
// stored on the class instance. You can define an attribute by adding the
|
||||
// word "public" and a variable name inside the class definition.
|
||||
class Bicycle
|
||||
{
|
||||
public $color;
|
||||
}
|
||||
|
||||
// Then, when you create an instance of the class, you can set and use
|
||||
// the color attribute on the bicycle using "->".
|
||||
$bike = new Bicycle();
|
||||
$bike->color = 'Blue';
|
||||
echo $bike->color . "\n";
|
||||
|
||||
// A method is a function attached to the class. You can add a method
|
||||
// to a class by using the "public" keyword followed by the function. A method
|
||||
// can access the attributes and methods of the instance using the "$this" variable.
|
||||
class Tricycle
|
||||
{
|
||||
public $color;
|
||||
|
||||
public function echoColor()
|
||||
{
|
||||
echo $this->color . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
$bike = new Tricycle();
|
||||
$bike->color = 'Red';
|
||||
$bike->echoColor();
|
41
code/comparisons.php
Normal file
41
code/comparisons.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
// A boolean is a value that is always 0 or 1, yes or no, on or off.
|
||||
// In PHP, a boolean is represented by the words true and false.
|
||||
// While programming, you will often want to know if something is true or false.
|
||||
$a = true;
|
||||
$b = false;
|
||||
|
||||
// There are many constructs and functions that will return a boolean.
|
||||
// To start, let's look at comparisons.
|
||||
$one = 1;
|
||||
$two = 2;
|
||||
|
||||
// Double equals checks if two values are equal.
|
||||
// This statement will return false.
|
||||
$one == $two;
|
||||
|
||||
// An exclamation point and equal sign check if two values are not equal.
|
||||
// This statement will return true.
|
||||
$one != $two;
|
||||
|
||||
// You can use greater than and less than symbols to check for comparisons too.
|
||||
// This statement will return false.
|
||||
$one > $two;
|
||||
|
||||
// This statement will return true.
|
||||
$one < $two;
|
||||
|
||||
// If you combine a greater than or less than symbol with an equal,
|
||||
// it will check if the value is greater or less than or equal to another value.
|
||||
$one <= $two;
|
||||
$one >= $two;
|
||||
|
||||
// You can also check that two values are equal and of the same type
|
||||
// by using three equal signs.
|
||||
|
||||
// This returns true.
|
||||
1 == '1';
|
||||
|
||||
// This returns false.
|
||||
1 === '1';
|
70
code/conditionals.php
Normal file
70
code/conditionals.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
// When writing code, there will be times when you need to perform actions only under certain circumstances.
|
||||
// There are several ways to control execution in PHP.
|
||||
// We will start with an if statement.
|
||||
$animal = 'cow';
|
||||
if ($animal == 'cow') {
|
||||
echo "Moooooo.....\n";
|
||||
}
|
||||
|
||||
// All conditionals check to see if a statement evaluates to true or false.
|
||||
// In the case above, since $animal equals 'cow', the statement returns true and the contents of the if statement are executed.
|
||||
|
||||
|
||||
// An if statement can have multiple conditions chained together.
|
||||
// If the first if statement returns false, then PHP will check each elseif.
|
||||
// If none of the checks return true, then the else block will be executed.
|
||||
$animal = 'bird';
|
||||
if ($animal == 'dog') {
|
||||
echo "Woof! 🐶\n";
|
||||
} elseif ($animal == 'cat') {
|
||||
echo "Meow!? 🐱\n";
|
||||
} elseif ($animal == 'bird') {
|
||||
echo "Chirp! 🐦\n";
|
||||
} else {
|
||||
echo "I am not a dog, cat or bird\n";
|
||||
}
|
||||
|
||||
// An alternative to the if statement is the switch.
|
||||
// A switch statement has multiple cases to check if the value in parentheses equals something.
|
||||
// In this statement, since $food equals 'apples', the switch will echo "Eating an apple".
|
||||
// The default case will be run if no other case evaluates to true, like an else statement.
|
||||
$food = 'apples';
|
||||
switch ($food) {
|
||||
case 'apples':
|
||||
echo "Eating an apple\n";
|
||||
break;
|
||||
case 'oranges':
|
||||
echo "Eating an orange\n";
|
||||
break;
|
||||
case 'peaches':
|
||||
echo "Eating a peach\n";
|
||||
break;
|
||||
default:
|
||||
echo "No food, I am hungry\n";
|
||||
}
|
||||
|
||||
// Breaks are a special keyword that tell PHP to stop execution once a case passes.
|
||||
// If you do not use a break, PHP will continue to execute all following cases.
|
||||
// In this switch, both "Drinking water" and "Drinking tea" will be executed since there is no break in the 'water' case.
|
||||
$drink = 'water';
|
||||
switch ($drink) {
|
||||
case 'water':
|
||||
echo "Drinking water\n";
|
||||
case 'tea':
|
||||
echo "Drinking tea\n";
|
||||
break;
|
||||
}
|
||||
|
||||
// PHP also supports single line conditionals called a ternary.
|
||||
// In a ternary, the condition is followed by a question mark before the value that should be returned if the condition is true and then another colon and a value to return if the condition is false.
|
||||
$language = 'english';
|
||||
echo $language == 'spanish' ? "hola\n" : "hello\n";
|
||||
|
||||
// Lastly, there is another form of a ternary that checks if a value is set and then returns the value to the right of two question marks if value is null.
|
||||
echo $IDoNotExist ?? "Variable not set\n";
|
||||
|
||||
// You can also chain multiple checks in a row.
|
||||
$IExist = "Variable exists\n";
|
||||
echo $IDoNotExist ?? $IExist ?? "Neither variable is set\n";
|
46
code/functions.php
Normal file
46
code/functions.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
// A function allows you to store code under a name and then execute
|
||||
// that code later.
|
||||
|
||||
// A function always starts with the
|
||||
// function keyword followed by the name with parentheses and then
|
||||
// opening and closing curly braces around the code.
|
||||
function hello_world() {
|
||||
echo "hello world\n";
|
||||
}
|
||||
|
||||
// To call the function, use the function name with parentheses.
|
||||
hello_world();
|
||||
|
||||
// You can set up values to be passed into a function.
|
||||
// To do so, write variables in between the function parentheses.
|
||||
// Each one should be separated by a comma.
|
||||
function greet($firstname, $lastname) {
|
||||
echo "hello $firstname $lastname\n";
|
||||
}
|
||||
|
||||
// Then, you can pass in values when calling a function. In the greet function,
|
||||
// 'John' is assigned to $firstname and 'Smith' is assigned to
|
||||
// $lastname.
|
||||
greet('John', 'Smith');
|
||||
|
||||
// You can also return a value from a function. You can only
|
||||
// return a single value from a function.
|
||||
function capitalize($value) {
|
||||
return strtoupper($value);
|
||||
}
|
||||
|
||||
// When calling a function, it will output the return value which
|
||||
// you can load into a variable.
|
||||
$animal = capitalize('dog');
|
||||
echo "$animal\n";
|
||||
|
||||
// You can also create nameless functions called closures. Closures can be
|
||||
// stored in variables or passed into other functions.
|
||||
$sum = function ($a, $b) {
|
||||
return $a + $b;
|
||||
};
|
||||
|
||||
// You can execute a closure by putting parentheses after the variable.
|
||||
echo $sum(1, 2) . "\n";
|
57
code/loops.php
Normal file
57
code/loops.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
// A loop tells PHP to run a block of code more than once.
|
||||
// A classic loop is a while loop.
|
||||
// A "while" loop will continue to run the block of code as long as the value in parentheses is true.
|
||||
$num = 5;
|
||||
while ($num > 0) {
|
||||
echo "While loop $num\n";
|
||||
--$num;
|
||||
}
|
||||
|
||||
// A "do while" loop is similar to a "while" loop except it always runs at least
|
||||
// one iteration. In a classic "while" loop, no iterations may be executed if
|
||||
// the value in parentheses is false. In a "do while", the boolean check
|
||||
// is not done until after the execution of an iteration.
|
||||
$num = 0;
|
||||
do {
|
||||
echo "Do while $num\n";
|
||||
++$num;
|
||||
} while ($num < 5);
|
||||
|
||||
// "for" loops allow you to create a more concise while loop.
|
||||
// Inside the parentheses, the left section creates a variable before the loop
|
||||
// starts, the middle section is the check that is done at the beginning of each loop
|
||||
// and the third section is executed after each loop.
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
echo "For loop $i\n";
|
||||
}
|
||||
|
||||
// A "foreach" loop allows you to easily loop over an array.
|
||||
// An array is a list of data stored together.
|
||||
// The "as" keyword lets you assign a variable to the value
|
||||
// in the array for the current iteration of the loop.
|
||||
$set = [1, 2, 3, 4, 5];
|
||||
foreach ($set as $num) {
|
||||
echo "Array value $num\n";
|
||||
}
|
||||
|
||||
// In loops, you can use the keyword "break" to stop the loop execution
|
||||
// no matter how many more iterations should run.
|
||||
$values = ['one', 'two', 'three'];
|
||||
foreach ($values as $value) {
|
||||
if ($value === 'two') {
|
||||
break;
|
||||
}
|
||||
echo "Break $value\n";
|
||||
}
|
||||
|
||||
// The "continue" keyword stops executing the current loop iteration,
|
||||
// but then allows the loop to continue with other iterations.
|
||||
$values = ['one', 'skip', 'three'];
|
||||
foreach ($values as $value) {
|
||||
if ($value === 'skip') {
|
||||
continue;
|
||||
}
|
||||
echo "Continue $value\n";
|
||||
}
|
31
code/variables.php
Normal file
31
code/variables.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
// The variable is the basic building block of any programming language.
|
||||
// In PHP, all variables start with a dollar sign.
|
||||
$greeting;
|
||||
|
||||
// To set data in a variable, you put an equals sign after it and some data.
|
||||
$greeting = 'Hello World!';
|
||||
|
||||
// Once you create a variable, you can use it again in other commands and functions.
|
||||
echo $greeting;
|
||||
|
||||
// After the dollar sign, a PHP variable must have an alphabetic character or underscore. Also, variables are case sensitive.
|
||||
$_var = 'I am a variable with an underscore!';
|
||||
$Var = 'I am a variable with a capital letter!';
|
||||
$var = 'I am a new variable';
|
||||
|
||||
// Variables can hold many different types of data, but there are four simple ones you can try now.
|
||||
// An int is a number without a decimal place.
|
||||
// A float is a number with a decimal place.
|
||||
// A boolean can be two values: true or false.
|
||||
// Last, there is a string, a collection of characters.
|
||||
$int = 1;
|
||||
$float = 100.10;
|
||||
$bool = true;
|
||||
$string = 'I am a string';
|
||||
|
||||
// In other programming languages, you have to write what type of data the variable will contain.
|
||||
// PHP keeps it simple by allowing you to put any type of data in a variable, including already used variables.
|
||||
$number = 1;
|
||||
$number = 'one';
|
Reference in New Issue
Block a user