Skip to content

Instantly share code, notes, and snippets.

@GraceHanssen
Created March 5, 2019 17:50
Show Gist options
  • Save GraceHanssen/4b57f999d7d50a312bf6574fd081b7d1 to your computer and use it in GitHub Desktop.
Save GraceHanssen/4b57f999d7d50a312bf6574fd081b7d1 to your computer and use it in GitHub Desktop.
Responsive Web Design Project: Technical Documentation Page
<nav id="navbar">
<header>PHP Documentation</header>
<ul>
<a class="nav-link" href="#Introduction" rel="internal">
<li>Introduction</li>
</a>
<a class="nav-link" href="#Hello_world" rel="internal">
<li>Hello world</li>
</a>
<a class="nav-link" href="#Variables_and_types" rel="internal">
<li>Variables And Types</li>
</a>
<a class="nav-link" href="#Simple_arrays" rel="internal">
<li>Simple Arrays</li>
</a>
<a class="nav-link" href="#Arrays_with_keys" rel="internal">
<li>Arrays With Keys</li>
</a>
<a class="nav-link" href="#Strings" rel="internal">
<li>Strings</li>
</a>
<a class="nav-link" href="#For_loops" rel="internal">
<li>For Loops</li>
</a>
<a class="nav-link" href="#While_loops" rel="internal">
<li>While Loops</li>
</a>
<a class="nav-link" href="#Functions" rel="internal">
<li>Functions</li>
</a>
<a class="nav-link" href="#Objects" rel="internal">
<li>Objects</li>
</a>
<a class="nav-link" href="#Reference" rel="internal">
<li>Reference</li>
</a>
</ul>
</nav>
<main id="main-doc">
<section class="main-section" id="Introduction">
<header>Introduction</header>
<p>PHP: Hypertext Preprocessor (or simply PHP) is a server-side scripting language designed for Web development, but also used as a general-purpose programming language. It was originally created by Rasmus Lerdorf in 1994, the PHP reference implementation
is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP: Hypertext Preprocessor.</p>
<section class="main-section" id="Hello_world">
<header>Hello World!</header>
<p>PHP is the most commonly used programming language for the web today. PHP is very common because it has a relatively simple architecture compared to other MVC based web frameworks (Python, Ruby, node.js, etc). Unlike the standard web frameworks,
a PHP file is actually an "enhanced" HTML file, which is also capable of executing code inside a document. So for example, you may start with a simple HTML page which looks like this:<code>
<html>
<head></head>
<body>Hello World!<?php ?>
</body>
</html>
</code>And later on add a PHP section which executes PHP code, and writes the output as HTML. Notice that the PHP line disappeared when executing, since the PHP code is replaced by the output. Let's try adding the name of the user's name.<code>
<html>
<head></head>
<body>Hello!</body>
</html>
</code>In our tutorials, we will always open and close a PHP tag (starting with
<?php and ending with ?>) in the beginning and the end of our code. For testing our code, we are able to print messages to our console using the echo command.
</p>
</section>
<section class="main-section" id="Variables_and_types">
<header>Variables and Types</header>
<p>To define a variable, simply use the following syntax: $x = 1; $y = "foo"; $z = True; We have just defined a variable named x with the number 1, a variable named y with the string "foo" and a variable name z with the boolean value True. Once they
are defined, we can use them in the code. PHP has many types of variables, but the most basic variable types are integer (whole numbers), float (real numbers), strings, and booleans. PHP also has arrays and objects which we will explain in other
tutorials. Variables can also be set to NULL, which means that the variables exist, but do not contain any value.<br><br>
<li> Operators
We can use simple arithmetic operators to add, subtract or concatenate between variables. We can also print out PHP variables using the echo command (you can try it out now). For example, let's sum up two numbers, put the result in a new variable, and
print out the result.</li><code>
$x = 1;
$y = 2;
$sum = $x + $y;
echo $sum; // prints out 3</code>
<li> String formatting
Like Perl, PHP double quoted strings can format strings using defined variables. For example:</li><code>
$name = "Jake";
echo "Your name is $name"; // prints out Your name is Jake</code>
</p>
</section>
<section class="main-section" id="Simple_arrays">
<header>Simple Arrays</header>
<p>Arrays are a special type of variable that can contain many variables, and hold them in a list. For example, let's say we want to create a list of all the odd numbers between 1 and 10. Once we create the list, we can assign new variables that will
refer to a variable in the array, using the index of the variable. To use the first variable in the list (in this case the number 1), we will need to give the first index, which is 0, since PHP uses zero based indices, like almost all programming
languages today.
<code>$odd_numbers = [1,3,5,7,9];
$first_odd_number = $odd_numbers[0];
$second_odd_number = $odd_numbers[1];
echo "The first odd number is $first_odd_number\n";
echo "The second odd number is $second_odd_number\n"; </code>We can now add new variables using an index. To add an item to the end of the list, we can assign the array with index 5 (the 6th variable):<code>$odd_numbers = [1,3,5,7,9];
$odd_numbers[5] = 11;
print_r($odd_numbers);</code>Arrays can contain different types of variables according to your needs, and can even contain other arrays or objects as members. To delete an item from an array, use the unset function on the member itself. For example:<code>$odd_numbers = [1,3,5,7,9];
unset($odd_numbers[2]); // will remove the 3rd item (5) from the list
print_r($odd_numbers);</code>
</p>
</section>
<section class="main-section" id="Arrays_with_keys">
<header>Arrays with Keys</header>
<p>PHP arrays are actually ordered maps, meaning that all values of arrays have keys, and the items inside the array preserve order. When using arrays as simple lists as we have seen last chapter, a zero based counter is used to set the keys. Each
item which is added to the array increments the next index by 1. A good example for using arrays with keys is a phone book. Let's say we want to save the phone numbers of people in a class.<code>$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r($phone_numbers);
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
echo "Jessica's phone number is " . $phone_numbers["Jessica"] . "\n";</code>To add an item to an array using a key, we use the brackets operator, as you would expect.<code>$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r($phone_numbers);</code>To check if a key exists within an array, we can use the array_key_exists function:<code>$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if (array_key_exists("Alex", $phone_numbers)) {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if (array_key_exists("Michael", $phone_numbers)) {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}</code>If we want to extract only the keys of the array (the names), we can use the array_keys function.<code>$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r(array_keys($phone_numbers));</code>Alternatively, to get only the values of an array (the phone numbers), we can use the array_values function.<code>$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r(array_values($phone_numbers));</code></p>
</section>
<section class="main-section" id="Strings">
<header>Strings</header>
<p>Strings are variables that hold text. For example, a string which contains a name is defined as follows:<code>$name = "John";
echo $name;</code>We can easily format strings using variables. For example:<code>$name = "John";
$introduction = "Hello $name";
echo $introduction;</code>We can also concatenate strings using the dot . operator. For example:<code>$first_name = "John";
$last_name = "Doe";
$name = $first_name . " " . $last_name;
echo $name;</code>To measure the length of a string, we use the strlen function:<code>$string = "The length of this string is 43 characters.";
echo strlen($string);</code>To cut a part of a string and return it as a new string, we can use the substr function:<code>$filename = "image.png";
$extension = substr($filename, strlen($filename) - 3);
echo "The extension of the file is $extension";</code></p>
<section class="main-section" id="For_loops">
<header>For Loops</header>
<p>For loops are simple loops which helps us iterate over an iterable variable by using an index. There are two types of for loops - a simple (C style) for loop, and a foreach loop. For loop For loops are very useful when we need to iterate over
an array and refer to member of the array using a changing index. For example, let's say we have a list of odd numbers. To print them out, we need to refer to each item individually. The code we write in the for loop can use the index i, which
changes in every iteration of the for loop.<code>$odd_numbers = [1,3,5,7,9];
for ($i = 0; $i < count($odd_numbers); $i=$i+1) {
$odd_number = $odd_numbers[$i];
echo $odd_number . "\n";
}</code>The first line of the for loop defines 3 parts: The initialization statement - in our case, we initialize the iterator variable $i to 0. The condition statement - this statement gets evaluated in every loop. The loop stops when this condition
is unmet. This will happen when the iterator variable $i will be larger than the length of the array. The increment statement - this statement is executed every iteration to increase the index variable by the needed amount. Usually, we will
increase $i by 1. There are two shorter ways of increasing a variable by 1 as well. We can use $i+=1 or $i++ as well. Foreach loop The foreach loop iterates over an iterable element such as an array or an object, providing the members in a specific
variable one at a time. For example, let's say we want to create a list of all the odd numbers between 1 and 10, and print them out one by one, like in the previous example. This time, we will be using the foreach statement instead of a regular
for loop wih an iterator variable. Instead of using the iterator variable as an index to the array, we get the item from the array directly into the $odd_number variable.<code>$odd_numbers = [1,3,5,7,9];
foreach ($odd_numbers as $odd_number) {
echo $odd_number . "\n";
}</code>When iterating over arrays with keys, we can use the following syntax:<code>$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
foreach ($phone_numbers as $name => $number) {
echo "$name's number is $number.\n";
}</code></p>
</section>
<section class="main-section" id="While_loops">
<header>While Loops</header>
<p>While loops are simple blocks that execute repeatedly until the while loop condition is not met. Here is an example of a loop that is executed a total of 10 times:<code>$counter = 0;
while ($counter < 10) {
$counter += 1;
echo "Executing - counter is $counter.\n";
}</code>The main difference between for loops and while loops is that for loops are used to iterate over an array or an object, and a while loop will execute an unknown amount of times, depending on variable conditions (for example, until a user has entered
the correct input). Flow statements Loops can be controlled using the break and continue flow statements, which come in handy in while loops very much. The break statement immediately quits the for loop at the middle of the block, while the
continue statement returns to the top of the while loop, re-checking if the loop condition is met as well. The continue statement Let's use the previous example, but this time let's add a check to see if the number is even. If it is, we will
skip it, so that only odd numbers will be printed out.<code>$counter = 0;
while ($counter < 10) {
$counter += 1;
if ($counter % 2 == 0) {
echo "Skipping number $counter because it is even.\n";
continue;
}
echo "Executing - counter is $counter.\n";
}</code>The break statement Let's assume we want to add another test that checks if the counter variable is no larger than 8. If it is, we would like to stop the loop. This will cause the number 9 to be not printed in this example.<code>$counter = 0;
while ($counter < 10) {
$counter += 1;
if ($counter > 8) {
echo "counter is larger than 8, stopping the loop.\n";
break;
}
if ($counter % 2 == 0) {
echo "Skipping number $counter because it is even.\n";
continue;
}
echo "Executing - counter is $counter.\n";
}</code></p>
</section>
<section class="main-section" id="Functions">
<header>Functions</header>
<p>Functions are simple code blocks we can call from anywhere. For example, we can create a function that sums a list of numbers and returns the result. Let's call this function sum. There are two types of functions - library functions and user functions.
Library functions, such as array_push are part of the PHP library and can be used by anyone. However, you may write your own functions and use them across your code. A function receives a list of arguments separated by commas. Every argument
only exists in the context of the function, meaning that they become variables inside the function block, but are not defined outside of that function block.<code>// define a function called `sum` that will
// receive a list of numbers as an argument.
function sum($numbers) {
// initialize the variable we will return
$sum = 0;
// sum up the numbers
foreach ($numbers as $number) {
$sum += $number;
}
// return the sum to the user
return $sum;
}
// Example usage of sum
echo sum([1,2,3,4,5,6,7,8,9,10]);</code>After defining functions, you may load other PHP files into one another, so you may define all your functions in one file, and load them for another. Let's say that we have defined the sum function inside a file
called sum.php. We can now create another file, say index.php and use the sum function by including sum.php as follows:<code>include("sum.php");
// Example usage of sum
echo sum([1,2,3,4,5,6,7,8,9,10]);</code></p>
</section>
<section class="main-section" id="Objects">
<header>Objects</header>
<p>PHP is an object oriented language, although it does not have to be used as one, since most PHP functions are not object oriented. In object oriented programming, a class is a definition of an object, whereas an object is an instance of an object,
meaning that from one class you can create many objects. For example, let's define a class of a student.
<code>
class Student {
// constructor
public function __construct($first_name, $last_name) {
$this->first_name = $first_name;
$this->last_name = $last_name;
}
public function say_name() {
echo "My name is " . $this->first_name . " " . $this->last_name . ".\n";
}
}
$alex = new Student("Alex", "Jones");
$alex->say_name();
</code>Let's analyze the code. Notice that the Student class has a constructor function, which is executed when the object is created. The constructor receives arguments which are later provided when constructing the object with the
new keyword. After we have constructed the object into the variable $alex we can now use the object's methods. We implemented an object method say_name, which prints out the name of the student. Notice that the say_name function does not receive
any arguments, but it does have access to the first and last name of the student, because they were previously defined in the constructor. Here are some important definitions related to objects: Classes define how objects behave. Classes do
not contain data. Objects are instances of classes, which contain data. Members are variables that belong to an object. Methods are functions that belong to an object, and have access to its members. Constructor is a special method that is executed
when an object is created. Inheritance The most important feature of object oriented programming is inheritance. This feature allows us to reuse code we've written and extend it. For example, let's say we want to be able to define a math student,
which also knows how to sum two numbers.<code>class Student {
// constructor
public function __construct($first_name, $last_name) {
$this->first_name = $first_name;
$this->last_name = $last_name;
}
public function say_name() {
echo "My name is " . $this->first_name . " " . $this->last_name . ".\n";
}
}
$alex = new Student("Alex", "Jones");
$alex->say_name();
class MathStudent extends Student {
function sum_numbers($first_number, $second_number) {
$sum = $first_number + $second_number;
echo $this->first_name . " says that " . $first_number . " + " . $second_number . " is " . $sum;
}
}
$eric = new MathStudent("Eric", "Chang");
$eric->say_name();
$eric->sum_numbers(3, 5);</code>Notice that Eric's object also has the same constructor and the say_name function, in addition to a new method called sum_numbers, which causes Eric to calculate the sum of two numbers. Also notice that the new function
has access to the same members we have previously defined in the Student class (first_name, last_name). Public and private functions We can use the public and private modifiers respectively to define functions which can be accessed from outside
the object or not, for encapsulation purposes. This allows to better define how objects should be used, to separate between functions which are used for internal use, as opposed to an external interface.<code>class Student {
// constructor should be public
public function __construct($first_name, $last_name) {
$this->first_name = $first_name;
$this->last_name = $last_name;
}
// for external use
public function say_name() {
echo "My name is " . $this->full_name() . "\n";
}
// for internal use
private function full_name() {
return $this->first_name . " " . $this->last_name;
}
}
$alex = new Student("Alex", "Jones");
$alex->say_name();
// this will not work
// echo $alex->full_name();</code>
</p>
<section class="main-section" id="Reference">
<header>Reference</header>
<p>Details are excerpt from <a href="https://en.wikipedia.org/wiki/PHP">Wikipedia</a> and <a href="https://www.learn-php.org">Learn PHP</a> </p>
</section>
</article>
<hr><br>
<center>
<footer>Created by Grace</footer>
@media screen and (min-width: 900px) {
article {
padding: 1rem 3rem;
}
}
body {
background-color: white;
padding: 2%;
margin: 2%;
}
#main-doc {
color: black;
font-family: verdana;
font-size: 130%;
font-weight: bold;
text-align: center;
}
nav {
color: black;
font-family: verdana;
font-size: 120%;
font-weight: bold;
text-align:;
}
header {
color: green;
text-decoration: underline;
font-size: 120%;
}
ul {
font-family: Georgia;
font-size: 100%;
font-weight: bold;
color: green;
text-align: justify;
text-decoration: none;
}
li {
font-weight: normal;
font-style: italic;
}
.main-section {
text-align: justify;
}
p {
color: black;
font-family: Times New Roman;
font-weight: lighter;
font-size: 110%;
text-decoration: none;
}
code {
display: block;
text-align: left;
white-space: pre;
position: relative;
word-break: normal;
word-wrap: normal;
line-height: 2;
background-color:#637328;
padding:15px;
margin:10px;
border-radius:5px;
} //code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment