diff --git a/php/README.md b/php/README.md new file mode 100644 index 0000000000..331d072dcf --- /dev/null +++ b/php/README.md @@ -0,0 +1,9 @@ +name: PHP + +description: A powerful language for web development + +color: A6D8FF + +icon: https://img.enkipro.com/3b93389af57a5f6359e50d207274df29.png +availableAspects: + - introduction \ No newline at end of file diff --git a/php/php-core/README.md b/php/php-core/README.md new file mode 100644 index 0000000000..eec73ccbae --- /dev/null +++ b/php/php-core/README.md @@ -0,0 +1,15 @@ +name: PHP Core + +description: Basic Syntax and Functionality + +core: true + +sections: + '0': + - intro-to-php + - variables-php + - data-types-php + - control-flow-i-php + - logical-operators-php + - looping-php + - more-data-types-php \ No newline at end of file diff --git a/php/php-core/control-flow-i-php/README.md b/php/php-core/control-flow-i-php/README.md new file mode 100644 index 0000000000..7260688c52 --- /dev/null +++ b/php/php-core/control-flow-i-php/README.md @@ -0,0 +1,12 @@ +name: Control Flow I + +description: Explore PHP's fundamental control flow. + +insights: + - if-statements-php + - comparison-operators-php + - if-else-statements-php + - nested-if-else-statements-php + +aspects: + - introduction diff --git a/php/php-core/control-flow-i-php/comparison-operators-php.md b/php/php-core/control-flow-i-php/comparison-operators-php.md new file mode 100644 index 0000000000..7a44958fb1 --- /dev/null +++ b/php/php-core/control-flow-i-php/comparison-operators-php.md @@ -0,0 +1,83 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# Comparison Operators + +--- + +## Content + +Comparison operators can be used to compare various types of data, including numbers, strings, and booleans: + + +| Operator | Name | +|:--------:|:---------------------------:| +| == | is equal to | +| != | is not equal to | +| < | is less than | +| <= | is less than or equal to | +| > | is greater than | +| >= | is greater than or equal to | + +```php +if ($num < 5) { // true + echo "$num is less than 5"; +} +// 1 is less than 5 + +if ($num != 2) { // true + echo "$num doesn't equal 2"; +} +// 1 doesn't equal 2 +``` + + +--- + +## Practice + +Finish the code to output "$num is greater than or equal to 3" if the condition evaluates to + +```php +??? = 5; +if ($num ??? 3) { + echo "$??? is greater than or equal to 3"; +} +``` + +- $num +- `>=` +- num +- 5 + + +--- + +## Revision + +Finish the code to output "$num is greater than or equal to 3" + +```php +??? = 5; +if ($num ??? 3) { + echo "$??? is greater than or equal to 3"; +} +``` + +- $num +- `>=` +- num +- 5 \ No newline at end of file diff --git a/php/php-core/control-flow-i-php/if-else-statements-php.md b/php/php-core/control-flow-i-php/if-else-statements-php.md new file mode 100644 index 0000000000..c77aa60197 --- /dev/null +++ b/php/php-core/control-flow-i-php/if-else-statements-php.md @@ -0,0 +1,72 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# If else Statements + +--- + +## Content + +We can extend the `if` statement to include an `else` clause, which will be executed if the condition is not met. + +```php +$num = 1; +if ($num == 0) { // false + echo "num equals 0"; +} else { // will execute if condition is false + echo "num doesn't equal 0"; +} + +// num doesn't equal 0 +``` + +--- + +## Practice + +What does the following code snippet print? + +```php +$x = 4; +if ($x <= 3) { + echo 'x is smaller'; +} else { + echo 'x is larger'; +} +``` + +??? + +- x is larger +- x is smaller + +--- + +## Revision + +What does the following code snippet print? + +```php +$x = 13; +if ($x == 13) { + echo 'true'; +} else { + echo 'false'; +} +``` + +- true +- false diff --git a/php/php-core/control-flow-i-php/if-statements-php.md b/php/php-core/control-flow-i-php/if-statements-php.md new file mode 100644 index 0000000000..9901245c37 --- /dev/null +++ b/php/php-core/control-flow-i-php/if-statements-php.md @@ -0,0 +1,74 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# If Statements + +--- + +## Content + +The flow of a PHP program depends on the order in which individual commands are executed. Control flow statements like if...else allow us to manage the order using conditions. + +```php +if (condition) { + // execute this code +} +``` + +The script will only execute the code if the condition has been met. + +```php +$num = 5; +if ($num > 0) { + echo "$num is a positive number"; +} +// Output: 5 is a positive number +``` + +If the condition `$num > 0` evaluates to `false`, this code would not output anything. + +--- +## Practice + +What does the following code snippet print? + +```php +$num = 5; +if ($num > 0) { + echo "$num is a positive number"; +} +``` + +??? + +- 5 is a positive number +- nothing + +--- + +## Revision + +What does the following code snippet print? + +```php +$num = 0; +if ($num > 0) { + echo "num is 0"; +} +``` + +- nothing +- num is 0 diff --git a/php/php-core/control-flow-i-php/nested-if-else-statements-php.md b/php/php-core/control-flow-i-php/nested-if-else-statements-php.md new file mode 100644 index 0000000000..389bdb2bde --- /dev/null +++ b/php/php-core/control-flow-i-php/nested-if-else-statements-php.md @@ -0,0 +1,98 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# Nested if else Statements + +--- + +## Content + +In PHP, we can have an `if-else` statement inside another `if-else` statement. This is called nesting. + +Nested `if-else` statements can be used to perform complex decision making. + +Here's a basic example: +```php +$age = 20; + +if($age >= 18){ + if($age <= 60){ + echo "You are an adult."; + } + else{ + echo "You are a senior."; + } +} +else{ + echo "You are not an adult."; +} + +// Output: "You are an adult." +``` + +--- + +## Practice + +What is the output of the following code? + +```php +$age = 70; + +if($age >= 18){ + if($age <= 60){ + echo "You are an adult."; + } + else{ + echo "You are a senior."; + } +} +else{ + echo "You are not an adult."; +} +// Output: ??? +``` + +- You are a senior. +- You are not an adult. +- You are an adult. + +--- + +## Revision + +What is the output of the following code? + +```php +$age = 15; + +if($age >= 18){ + if($age <= 60){ + echo "You are an adult."; + } + else{ + echo "You are a senior."; + } +} +else{ + echo "You are not an adult."; +} +// Output: ??? +``` + +- You are not an adult. +- You are a senior. +- You are an adult. \ No newline at end of file diff --git a/php/php-core/data-types-php/README.md b/php/php-core/data-types-php/README.md new file mode 100644 index 0000000000..122d97ac12 --- /dev/null +++ b/php/php-core/data-types-php/README.md @@ -0,0 +1,13 @@ +name: Data Types + +description: Learn about basic data types in PHP. + +insights: + - basic-data-types-php + - basic-data-types-php-ii + - basic-data-types-php-iii + - basic-data-types-php-iv + +aspects: + - workout + - deep \ No newline at end of file diff --git a/php/php-core/data-types-php/basic-data-types-php-ii.md b/php/php-core/data-types-php/basic-data-types-php-ii.md new file mode 100644 index 0000000000..ec74672d49 --- /dev/null +++ b/php/php-core/data-types-php/basic-data-types-php-ii.md @@ -0,0 +1,28 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know + +--- + +# Basic Data Types (integer, float) + +--- + +## Content + +Integers are whole numbers, such as 1, 2, 3, 99, 5555. +```php +$number = 10; +echo $number; +// 10 +``` + +### float + +Floating-point numbers (also refered to as **double**), are numbers that contain a decimal point. +```php +$number = 10.5; +echo $number; +// 10.5 +``` \ No newline at end of file diff --git a/php/php-core/data-types-php/basic-data-types-php-iii.md b/php/php-core/data-types-php/basic-data-types-php-iii.md new file mode 100644 index 0000000000..d99ded8797 --- /dev/null +++ b/php/php-core/data-types-php/basic-data-types-php-iii.md @@ -0,0 +1,39 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# Basic Data Types (string) + +--- + +## Content + +The `string` type represents a sequence of characters, such as "hello", "Enki", and so on. + +```php +$greeting = "Hello, world!"; +echo $greeting; +// Hello, world! +``` + + +--- +## Practice + +Complete the following code snippet by declaring a `string` variable named `name` and assigning it a value of `"Enki"`. + +```php +??? = "???"; +``` + +- `$name` +- `Enki` +- `name` +- `String` + diff --git a/php/php-core/data-types-php/basic-data-types-php-iv.md b/php/php-core/data-types-php/basic-data-types-php-iv.md new file mode 100644 index 0000000000..f7c708c1ab --- /dev/null +++ b/php/php-core/data-types-php/basic-data-types-php-iv.md @@ -0,0 +1,19 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know + +--- + +# Basic Data Types (bool) + +--- + +## Content + +Boolean values are either `true` or `false`: +```php +$true = true; +var_dump($true); +// bool(true) +``` diff --git a/php/php-core/data-types-php/basic-data-types-php.md b/php/php-core/data-types-php/basic-data-types-php.md new file mode 100644 index 0000000000..5b42fcd00d --- /dev/null +++ b/php/php-core/data-types-php/basic-data-types-php.md @@ -0,0 +1,37 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know + +--- + +# Basic Data Types in PHP + +--- + +## Content + +In PHP, variable are loosely typed, meaning that they are converted automatically as needed by the context where they are being used. + +**PHP** supports eight primitive types which can be grouped into three categories: + +### Scalar types + + +- `int` for storing integers, like `5`, `-10`. +- `float` (or double) for storing floating-point numbers, like 3.14159 or -0.001. +- `string` for storing sequences of characters, like "Hello, world!" or "Enki". +- `bool` for true or false. + +### Compound types + +- `array` for storing multiple values in a single variable. +- `object` for storing instances of classes. + +### Special types + + +- resource for storing references to functions and resources external to PHP. +- NULL for a variable with no value. + +> We will cover the scalar types in this workout. \ No newline at end of file diff --git a/php/php-core/intro-to-php/README.md b/php/php-core/intro-to-php/README.md new file mode 100644 index 0000000000..44b051658c --- /dev/null +++ b/php/php-core/intro-to-php/README.md @@ -0,0 +1,11 @@ +name: PHP Intro + +description: What is PHP? + +insights: + - what-is-php + - why-learn-php + - first-program-php + +aspects: + - workout \ No newline at end of file diff --git a/php/php-core/intro-to-php/first-program-php.md b/php/php-core/intro-to-php/first-program-php.md new file mode 100644 index 0000000000..e9d7f1d6d3 --- /dev/null +++ b/php/php-core/intro-to-php/first-program-php.md @@ -0,0 +1,99 @@ +--- +author: Stefan-Stojanovic + +type: normal + +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone + +--- + +# Let's Create Our First Program! + +--- + +## Content + +Ready for some code? + +It's time to create our first program! + +`"Hello, World!"` is a simple program that prints the text `"Hello, World!"` to the screen. It is often used as a starting point when learning a new programming language. + +In **PHP**, you can print text to the screen using the `echo` method. Here is an example: +```html + + +// Hello, World! +``` + +Once you have PHP installed[1], you can save your program to a script file with the `.php` extension, for instance `hello.php` and run it using this command: +```bash +php hello.php +``` + +You should see the text "Hello, World!" printed to the screen. + +--- +## Footnotes + +[1: Installing PHP] +To install **PHP**, you can follow these steps: + +### Windows + +Download the VC15 x64 Non Thread Safe version from the official [PHP website](https://windows.php.net/download/). Then, extract the zip file to a directory. + +### Linux + +**PHP** can be installed via the package manager. For example, on Ubuntu, you would use this command: +```bash +sudo apt install php. +``` + +### MacOs + +On macOS, **PHP** is preinstalled, but you can use Homebrew to install a more recent version. + +```bash +brew install php +``` + +Once the installation is complete, you can start using **PHP** by writing scripts and running them with the **PHP** command-line interface or with a web server that supports **PHP** (like Apache or Nginx). + +--- +## Practice + +Which function is used to print text to the screen in **PHP**? + +```php +???("Hello, World!") +``` + +- `echo` +- `print` +- `printout` +- `display` + +--- +## Revision + +Which command is used to run a script file called hello.php in **PHP**? + +```php +??? hello.php +``` + +- `php` +- `run` +- `execute` +- `eval` diff --git a/php/php-core/intro-to-php/what-is-php.md b/php/php-core/intro-to-php/what-is-php.md new file mode 100644 index 0000000000..207f0a751a --- /dev/null +++ b/php/php-core/intro-to-php/what-is-php.md @@ -0,0 +1,57 @@ +--- +author: Stefan-Stojanovic + +type: normal + +category: discussion + +--- + +# What is PHP? + +--- + +## Content + +**PHP** is a general-purpose scripting language used by over 75% of websites! [1] + +**PHP** code is executed on the server, and the plain HTML result is sent back to the client's browser.[2] + +A unique feature of **PHP** is that it can be embedded directly within HTML: + +```html + + + + +

Welcome to My Home Page

+ + + + + +``` + +> Don't worry if you do not understand the code, this is just an example to get you confortable with the syntax. We will dive deeper into it later on. + + +> 💬 Why are you interested in **PHP**? +> +> Leave a comment or view some of the other comments for inspiration before moving on. + +--- + +## Footnotes + +[1: PHP Usage] + +PHP is estimated to be used by [76.8% of all websites](https://w3techs.com/technologies/details/pl-php) + +[2: Server vs Client] + +The server is the computer which hosts and manages your web app. +The client is the user's device that is looking to use the web app. + +When a user wants to use your web app, their browser makes a requests to the server, the server will then execute the PHP code and return to the client the resulting website \ No newline at end of file diff --git a/php/php-core/intro-to-php/why-learn-php.md b/php/php-core/intro-to-php/why-learn-php.md new file mode 100644 index 0000000000..3b952703c5 --- /dev/null +++ b/php/php-core/intro-to-php/why-learn-php.md @@ -0,0 +1,37 @@ +--- +author: Stefan-Stojanovic + +type: normal + +category: must-know + +--- + +# Why It's Good to Learn PHP? + +--- + +## Content + +There are several reasons why learning **PHP** can be beneficial: + +- **PHP** is the primary language used in WordPress development, which powers over **40% of all websites**! [1] +- 🌐 It is fully embedded within **HTML**, making it a great language for creating dynamic web pages. +- **PHP** has extensive online documentation and a large community, making it easier to find help and resources. +- 💰💰💰 The average annual salary for a **PHP** developer is $90,000 dollars.[2] + +--- + +## Footnotes + +[1: Wordpress Usage] + +Wordpress is a content-management system(CMS) for people to build and publish websites. + +It is estimated to be used by [43% of all websites](https://w3techs.com/technologies/details/cm-wordpress) + +[2: PHP salary] + +Per Zippia, the estimated average PHP Developer Salary is [$91,010 as of October 2023](https://www.zippia.com/php-developer-jobs/salary/) + + diff --git a/php/php-core/logical-operators-php/README.md b/php/php-core/logical-operators-php/README.md new file mode 100644 index 0000000000..23cc12e1bd --- /dev/null +++ b/php/php-core/logical-operators-php/README.md @@ -0,0 +1,14 @@ +name: Logical Operators + +description: Learn Boolean Algebra in PHP + +insights: + - what-are-logical-operators-php + - and-php + - and-precedence-php + - or-php + - not-php + - xor-php + +aspects: + - introduction diff --git a/php/php-core/logical-operators-php/and-php.md b/php/php-core/logical-operators-php/and-php.md new file mode 100644 index 0000000000..5424891835 --- /dev/null +++ b/php/php-core/logical-operators-php/and-php.md @@ -0,0 +1,35 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +--- + +# AND (and, &&) Operators + +--- + +## Content + +The `and` and `&&` operators in **PHP** return true only if both operands are true. Otherwise, they return false. + +Here are some examples: + +```php +$a = true; +$b = true; + +if($a && $b) { + echo "Both a and b are true."; +} +// Output: "Both a and b are true." +if($a and $b) { + echo "Both a and b are true."; +} +// Output: "Both a and b are true." +``` + +Note that there is a difference in precedence between `and` and `&&`. We will look at an example in the next insight. + +--- \ No newline at end of file diff --git a/php/php-core/logical-operators-php/and-precedence-php.md b/php/php-core/logical-operators-php/and-precedence-php.md new file mode 100644 index 0000000000..fd6deb12f1 --- /dev/null +++ b/php/php-core/logical-operators-php/and-precedence-php.md @@ -0,0 +1,94 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +links: + - >- + [PHP documentation](https://www.php.net/manual/en/language.operators.precedence.php){documentation} + +--- + +# (and, &&) Precedence + +--- + +## Content + + +In PHP, `&&` takes precedence over `and`. When mixed, this order matters. + +Let's look at an example: + +```php +$a = true; +$b = false; +$c = true; + +if($a and $b && $c) { + // Interpreted as: ($a and $b) && $c + echo "Won't print."; +} else { + echo "Will print."; +} + +if($a && $b and $c) { + // Interpreted as: $a && ($b and $c) + echo "Won't print."; +} else { + echo "Will print."; +} +``` +For clarity, use one operator type or add parentheses. + +--- + +## Practice + +Complete the following code so that the output is "Both a and b are true.": + +```php +$a = ???; +$b = true; + +if($a ??? $b) { + echo ???; +} else { + echo "Either a or b is false."; +} +// Output: "Both a and b are true." +``` + +- true +- && +- "Both a and b are true." + +--- + +## Revision + +What is the output of the following code? + +```php +$a = false; +$b = true; + +if($a && $b) { + echo "Both a and b are true."; +} else { + echo "Either a or b is false."; +} +// Output: ??? +``` + +- "Either a or b is false." +- "Both a and b are true." \ No newline at end of file diff --git a/php/php-core/logical-operators-php/not-php.md b/php/php-core/logical-operators-php/not-php.md new file mode 100644 index 0000000000..a4bd3248ca --- /dev/null +++ b/php/php-core/logical-operators-php/not-php.md @@ -0,0 +1,76 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# NOT (!) Operator + +--- + +## Content + +The `!` operator returns the inverse of the operand's boolean value. If the operand is true, it returns false, and vice versa. + +```php +bool a = true; + +if(!a) { + printf("a is false.") +} else { + printf("a is true.") +} +// Output: "a is true." +``` + +--- + +## Practice + +Finish the code to output 'a is false'? + +```php +$a = ???; + +if(???) { + echo "a is false"; +} else { + echo "a is true"; +} +// Output: a is false +``` + +- false +- `!$a` +- `!$b` +- true + +--- + +## Revision + +What is the output of the following code? + +```php +$a = true; + +if(!$a) { + echo "a is false"; +} else { + echo "a is true"; +} +// Output: a is ??? +``` + +- true +- false \ No newline at end of file diff --git a/php/php-core/logical-operators-php/or-php.md b/php/php-core/logical-operators-php/or-php.md new file mode 100644 index 0000000000..3f6422848d --- /dev/null +++ b/php/php-core/logical-operators-php/or-php.md @@ -0,0 +1,34 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +--- + +# OR (||, or) operator + +--- + +## Content + +The `or` and `||` operators in PHP return true if at least one of the operands is true. If both operands are false, they return false. + +Here are some examples: +```php +$a = true; +$b = false; + +if($a || $b) { + echo "At least one of a or b is true."; +} +// Output: "At least one of a or b is true." +if($a or $b) { + echo "At least one of a or b is true."; +} +// Output: "At least one of a or b is true." +``` + +Just like the `and` and `&&` operators, `or` and `||` have different precedence. The `||` operator has a higher precedence than `or`. + +--- diff --git a/php/php-core/logical-operators-php/what-are-logical-operators-php.md b/php/php-core/logical-operators-php/what-are-logical-operators-php.md new file mode 100644 index 0000000000..74be06860f --- /dev/null +++ b/php/php-core/logical-operators-php/what-are-logical-operators-php.md @@ -0,0 +1,30 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know + +--- + +# Logical Operators + +--- + +## Content + +Logical operators in PHP are used to perform logical operations on the operands. + +They mainly involve boolean values and they return a boolean value as a result. + +The logical operators provided by PHP include: + +| Operator | Name | +|:--------:|:----:| +| `and` | AND | +| `or` | OR | +| `xor` | XOR | +| `!` | NOT | +| `&&` | AND | +| `⎮⎮` | OR | + diff --git a/php/php-core/logical-operators-php/xor-php.md b/php/php-core/logical-operators-php/xor-php.md new file mode 100644 index 0000000000..9d32151385 --- /dev/null +++ b/php/php-core/logical-operators-php/xor-php.md @@ -0,0 +1,78 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# XOR Operator + +--- + +## Content + +In PHP, the `xor` operator is a logical operator that returns true if exactly one of the two operands (conditions) is true, otherwise it returns false. + +Here is an example: +```php +$a = true; +$b = false; + +if($a xor $b) { + echo "Exactly one of a or b is true."; +} else { + echo "Both a and b are either true or false."; +} +// Output: "Exactly one of a or b is true." +``` + +--- + +## Practice + +What is the output of the following code? + +```php +$a = true; +$b = true; + +if($a xor $b) { + echo "Exactly one of a or b is true"; +} else { + echo "Both a and b are either true or false"; +} +// Output: ??? +``` + +- Exactly one of a or b is true +- Both a and b are either true or false + +--- + +## Revision + +What is the output of the following code? + +```php +$a = false; +$b = false; + +if($a xor $b) { + echo "Exactly one of a or b is true"; +} else { + echo "Both a and b are either true or false"; +} +// Output: ??? +``` + +- Exactly one of a or b is true +- Both a and b are either true or false \ No newline at end of file diff --git a/php/php-core/looping-php/README.md b/php/php-core/looping-php/README.md new file mode 100644 index 0000000000..aa5f3140e0 --- /dev/null +++ b/php/php-core/looping-php/README.md @@ -0,0 +1,13 @@ +name: Looping +description: How to loop +insights: + - what-are-loops-php + - for-loops-php + - while-loops-php + - do-while-loops-php + - for-each-loops-php + - for-each-loops-php-ii + +aspects: + - introduction + diff --git a/php/php-core/looping-php/do-while-loops-php.md b/php/php-core/looping-php/do-while-loops-php.md new file mode 100644 index 0000000000..ab5a5c1b22 --- /dev/null +++ b/php/php-core/looping-php/do-while-loops-php.md @@ -0,0 +1,72 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# do...while Loops + +--- + +## Content + +The `do...while` loop will execute the block of code once, and then repeat the loop as long as a specified condition is true. + +```php +$i = 0; +do { + echo $i; + $i++; +} while ($i < 3); +// Output: 012 +``` + +In the example, the loop will start at 0 `$i = 0`, continue as long as `$i` is less than 3 `$i < 3`, and `$i` will increase by 1 each time the loop runs `$i++`. + + + +--- +## Practice + +Fill in the blanks to output the numbers 1 through 5: + +```php +$i = 1; +do { + echo $i; + $i++; +} ??? ($i <= ???); +``` + +- while +- 5 +- do +- for + +--- +## Revision + + +Fill in the blanks to output the numbers 5 through 10: + +```php +$i = ???; +do { + echo $i; + $i++; +} ??? ($i <= ???); +``` + +- 5 +- while +- 10 +- do +- for diff --git a/php/php-core/looping-php/for-each-loops-php-ii.md b/php/php-core/looping-php/for-each-loops-php-ii.md new file mode 100644 index 0000000000..a4a769239a --- /dev/null +++ b/php/php-core/looping-php/for-each-loops-php-ii.md @@ -0,0 +1,70 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# foreach Loop + +--- + +## Content + +It's also possible to access both the key and the value of the current item: +```php +$array = array("a" => "Apple", "b" => "Banana", "c" => "Cherry"); + +foreach ($array as $key => $value) { + echo "$key: $value\n"; +} +// Output: +// a: Apple +// b: Banana +// c: Cherry +``` + +--- + +## Practice + +What is the output of the following code? + +```php +$array = array(10, 20, 30); + +foreach ($array as $value) { + echo $value; +} +// Output: ??? +``` + +- 102030 +- 1020 +- 123 +- 302010 + +--- +## Revision + +Fill in the blanks to output the values of the array: +```php +$array = array("Red", "Green", "Blue"); + +foreach ($array as $???) { + echo $value; +} +// Output: RedGreenBlue +``` + +- value +- key +- index +- part \ No newline at end of file diff --git a/php/php-core/looping-php/for-each-loops-php.md b/php/php-core/looping-php/for-each-loops-php.md new file mode 100644 index 0000000000..8e32122af2 --- /dev/null +++ b/php/php-core/looping-php/for-each-loops-php.md @@ -0,0 +1,25 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +--- + +# foreach Loop + +--- + +## Content + +A `foreach` loop is used to iterate over arrays. + +Here's an example: +```php +$array = array(1, 2, 3, 4, 5); + +foreach ($array as $value) { + echo $value; +} +// Output: 12345 +``` + +In this case, the `foreach` loop is iterating over each value in the `$array`. For each iteration, `$value` is assigned the current item's value, and the code inside the loop is executed. diff --git a/php/php-core/looping-php/for-loops-php.md b/php/php-core/looping-php/for-loops-php.md new file mode 100644 index 0000000000..08028d4538 --- /dev/null +++ b/php/php-core/looping-php/for-loops-php.md @@ -0,0 +1,65 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# for loops + +--- +## Content + +In PHP, the `for` loop is used when you know in advance how many times the script should run. It consists of three parts: the initialization, the condition, and the increment/decrement. + +Example: +```php +for($i = 0; $i < 5; $i++){ + echo $i; +} +// Output: 01234 +``` + +In the example, the loop starts at 0 `$i = 0`, and continues as long as `$i` is less than 5 `$i < 5`, and `$i` will increase by 1 each time the loop runs `$i++`. + + +--- +## Practice + +Fill in the blank to output the numbers 1 through 5: + +```php +for($i = 1; $i <= ???; $i++){ + echo $i; +} +``` + +- 5 +- 4 +- 3 +- 6 + +--- +## Revision + +What is the output of the following code? + +```php +for($i = 1; $i <= 3; $i++){ + echo $i; +} + +// Output: ??? +``` + +- 123 +- 0123 +- 012 +- 23 \ No newline at end of file diff --git a/php/php-core/looping-php/what-are-loops-php.md b/php/php-core/looping-php/what-are-loops-php.md new file mode 100644 index 0000000000..09cc727148 --- /dev/null +++ b/php/php-core/looping-php/what-are-loops-php.md @@ -0,0 +1,20 @@ +--- +author: Stefan-Stojanovic + +type: normal + +category: must-know + +--- + +# What Are Loops? + +--- + +## Content + +In **PHP**, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met. **PHP** supports several types of loops: +- `for` +- `while` +- `do-while` +- `foreach` \ No newline at end of file diff --git a/php/php-core/looping-php/while-loops-php.md b/php/php-core/looping-php/while-loops-php.md new file mode 100644 index 0000000000..0e5710c764 --- /dev/null +++ b/php/php-core/looping-php/while-loops-php.md @@ -0,0 +1,73 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# while Loops + + +--- + +## Content + +The `while` loop continues to execute a block of code as long as a specified condition is `true`. + +Example: +```php +$i = 0; +while($i < 5){ + echo $i; + $i++; +} +// Output: 01234 +``` + +In the example above, the loop will start at 0 `$i = 0`, continue as long as `$i` is less than 5 `$i < 5`, and `$i` will increase by 1 each time the loop runs `$i++`. + + +--- +## Practice + +Fill in the blank to output the numbers 1 to 10: + +```php +$i = 1; +while($i <= ???){ + echo $i; + $i++; +} +``` + +- 10 +- 11 +- 5 +- 6 + +--- +## Revision + +What is the output of the following code? + +```php +$i = 1; +while($i <= 3){ + echo $i; + $i++; +} + +// Output: ??? +``` + +- 123 +- 23 +- 1234 +- 0123 diff --git a/php/php-core/more-data-types-php/README.md b/php/php-core/more-data-types-php/README.md new file mode 100644 index 0000000000..ecce04d1eb --- /dev/null +++ b/php/php-core/more-data-types-php/README.md @@ -0,0 +1,12 @@ +name: More Data Types + +description: Compound and Special Data + +insights: + - arrays-php + - arrays-as-dicts-php + - array-functions-php + - arrays-functions-php-ii + +aspects: + - introduction diff --git a/php/php-core/more-data-types-php/array-functions-php.md b/php/php-core/more-data-types-php/array-functions-php.md new file mode 100644 index 0000000000..e939631fea --- /dev/null +++ b/php/php-core/more-data-types-php/array-functions-php.md @@ -0,0 +1,34 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know + +--- + +# Array Functions + +--- + +## Content + +Some of the most important array functions in **PHP** are: + +- `count()`: returns the number of elements in an array. +- `array_push()`: pushes one or more elements onto the end of an array. +- `array_pop()`: pops and returns the last value of the array, shortening the array by one element. + +```php +$numbers = array(2, 3, 1); + +echo "Count: " . count($numbers) . "\n"; +// Output: Count: 3 + +// Add more elements +array_push($numbers, 4, 5, 6); + +// Remove the last element of the array +$lastElement = array_pop($numbers); +``` + diff --git a/php/php-core/more-data-types-php/arrays-as-dicts-php.md b/php/php-core/more-data-types-php/arrays-as-dicts-php.md new file mode 100644 index 0000000000..3869c0a72e --- /dev/null +++ b/php/php-core/more-data-types-php/arrays-as-dicts-php.md @@ -0,0 +1,44 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +--- + +# Arrays as Dictionaries + +--- + +## Content + + +Arrays in PHP can also be used as dicitonaries, where you can assign keys to the values: +```php +$numbers = array( + 'one' => 1, + 'two' => 2, + 'three' => 3, + 'four' => 4, + 'five' => 5 +); +``` + +For outputting the contents of the whole array, we can use the `print_r()` function: +```php +print_r($numbers); +``` + +Which gives us this output: +```php +Array +( + [one] => 1 + [two] => 2 + [three] => 3 + [four] => 4 + [five] => 5 +) +``` + + diff --git a/php/php-core/more-data-types-php/arrays-functions-php-ii.md b/php/php-core/more-data-types-php/arrays-functions-php-ii.md new file mode 100644 index 0000000000..4651e95a3e --- /dev/null +++ b/php/php-core/more-data-types-php/arrays-functions-php-ii.md @@ -0,0 +1,32 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know + +--- + +# Array Sorting + +--- + +## Content + +**PHP** also includes built-in functions for sorting arrays: + +- `sort()`: sorts the elements of an array in ascending order. +- `rsort()`: sorts the elements of an array in descending order. + +```php +$numbers = array(2, 3, 1); + +sort($numbers); +echo "Sorted: " . implode(", ", $numbers) . "\n"; +// Output: Sorted: 1, 2, 3 + +rsort($numbers); +echo "Reversed: " . implode(", ", $numbers) . "\n"; +// Output: Reversed: 3, 2, 1 +``` + diff --git a/php/php-core/more-data-types-php/arrays-php.md b/php/php-core/more-data-types-php/arrays-php.md new file mode 100644 index 0000000000..e8b0ebc54e --- /dev/null +++ b/php/php-core/more-data-types-php/arrays-php.md @@ -0,0 +1,67 @@ +--- +author: Stefan-Stojanovic +tags: + - introduction +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# Arrays + +--- + +## Content + +An array is a special variable that can hold multiple values at a time. + +Here's how you define an array that holds 5 integers in PHP: +```php +$numbers = array(1, 2, 3, 4, 5); +``` + +To access an element in the array, use the index of that element (keep in mind that indexing starts at 0): + +```php +echo $numbers[0]; +// Output: 1 +``` + +--- + +## Practice + +Fill in the code to create an array of integers called `nums` that hold numbers from 1 through 5: + +```php +??? = ???(1, 2, 3, 4, 5); +``` + + +- $nums +- array +- $arr +- nums + +--- + +## Revision + +Fill in the code to output the number 7 from the `numbers` array using its index: +```php +$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +echo $numbers[???]; +// Output: 7 +``` + +- 6 +- 7 +- 0 +- 1 \ No newline at end of file diff --git a/php/php-core/variables-php/README.md b/php/php-core/variables-php/README.md new file mode 100644 index 0000000000..754f7a1e72 --- /dev/null +++ b/php/php-core/variables-php/README.md @@ -0,0 +1,13 @@ +name: Variables + +description: Learn about variables in PHP. + +insights: + - creating-and-storing-variables-in-php + - creating-and-storing-variables-in-php-ii + - assignment-operators-php + - printing-variables-in-php + +aspects: + - workout + - deep \ No newline at end of file diff --git a/php/php-core/variables-php/assignment-operators-php.md b/php/php-core/variables-php/assignment-operators-php.md new file mode 100644 index 0000000000..7494b2a8cb --- /dev/null +++ b/php/php-core/variables-php/assignment-operators-php.md @@ -0,0 +1,63 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone + +--- + +# Assignment Operators in PHP + +--- + +## Content + +In **PHP**, assignment operators are used to assign values to variables. + +Other than `=`, **PHP** also includes compound assignment operators such as `+=`, `-=`, `*=`, and `/=`. + +```php +// equivalent to $x = $x + 5 +$x += 5; +echo $x; +// Outputs: 12 + +// equivalent to $y = $y * 2 +$y *= 2; +echo $y; +// Outputs: 26 +``` + +--- +## Practice + +Which operator is used to assign a value to a variable in **PHP**? + +??? + +- `=` +- `==` +- `->` +- `>` + +--- +## Revision + +Which of these variables uses the recommended way of assigning values in **PHP**? + +```php +x = 2; +$y = 10; +``` + +??? + +- y +- x diff --git a/php/php-core/variables-php/creating-and-storing-variables-in-php-ii.md b/php/php-core/variables-php/creating-and-storing-variables-in-php-ii.md new file mode 100644 index 0000000000..55465df116 --- /dev/null +++ b/php/php-core/variables-php/creating-and-storing-variables-in-php-ii.md @@ -0,0 +1,70 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + - type-in-the-gap + context: standalone + +--- + +# Creating and Storing Variables(ii) + +--- + +## Content + + +You can also create variables using other variables: +```php +$z = $x + $y; +echo $z; +// Outputs: 15.0 +``` + +Note that variables are case-sensitive. These are three different variables: +```php +$number = 1 +$nUmber = 1 +$numbeR = 1 +``` + +--- +## Practice + +Which of the following is the correct way to create a variable in **PHP**? + +??? + +- `$x = 5;` +- `x = 5;` +- `@y = 10.0;` +- `*z = 3;` + + +--- +## Revision + +Finish the code to create a new variable called `c` by summing the values of the `a` and `b` variables. Print the new variable. +```php +$a = 3; +$b = 7; + +??? = $a ??? $b; + +??? $c; +// 10 +``` + +- `$c` +- `+` +- `echo` +- `-` +- `plus` +- `print` diff --git a/php/php-core/variables-php/creating-and-storing-variables-in-php.md b/php/php-core/variables-php/creating-and-storing-variables-in-php.md new file mode 100644 index 0000000000..9d560c58d7 --- /dev/null +++ b/php/php-core/variables-php/creating-and-storing-variables-in-php.md @@ -0,0 +1,27 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +--- + +# Creating and Storing Variables + +--- + +## Content + +Variables are crucial in programming. They are named locations in memory that store a value, which can be later retrieved and used. They allow us to store and manipulate data in a program. + +In **PHP**, variables are declared with a dollar sign `$` and do not require a type to be specified: +```php +$x = 5; +$y = 10.0; +``` + +To print the value of a variable we use `echo`: +```php +echo $x; +// Outputs: 5 +echo $y; +// Outputs: 10.0 +``` diff --git a/php/php-core/variables-php/printing-variables-in-php.md b/php/php-core/variables-php/printing-variables-in-php.md new file mode 100644 index 0000000000..0b72a66ff7 --- /dev/null +++ b/php/php-core/variables-php/printing-variables-in-php.md @@ -0,0 +1,63 @@ +--- +author: Stefan-Stojanovic +type: normal +category: must-know +practiceQuestion: + formats: + - fill-in-the-gap + context: standalone +revisionQuestion: + formats: + - fill-in-the-gap + context: standalone +--- + +# Printing + +--- + +## Content + +If you want to print[1] a message and move to a new line, you can use the newline character `\n`. + +**Note** that the newline character only works in double-quoted strings: +```php +echo "Hello, world!\n"; +``` + +To print the type of variable, we can use `var_dumb`: +```php +$name = "Stefan"; +var_dump($name); +// string(6) "Stefan" +``` + + +--- +## Practice + +Print a message to the console without adding a newline character after the message: +```php +??? "Hello, world!" +``` + +- `echo` +- `output` + +--- + +## Revision + +Print a message to the console and move to a new line: +```php +echo ??? +``` + +- `"Hello, world!\n"` +- `'Hello, world!\n'` + +--- +## Footnotes + +[1: Printing] +Printing messages in **PHP** is useful for debugging and for communicating information to the user.