CS50 - Week 7
07557267417 QDGxqQ2K
IP - Internet Protocol
Method for addressing every computer on the internet.
DNS - Domain Name Server - has mapping of URLs to IP addresses and vice versa. Use nslookup
to query DNS.
TCP - Transmission Communication Protocol
Method for sending information between computers on the internet with reliabiity that whole message will be delivered. It will also divide the message into multiple segments.
HTTP
Protocol which web browers use to talk to web servers.
GET / HTTP/1.1 Host: www.google.com ...
HTTP/1.1 200 OK Content-Type: text/html ...
PHP Syntax
-
Use the following to demarquate php code to be interpreted by the php interpreter.
-
All variables start with a dollar sign e.g.
$x = 4;
$s = "hello";
-
Types are implicit, not explicit
-
Functions are declared using the
function
keyword e.g.function f() { ... }
-
Functions do not specify a return type and can return different types at run time as required
-
Declare arrays using e.g.
$a = array(1, 2, 3);
$a = [1, 2, 3];
$a = [];
-
Declare associative arrays using e.g.
$h = ["a" => "apple", "b" => "banana"];
$h = [];
-
Add a new element to an array using e.g.
$a[] = 4;
-
Add a new element to an associate array using e.g.
$h["c"] = "cat";
-
The function
count
returns the number of elements in an array e.g.count($a);
-
Single or double quotes can be used for creating strings
-
The dot operator concatenates strings e.g.
$n = 5; $s = 'n is '.$n;
-
Single or double quotes can be used in the example above.
-
Variables are interpreted when used in double quoted strings e.g.
"n is $n"
will become"n is 5"
-
Variables are not interpreted when using single quotes e.g.
'n is $n'
still remain"n is $n"
-
To include values of arrays in strings use {} e.g.
"{h['c']}"
would give "cat" -
foreach
loops are useful in php. Note the order of elements in the declaration -
require_once
copies the contents of one file into another filerequire_once(DIR . "/math.php"); print(add(1, 2) . "\n");
-
DIR is a special constant for the current directory, so this file is in the same directory as
math.php
CSS
- A
CSS rule
is usually aselector
followed bydeclarations
to determine the style Combinators
let you join multiplesimple selectors
- Simple selectors include:
- ID selectors i.e. #
- tag selectors e.g. h1
- class selectors e.g. .box
- attribute selectors e.g. [type="radio"] to target e.g.
- pseudoselectors for specificing styling when interations occur e.g. h1:hover
- Combinators include:
- whitespace for children
- commas to distinguish selectors
>
for direct children only~
for sibling elements+
for the one sibling which comes directly after another element
HTML
Not a programming language, a markup language.
PHP
Find in your environment the interpreter for php
#!/usr/bin/env php
Print out post variables using
<?php print_r($_POST); ?>
Where print_r
is a recursive print function and $_POST
is a special variable, a super global, which holds the post variables. They can be indexed into using square brackets e.g. $_POST["name"]
. The function empty
can be used to check if a variable has a function e.g. empty($_POST["name"])
To send email use:
require("libphp-phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host ="smtp.fas.harvard.edu";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->SetFrom("jharvard@cs50.harvard.edu");
$mail->AddAddress("jharvard@cs50.harvard.edu");
$mail->Subject("Registration");
$mail->Body =
"This person just registered:\n\n" .
"Name: " . $_POST["name"] . "\n";
if ($mail->Send() == false)
{
die($mail->ErrInfo);
}
Superglobals
$_COOKIE $_GET $_POST $_SERVER $_SESSION ...
Sessions
Sessions need to be started before they can be used.
session_start();
if (isset($_SESSION["counter"]))
{
$counter = $_SESSION["counter"];
}
else
{
$counter = 0;
}
// increment counter
$_SESSION["counter"] = $counter + 1;
Parameters to functions
You can pass in arbitrary key value pairs to functions where the key indicates the name of an argument and the value indicates the value to pass for that argument. E.g.
renderHeader(["title" => "CS50"]);
and the square brackets incidate that an associative array is being passed in, so multiple values are possible. E.g.
renderHeader(["title" => "CS50", "week" => 0]);
The definition of this function may be:
function renderHeader($data = [])
{
extract($data);
}
where extract($data)
takes all the keys and creates variables for them.
Misc
htmlspecialchars($title)
is a special function which renders any dangerous characters harmless.- Use
<?= ... ?>
as a shortcut to<?php print ... ?>
===
compares operands values and types whereas==
only compares values. So e.g. an empty array will evaluate to false so[] == false
will betrue
whereas[] === false
will befalse
.
File permissions
- PHP files should have
600
so that you (and the webserver) can read and write to it (visitors should not be able to see php files, only the result of executing them) - for security reasons, php web pages are interpreted by "you" (using suPHP and Apache on the appliance) - Non PHP files should have
644
so that you can read and write, others can read and execute - Directories should have
711
so that you can read, write and execute and everyone else can only execute
Stocks
What are stocks?
A stock is a share in the ownership of a company. Stock = Shares = Equity. Being a shareholder means you are one of the many owners of a company and as such have a claim to everything the company owns. You are entitled to your share of the company's earnings as well as any voting rights attached to the share.
Usually holding shares gives you an equivalent number of votes for who should be on the board of directors. Though only having a few share, and therefore a few votes won't affect this too much.
Profits are sometimes paid out in the form of dividends. Your claim on assets is only relevant if the company goes bankrupt. In this scenario, you will only get some money back once all other creditors have been paid. Nevertheless owning shares gives you a claim on the earnings and assets.
Limited liability is another important aspect of shares. You are not personally liable if the company is not able to pay its debts. Partnerships, on the other hand, do hold any partners liable. Shareholders only ever stand to loose their investment in shares.
Companies sell stocks as a way to raise money. Bank loans or issuing bonds (IOUs) are also possible and this is called debt financing. However, issuing stocks to raise money is equity financing. These type of money does not require the company to pay back the money, or pay interest. IPO - initial public offering - the first sale of stock by the company.
Companies do not have to pay dividends and many do not.
Stocks are risky, but the return has historically been good. Over the long term and investment in stocks has historically had an average return of 10-12%.
Different types of stocks
Common stock - as described above.
Preferred stock - doesn't usually have the same voting rights (i.e. one vote per share). Investors are usually guaranteed a fixed dividend forever, and in the event of liquidation preferred shareholders and paid off before common shareholders. Preferred shares have similarity with bonds.
Other classes - other variants can also exist. E.g. a stock which gives voting rights of 10 votes per share.
When there are different classes of stock these are often referred to as Class A and Class B and these are often represented by placing a letter behind the ticker symbol e.g. BRKa and BRKb for a company whose ticker symbol is BRK.