mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-04 13:57:40 +02:00
Merge pull request #20 from restoreddev/develop
Editing and updating icons and styling
This commit is contained in:
@@ -12,13 +12,19 @@ body {
|
|||||||
padding-top: 1em;
|
padding-top: 1em;
|
||||||
padding-bottom: 1em;
|
padding-bottom: 1em;
|
||||||
}
|
}
|
||||||
pre, code {
|
code {
|
||||||
|
background-color: #F0F0F0;
|
||||||
|
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
||||||
|
font-size: 14px;
|
||||||
|
padding: 0.10em 0.25em;
|
||||||
|
}
|
||||||
|
pre, pre code {
|
||||||
|
background-color: transparent;
|
||||||
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
margin: 0 !important;
|
margin: 0 !important;
|
||||||
border-radius: 0 !important;
|
border-radius: 0 !important;
|
||||||
padding-top: 0.5em !important;
|
padding: 0.5em 0 0.5em 0;
|
||||||
padding-bottom: 0.5em !important;
|
|
||||||
}
|
}
|
||||||
a {
|
a {
|
||||||
color: $primary-color; /* #4078f2 */
|
color: $primary-color; /* #4078f2 */
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
<title>elephant</title>
|
<title>elephant</title>
|
||||||
<desc>Created with Sketch.</desc>
|
<desc>Created with Sketch.</desc>
|
||||||
<defs>
|
<defs>
|
||||||
<path d="M114.406262,33.0409234 C116.906654,33.0409234 198.587086,33.0409234 212.851562,33.0409234 C236.23173,33.0409234 290.966457,24.8360253 302.9375,21.828125 C322.709617,16.8600902 345.780295,41.1710486 350.739426,66.2834373 C355.388936,89.8279414 351.557615,107.060167 350.739426,117.478001 C348.492267,146.090655 340.913827,171.751032 340.913827,188.521289 C340.913827,211.397578 342.398654,228.60813 345.368307,240.152946 C338.345502,243.384315 332.63113,245 328.225191,245 C323.819252,245 319.466187,243.384315 315.165998,240.152946 C315.165998,212.852821 315.165998,195.642269 315.165998,188.521289 C315.165998,182.659437 313.041474,171.488839 308.792425,155.009494 C278.81585,157.935469 251.486979,159.893283 226.805814,160.882937 C202.124648,161.87259 177.935757,159.914776 154.23914,155.009494 C151.904793,170.439973 150.468074,183.375856 149.928982,193.817144 C149.389891,204.258431 148.391339,219.703699 146.933325,240.152946 C138.57305,243.384315 132.573934,245 128.935979,245 C125.298024,245 120.454785,243.384315 114.406262,240.152946 C121.029663,211.242836 124.190445,189.722491 123.888609,175.59191 C122.851956,127.060632 111.761968,33.0409234 114.406262,33.0409234 Z" id="path-1"></path>
|
<path d="M114.406262,33.0409234 C116.906654,33.0409234 198.587086,33.0409234 212.851562,33.0409234 C236.23173,33.0409234 290.966457,24.8360253 302.9375,21.828125 C322.709617,16.8600902 345.780295,41.1710486 350.739426,66.2834373 C355.388936,89.8279414 351.557615,107.060167 350.739426,117.478001 C348.492267,146.090655 340.913827,171.751032 340.913827,188.521289 C340.913827,211.397578 342.398654,228.60813 345.368307,240.152946 C338.345502,243.384315 332.63113,245 328.225191,245 C323.819252,245 319.466187,243.384315 315.165998,240.152946 C313.832665,212.852821 313.165998,195.642269 313.165998,188.521289 C313.165998,182.659437 311.70814,171.488839 308.792425,155.009494 C278.81585,157.935469 251.486979,159.893283 226.805814,160.882937 C202.124648,161.87259 177.935757,159.914776 154.23914,155.009494 C151.904793,170.439973 150.468074,183.375856 149.928982,193.817144 C149.389891,204.258431 148.391339,219.703699 146.933325,240.152946 C138.57305,243.384315 132.573934,245 128.935979,245 C125.298024,245 120.454785,243.384315 114.406262,240.152946 C121.029663,211.242836 124.190445,189.722491 123.888609,175.59191 C122.851956,127.060632 111.761968,33.0409234 114.406262,33.0409234 Z" id="path-1"></path>
|
||||||
<filter x="-2.7%" y="-2.0%" width="103.8%" height="104.0%" filterUnits="objectBoundingBox" id="filter-2">
|
<filter x="-2.7%" y="-2.0%" width="103.8%" height="104.0%" filterUnits="objectBoundingBox" id="filter-2">
|
||||||
<feMorphology radius="0.5" operator="dilate" in="SourceAlpha" result="shadowSpreadOuter1"></feMorphology>
|
<feMorphology radius="0.5" operator="dilate" in="SourceAlpha" result="shadowSpreadOuter1"></feMorphology>
|
||||||
<feOffset dx="-2" dy="0" in="shadowSpreadOuter1" result="shadowOffsetOuter1"></feOffset>
|
<feOffset dx="-2" dy="0" in="shadowSpreadOuter1" result="shadowOffsetOuter1"></feOffset>
|
||||||
@@ -24,8 +24,8 @@
|
|||||||
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
||||||
<g id="Elephant" transform="translate(-120.000000, -62.000000)">
|
<g id="Elephant" transform="translate(-120.000000, -62.000000)">
|
||||||
<g id="elephant" transform="translate(122.000000, 63.000000)">
|
<g id="elephant" transform="translate(122.000000, 63.000000)">
|
||||||
<path d="M292,154 L292,229.687017 C297.80012,232.562339 302.466787,234 306,234 C309.533213,234 314.19988,232.562339 320,229.687017 L320,154 L292,154 Z" id="Path-3" stroke="#979797" fill="#D8D8D8"></path>
|
<path d="M296,154 L296,229.687017 C301.80012,232.562339 306.466787,234 310,234 C313.533213,234 318.19988,232.562339 324,229.687017 L324,154 L296,154 Z" id="Path-3" stroke="#979797" fill="#D8D8D8"></path>
|
||||||
<path d="M113.089613,112 C112.215697,146.301082 110.868891,171.05041 109.049195,186.247987 C107.229499,201.445563 103.879767,215.920359 99,229.672376 C104.837247,232.557459 109.533784,234 113.089613,234 C116.645442,234 121.34198,232.557459 127.179226,229.672376 L134,112 L113.089613,112 Z" id="Path-3-Copy" stroke="#979797" fill="#D8D8D8"></path>
|
<path d="M121.059144,123.33286 C113.079457,155.491919 108.089613,178.903422 106.089613,193.56737 C104.449625,205.591719 102.753088,217.626721 101,229.672376 C104.755708,232.792207 108.411477,234.352122 111.967306,234.352122 C115.523135,234.352122 118.887081,232.792207 122.059144,229.672376 L143.118289,129.33286 L121.059144,123.33286 Z" id="Path-3-Copy" stroke="#979797" fill="#D8D8D8"></path>
|
||||||
<g id="Path">
|
<g id="Path">
|
||||||
<use fill="black" fill-opacity="1" filter="url(#filter-2)" xlink:href="#path-1"></use>
|
<use fill="black" fill-opacity="1" filter="url(#filter-2)" xlink:href="#path-1"></use>
|
||||||
<use stroke="#979797" stroke-width="1" fill="#D8D8D8" fill-rule="evenodd" xlink:href="#path-1"></use>
|
<use stroke="#979797" stroke-width="1" fill="#D8D8D8" fill-rule="evenodd" xlink:href="#path-1"></use>
|
||||||
|
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 6.2 KiB |
@@ -31,6 +31,7 @@
|
|||||||
<li><a href="https://postcss.org/" target="_blank">PostCSS</a></li>
|
<li><a href="https://postcss.org/" target="_blank">PostCSS</a></li>
|
||||||
<li><a href="https://symfony.com/doc/current/components/console.html" target="_blank">Symfony CLI</a></li>
|
<li><a href="https://symfony.com/doc/current/components/console.html" target="_blank">Symfony CLI</a></li>
|
||||||
<li><a href="https://symfony.com/doc/current/frontend.html" target="_blank">Symfony Encore</a></li>
|
<li><a href="https://symfony.com/doc/current/frontend.html" target="_blank">Symfony Encore</a></li>
|
||||||
|
<li><a href="https://parsedown.org/" target="_blank">Parsedown</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</p>
|
</p>
|
||||||
<hr />
|
<hr />
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
Abstract classes are similar to interfaces in that they define methods that a sub-class must implement.
|
Abstract classes are similar to interfaces in that they define methods that a sub-class must implement.
|
||||||
However, an abstract class can also have normal methods. To create an abstract class, use the "abstract"
|
However, an abstract class can also have normal methods. To create an abstract class, use the `abstract`
|
||||||
keyword followed by class and the name of the class.
|
keyword followed by `class` and the name of the class.
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ class Android extends CellPhone
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Our iPhone and Android classes can now both use the turnOn method and the unlock method.
|
Our iPhone and Android classes can now both use the `turnOn` method and the `unlock` method.
|
||||||
```php
|
```php
|
||||||
$iPhone = new iPhone();
|
$iPhone = new iPhone();
|
||||||
$iPhone->turnOn();
|
$iPhone->turnOn();
|
||||||
@@ -54,5 +54,5 @@ $android->unlock();
|
|||||||
Lastly, you cannot create an instance of an abstract class. PHP would not know how to use the abstract methods
|
Lastly, you cannot create an instance of an abstract class. PHP would not know how to use the abstract methods
|
||||||
so when you try to create an abstract instance you will get an error.
|
so when you try to create an abstract instance you will get an error.
|
||||||
```php
|
```php
|
||||||
$cellPhone = new CellPhone();
|
$cellPhone = new CellPhone(); // causes an error
|
||||||
```
|
```
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
In PHP, arrays are used to store a list of items in a single variable.
|
In PHP, arrays are used to store a list of items in a single variable.
|
||||||
There are two ways to create an array.
|
There are two ways to create an array.
|
||||||
|
|
||||||
First, you can use the array construct to pass in values separated by commas
|
First, you can use the `array` construct to pass in values separated by commas
|
||||||
and it will return an array.
|
and it will return an array.
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
@@ -25,7 +25,7 @@ echo $groceryList[1] . "\n";
|
|||||||
```
|
```
|
||||||
|
|
||||||
You can also assign keys in an array using numbers or strings.
|
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
|
It is very common to create an array with string keys. The feature
|
||||||
is called an associative array or a map.
|
is called an associative array or a map.
|
||||||
```php
|
```php
|
||||||
$car = ['make' => 'Toyota', 'model' => 'Camry'];
|
$car = ['make' => 'Toyota', 'model' => 'Camry'];
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
In the tradition of our ancestors, let's start with a hello world program.
|
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.
|
All PHP files must start with a `<?php` tag unless it is for a html template.
|
||||||
(We will learn about html templates later.)
|
(We will learn about html templates later.)
|
||||||
|
|
||||||
```php
|
```php
|
||||||
@@ -12,12 +12,14 @@ echo "Hello World!\n";
|
|||||||
There is a lot going on in the above code so let's work through it.
|
There is a lot going on in the above code so let's work through it.
|
||||||
|
|
||||||
First, the echo keyword tells PHP to output some text.
|
First, the echo keyword tells PHP to output some text.
|
||||||
|
```php
|
||||||
echo "I am some text\n";
|
echo "I am some text\n";
|
||||||
|
```
|
||||||
|
|
||||||
Second, PHP stores text in strings.
|
Second, PHP stores text in strings.
|
||||||
|
|
||||||
To write a string, you surround letters with single or double quotes.
|
To write a string, you surround letters with single or double quotes.
|
||||||
Double quoted strings can hold special characters like \n which tells PHP to start a new line.
|
Double quoted strings can hold special characters like `\n` which tells PHP to start a new line.
|
||||||
```php
|
```php
|
||||||
"\nI am a string on a new line";
|
"\nI am a string on a new line";
|
||||||
```
|
```
|
||||||
|
@@ -8,15 +8,9 @@ If so, it will return true. If not, it will return false.
|
|||||||
$a = true;
|
$a = true;
|
||||||
$b = true;
|
$b = true;
|
||||||
$c = false;
|
$c = false;
|
||||||
```
|
|
||||||
|
|
||||||
Returns true.
|
$a && $b; // returns true
|
||||||
```php
|
$a && $c; // returns false
|
||||||
$a && $b;
|
|
||||||
```
|
|
||||||
Returns false.
|
|
||||||
```php
|
|
||||||
$a && $c;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Using two pipe characters checks if either value is true.
|
Using two pipe characters checks if either value is true.
|
||||||
@@ -27,27 +21,14 @@ $a = true;
|
|||||||
$b = false;
|
$b = false;
|
||||||
$c = false;
|
$c = false;
|
||||||
$d = true;
|
$d = true;
|
||||||
|
|
||||||
|
$a || $b; // returns true
|
||||||
|
$b || $c; // returns false
|
||||||
|
$a || $d; // returns true
|
||||||
```
|
```
|
||||||
|
|
||||||
Returns true.
|
Using an exclamation point returns the opposite value.
|
||||||
```php
|
|
||||||
$a || $b;
|
|
||||||
```
|
|
||||||
Returns false.
|
|
||||||
```php
|
|
||||||
$b || $c;
|
|
||||||
```
|
|
||||||
Returns true.
|
|
||||||
```php
|
|
||||||
$a || $d;
|
|
||||||
```
|
|
||||||
|
|
||||||
Using an exclamation point returns the value flipped.
|
|
||||||
```php
|
```php
|
||||||
$d = true;
|
$d = true;
|
||||||
```
|
echo !$d; // outputs false
|
||||||
|
|
||||||
Outputs false.
|
|
||||||
```php
|
|
||||||
echo !$d;
|
|
||||||
```
|
```
|
||||||
|
@@ -53,7 +53,7 @@ class Tophat
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
"$tophat" now holds an instance of Tophat, not the color "Grey".
|
`$tophat` now holds an instance of `Tophat`, not the string "Grey".
|
||||||
```php
|
```php
|
||||||
$tophat = new Tophat('Grey');
|
$tophat = new Tophat('Grey');
|
||||||
```
|
```
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
In PHP, a class can extend another class, inheriting the parent class'
|
In PHP, a class can extend another class, inheriting the parent class'
|
||||||
properties and methods. To make a class a child of another, use the "extends"
|
properties and methods. To make a class a child of another, use the `extends`
|
||||||
keyword after the class name.
|
keyword after the class name.
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
@@ -15,7 +15,7 @@ class Vehicle
|
|||||||
class Truck extends Vehicle {}
|
class Truck extends Vehicle {}
|
||||||
```
|
```
|
||||||
|
|
||||||
The `Truck` class does not error because `Truck` extends `Vehicle`.
|
Using the `drive` method on the `Truck` class does not cause an error because `Truck` extends `Vehicle`.
|
||||||
```php
|
```php
|
||||||
$truck = new Truck();
|
$truck = new Truck();
|
||||||
$truck->drive();
|
$truck->drive();
|
||||||
@@ -39,7 +39,7 @@ $tractor = new Tractor();
|
|||||||
$tractor->drive();
|
$tractor->drive();
|
||||||
```
|
```
|
||||||
|
|
||||||
A class can use a parent's property or method from the "$this" variable.
|
A class can use a parent's property or method from the `$this` variable.
|
||||||
```php
|
```php
|
||||||
class Motorcycle extends Vehicle
|
class Motorcycle extends Vehicle
|
||||||
{
|
{
|
||||||
@@ -56,9 +56,9 @@ $cycle = new Motorcycle();
|
|||||||
$cycle->pushPedal();
|
$cycle->pushPedal();
|
||||||
```
|
```
|
||||||
|
|
||||||
If you override a parent's property or method, the "$this" variable will refer to the child's
|
If you override a parent's property or method, the `$this` variable will refer to the child's
|
||||||
implementation of the property or method. To call the parent's property or method explicity,
|
implementation of the property or method. To call the parent's property or method explicity,
|
||||||
use the "parent" keyword.
|
use the `parent` keyword.
|
||||||
```php
|
```php
|
||||||
class Racecar extends Vehicle
|
class Racecar extends Vehicle
|
||||||
{
|
{
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
In the last chapter, we defined properties and methods on the class using the public keyword.
|
In the last chapter, we defined properties and methods on the class using the public keyword.
|
||||||
You can also define them using the "protected" and "private" keywords.
|
You can also define them using the `protected` and `private` keywords.
|
||||||
Both keywords prevent the properties and functions from being accessible outside the object.
|
Both keywords prevent the properties and functions from being accessible outside the object.
|
||||||
Only the object itself can use each.
|
Only the object itself can use each.
|
||||||
```php
|
```php
|
||||||
@@ -16,7 +16,7 @@ class Phone
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
We cannot set the number using "$phone->number = '123-456-7890'".
|
We cannot set the number using `$phone->number = '123-456-7890'`.
|
||||||
Instead, we can use the public method.
|
Instead, we can use the public method.
|
||||||
```php
|
```php
|
||||||
$phone = new Phone();
|
$phone = new Phone();
|
||||||
@@ -39,9 +39,9 @@ class Phone2
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The "protected" and "private" keywords work a little differently.
|
The `protected` and `private` keywords work a little differently.
|
||||||
They both prevent functions and properties from being accessed outside an object.
|
They both prevent functions and properties from being accessed outside an object.
|
||||||
However, a method or property marked "protected" can still be accessed by a child class.
|
However, a method or property marked `protected` can still be accessed by a child class.
|
||||||
```php
|
```php
|
||||||
class Phone3
|
class Phone3
|
||||||
{
|
{
|
||||||
@@ -56,8 +56,8 @@ class Phone3
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In class "Smartphone", the "caller" property is accessible because the parent class
|
In class `Smartphone`, the `caller` property is accessible because the parent class
|
||||||
has it marked as "protected". However, "Smartphone" cannot access the "number" property
|
has it marked as `protected`. However, `Smartphone` cannot access the `number` property
|
||||||
because it is still listed as private.
|
because it is still listed as private.
|
||||||
```php
|
```php
|
||||||
class Smartphone extends Phone3
|
class Smartphone extends Phone3
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
Classes allow you to define your own data types. All classes start with the
|
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 keyword followed by the name of the class and opening and closing curly braces.
|
||||||
```php
|
```php
|
||||||
@@ -9,7 +8,7 @@ class Car
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
To create an instance of a class, you use the "new" keyword in front of the class name
|
To create an instance of a class, you use the `new` keyword in front of the class name
|
||||||
with parentheses.
|
with parentheses.
|
||||||
```php
|
```php
|
||||||
$car = new Car();
|
$car = new Car();
|
||||||
@@ -17,7 +16,7 @@ $car = new Car();
|
|||||||
|
|
||||||
A class can define attributes and methods. An attribute is a piece of data
|
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
|
stored on the class instance. You can define an attribute by adding the
|
||||||
word "public" and a variable name inside the class definition.
|
word `public` and a variable name inside the class definition.
|
||||||
```php
|
```php
|
||||||
class Bicycle
|
class Bicycle
|
||||||
{
|
{
|
||||||
@@ -26,7 +25,7 @@ class Bicycle
|
|||||||
```
|
```
|
||||||
|
|
||||||
Then, when you create an instance of the class, you can set and use
|
Then, when you create an instance of the class, you can set and use
|
||||||
the color attribute on the bicycle using "->".
|
the color attribute on the bicycle using `->`.
|
||||||
```php
|
```php
|
||||||
$bike = new Bicycle();
|
$bike = new Bicycle();
|
||||||
$bike->color = 'Blue';
|
$bike->color = 'Blue';
|
||||||
@@ -42,8 +41,8 @@ echo $redBike->color . " Bike Object\n";
|
|||||||
```
|
```
|
||||||
|
|
||||||
A method is a function attached to the class. You can add a method
|
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
|
to a class by using the `public` keyword followed by the function. A method
|
||||||
can access the attributes and methods of an object instance using the "$this" variable.
|
can access the attributes and methods of an object instance using the `$this` variable.
|
||||||
```php
|
```php
|
||||||
class Tricycle
|
class Tricycle
|
||||||
{
|
{
|
||||||
@@ -56,7 +55,7 @@ class Tricycle
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can execute a method on an object using the same "->" arrow characters.
|
You can execute a method on an object using the same `->` arrow characters.
|
||||||
```php
|
```php
|
||||||
$bike = new Tricycle();
|
$bike = new Tricycle();
|
||||||
$bike->color = 'Red';
|
$bike->color = 'Red';
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
A boolean is a value that is always 0 or 1, yes or no, on or off.
|
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.
|
In PHP, a boolean is represented by the words true and false.
|
||||||
While programming, you will often want to know if something is positive or negative.
|
While programming, you will often want to know if something is positive or negative.
|
||||||
@@ -48,7 +47,7 @@ $one >= $two;
|
|||||||
You can also check that two values are equal and of the same type
|
You can also check that two values are equal and of the same type
|
||||||
by using three equal signs.
|
by using three equal signs.
|
||||||
|
|
||||||
This returns true.
|
The following comparisons return true.
|
||||||
```php
|
```php
|
||||||
1 == 1;
|
1 == 1;
|
||||||
1 == '1';
|
1 == '1';
|
||||||
@@ -57,7 +56,7 @@ This returns true.
|
|||||||
1 === 1;
|
1 === 1;
|
||||||
```
|
```
|
||||||
|
|
||||||
This returns false.
|
These return false.
|
||||||
```php
|
```php
|
||||||
1 === '1';
|
1 === '1';
|
||||||
1 === true;
|
1 === true;
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
When writing code, there will be times when you need to perform actions only under certain circumstances.
|
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.
|
There are several ways to control execution in PHP.
|
||||||
We will start with an if statement.
|
We will start with an if statement.
|
||||||
@@ -12,10 +11,10 @@ if ($animal == 'cow') {
|
|||||||
```
|
```
|
||||||
|
|
||||||
All conditionals check to see if a statement evaluates to true or false.
|
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.
|
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.
|
An if statement can have multiple conditions chained together.
|
||||||
If the first if statement returns false, then PHP will check each elseif.
|
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.
|
If none of the checks return true, then the else block will be executed.
|
||||||
```php
|
```php
|
||||||
$animal = 'bird';
|
$animal = 'bird';
|
||||||
@@ -32,7 +31,7 @@ if ($animal == 'dog') {
|
|||||||
|
|
||||||
An alternative to the if statement is the switch.
|
An alternative to the if statement is the switch.
|
||||||
A switch statement has multiple cases to check if the value in parentheses equals something.
|
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".
|
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.
|
The default case will be run if no other case evaluates to true, like an else statement.
|
||||||
```php
|
```php
|
||||||
$food = 'apples';
|
$food = 'apples';
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
Sometimes things go wrong when someone uses your code. How do we handle this situation?
|
Sometimes things go wrong when someone uses your code. How do we handle this situation?
|
||||||
PHP has Exceptions to define errors and the ability to "throw" them to stop code
|
PHP has Exceptions to define errors and the ability to `throw` them to stop code
|
||||||
execution and tell the user of your code that something is wrong.
|
execution and tell the user of your code that something is wrong.
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
@@ -15,9 +15,9 @@ class Processor
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In this case, if someone tried to use the Processor class
|
In this case, if someone tried to use the `Processor` class
|
||||||
to charge a credit card number that is not 16 characters long, an
|
to charge a credit card number that is not 16 characters long, an
|
||||||
exception will be thrown which stops the rest of the code from running.
|
`Exception` will be thrown which stops the rest of the code from running.
|
||||||
```php
|
```php
|
||||||
$processor = new Processor();
|
$processor = new Processor();
|
||||||
$processor->charge('1234');
|
$processor->charge('1234');
|
||||||
@@ -26,8 +26,8 @@ $processor->charge('1234');
|
|||||||
A developer who wants to prevent an exception from stopping code execution
|
A developer who wants to prevent an exception from stopping code execution
|
||||||
can catch the exception and use it for logging or display the error to a user.
|
can catch the exception and use it for logging or display the error to a user.
|
||||||
|
|
||||||
Just wrap the code that might throw an exception with the keyword "try" and brackets
|
Just wrap the code that might throw an exception with the keyword `try` and braces
|
||||||
followed by "catch", the exception type in parentheses and more brackets.
|
followed by `catch`, the exception type in parentheses and more braces.
|
||||||
```php
|
```php
|
||||||
try {
|
try {
|
||||||
$processor->charge('1234');
|
$processor->charge('1234');
|
||||||
@@ -37,7 +37,7 @@ try {
|
|||||||
```
|
```
|
||||||
|
|
||||||
You can make your own custom exceptions as well. They are just classes
|
You can make your own custom exceptions as well. They are just classes
|
||||||
that extend Exception.
|
that extend the `Exception` class.
|
||||||
```php
|
```php
|
||||||
class MyCustomException extends Exception {}
|
class MyCustomException extends Exception {}
|
||||||
```
|
```
|
||||||
@@ -47,12 +47,12 @@ Then, you can try to catch your exception instead of the base exception.
|
|||||||
try {
|
try {
|
||||||
throw new MyCustomException('I am a custom exception');
|
throw new MyCustomException('I am a custom exception');
|
||||||
} catch (MyCustomException $e) {
|
} catch (MyCustomException $e) {
|
||||||
echo "Cauth MyCustomException\n";
|
echo "Caught MyCustomException\n";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Since all exceptions inherit from Exception, catching
|
Since all exceptions inherit from `Exception`, catching
|
||||||
Exception will catch any and all exceptions that might be thrown.
|
`Exception` will catch any and all exceptions that might be thrown.
|
||||||
```php
|
```php
|
||||||
try {
|
try {
|
||||||
throw new MyCustomException('I inherit from Exception');
|
throw new MyCustomException('I inherit from Exception');
|
||||||
|
@@ -27,8 +27,8 @@ function greet($firstname, $lastname) {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Then, you can pass in values when calling a function. In the greet function,
|
Then, you can pass in values when calling a function. In the greet function,
|
||||||
'John' is assigned to $firstname and 'Smith' is assigned to
|
'John' is assigned to `$firstname` and 'Smith' is assigned to
|
||||||
$lastname.
|
`$lastname`.
|
||||||
```php
|
```php
|
||||||
greet('John', 'Smith');
|
greet('John', 'Smith');
|
||||||
```
|
```
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
The word "interface" is a confusing term because it is used for so many different concepts.
|
The word `interface` is a confusing term because it is used for so many different concepts.
|
||||||
Most often, we use it to describe the appearance of an app and how a user interacts with it.
|
Most often, we use it to describe the appearance of an app and how a user interacts with it.
|
||||||
However, in PHP, an interface is a special construct that acts as a contract for classes.
|
However, in PHP, an interface is a special construct that acts as a contract for classes.
|
||||||
An interface defines what methods a class should have.
|
An interface defines what methods a class should have.
|
||||||
@@ -12,8 +12,8 @@ interface Chair
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
To use an interface with a class, you use the "implements" keyword after the class name.
|
To use an interface with a class, you use the `implements` keyword after the class name.
|
||||||
Now, the Recliner class must have a setColor method and a setLegs method.
|
Now, the `Recliner` class must have a `setColor` method and a `setLegs` method.
|
||||||
If you do not create the required methods on the class, PHP will throw an error.
|
If you do not create the required methods on the class, PHP will throw an error.
|
||||||
```php
|
```php
|
||||||
class Recliner implements Chair
|
class Recliner implements Chair
|
||||||
@@ -53,7 +53,7 @@ class CreditCard
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Since CreditCard implements Payment, other developers can use the charge method, knowing it exists on the class.
|
Since `CreditCard` implements `Payment`, other developers can use the charge method, knowing it exists on the class.
|
||||||
```php
|
```php
|
||||||
$creditCard = new CreditCard();
|
$creditCard = new CreditCard();
|
||||||
$creditCard->charge(25);
|
$creditCard->charge(25);
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
A loop tells PHP to run a block of code more than once.
|
A loop tells PHP to run a block of code more than once.
|
||||||
A classic loop is a while loop.
|
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.
|
A `while` loop will continue to run the block of code as long as the value in parentheses is true.
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -11,9 +11,9 @@ while ($num > 0) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
A "do while" loop is similar to a "while" loop except it always runs at least
|
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
|
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
|
the value in parentheses is false. In a `do while`, the boolean check
|
||||||
is not done until after the execution of an iteration.
|
is not done until after the execution of an iteration.
|
||||||
```php
|
```php
|
||||||
$num = 0;
|
$num = 0;
|
||||||
@@ -23,7 +23,7 @@ do {
|
|||||||
} while ($num < 5);
|
} while ($num < 5);
|
||||||
```
|
```
|
||||||
|
|
||||||
"for" loops allow you to create a more concise while loop.
|
`for` loops allow you to create a more concise while loop.
|
||||||
Inside the parentheses, the left section creates a variable before the 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
|
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.
|
and the third section is executed after each loop.
|
||||||
@@ -33,9 +33,9 @@ for ($i = 0; $i < 10; $i++) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
A "foreach" loop allows you to easily loop over an array.
|
A `foreach` loop allows you to easily loop over an array.
|
||||||
An array is a list of data stored together.
|
An array is a list of data stored together.
|
||||||
The "as" keyword lets you assign a variable to the value
|
The `as` keyword lets you assign a variable to the value
|
||||||
in the array for the current iteration of the loop.
|
in the array for the current iteration of the loop.
|
||||||
```php
|
```php
|
||||||
$set = [1, 2, 3, 4, 5];
|
$set = [1, 2, 3, 4, 5];
|
||||||
@@ -44,7 +44,7 @@ foreach ($set as $num) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In loops, you can use the keyword "break" to stop the loop execution
|
In loops, you can use the keyword `break` to stop the loop execution
|
||||||
no matter how many more iterations should run.
|
no matter how many more iterations should run.
|
||||||
```php
|
```php
|
||||||
$values = ['one', 'two', 'three'];
|
$values = ['one', 'two', 'three'];
|
||||||
@@ -56,7 +56,7 @@ foreach ($values as $value) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The "continue" keyword stops executing the current loop iteration,
|
The `continue` keyword stops executing the current loop iteration,
|
||||||
but then allows the loop to continue with other iterations.
|
but then allows the loop to continue with other iterations.
|
||||||
```php
|
```php
|
||||||
$values = ['one', 'skip', 'three'];
|
$values = ['one', 'skip', 'three'];
|
||||||
|
@@ -22,7 +22,7 @@ $house = new House('Green');
|
|||||||
```
|
```
|
||||||
|
|
||||||
However, what if you want the blueprint to have properties and methods?
|
However, what if you want the blueprint to have properties and methods?
|
||||||
That is when you use the "static" keyword. In this class, we will define a default color
|
That is when you use the `static` keyword. In this class, we will define a default color
|
||||||
on the class itself and then use it when creating a new object.
|
on the class itself and then use it when creating a new object.
|
||||||
```php
|
```php
|
||||||
class Skyscraper
|
class Skyscraper
|
||||||
@@ -42,7 +42,7 @@ class Skyscraper
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can access static methods and properties using double colons on "self" inside the object
|
You can access static methods and properties using double colons on `self` inside the object
|
||||||
or on the class name outside of the object. Static methods and properties can only access
|
or on the class name outside of the object. Static methods and properties can only access
|
||||||
other static methods and properties.
|
other static methods and properties.
|
||||||
```php
|
```php
|
||||||
@@ -75,7 +75,7 @@ class TinyHouse
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Using "build" can make more sense than "new", but it is ultimately a personal preference.
|
Using `build` can make more sense than `new`, but it is ultimately a personal preference.
|
||||||
```php
|
```php
|
||||||
$house = TinyHouse::build('Blue', 4, true);
|
$house = TinyHouse::build('Blue', 4, true);
|
||||||
```
|
```
|
||||||
|
@@ -8,7 +8,7 @@ $lastname = "Johnson";
|
|||||||
```
|
```
|
||||||
|
|
||||||
A double quoted string can interpret special characters starting
|
A double quoted string can interpret special characters starting
|
||||||
with a back slash to create formatting. The \n creates a newline
|
with a back slash to create formatting. The `\n` creates a newline
|
||||||
between the names and after them.
|
between the names and after them.
|
||||||
```php
|
```php
|
||||||
echo "Jacob\nJones\n";
|
echo "Jacob\nJones\n";
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
The variable is the basic building block of any programming language.
|
The variable is the basic building block of any programming language.
|
||||||
In PHP, all variables start with a dollar sign.
|
In PHP, all variables start with a dollar sign.
|
||||||
```php
|
```php
|
||||||
|
Reference in New Issue
Block a user