infuerno.github.io

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

CSS

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

File permissions

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.

How stocks trade

What causes stock prices to change

Buying stocks

How to read a stock table/quote

The Bulls, the Bears and the Farm