Reminder that HW3 Milestone 1 is due tonight:
If you need left-handed seating for the Final, please fill out the survey on the course home page (announcements) page by Wednesday!
Exploration session this week on websockets!
We have been working on incorporating some very useful suggestions from "Gots" and "Needs"!
Wrapping up PHP Syntax
Accessing GET/POST parameters in PHP
<?php
header("Content-type: text/plain");
$course = "CSE154";
$month = "November";
$abbr = $month.substr(0, 3);
$yesterday = "4";
echo "Hello {$course} student!";
echo "It's Monday, $abbr." + ($yesterday + 1) + "th!";
?php>
What bugs did you find?
<?php
header("Content-type: text/plain");
$course = "CSE154";
$month = "November";
$abbr = substr($month, 0, 3);
$yesterday = "4";
echo "Hello {$course} student!\n";
echo "It's Monday, {$abbr}. " . ($yesterday + 1) . "th!";
?>
for (initialization; condition; update) {
statements
}
for ($i = 0; $i < 10; $i++) {
echo "$i squared is " . $i * $i . "\n";
}
(remember . not + for string concatenation)
for ($i = 0; $i < 10; $i++) {
echo "$i squared is " . $i * $i . "\n";
}
for (let i = 0; i < 10; i++) {
console.log(i + " squared is " + (i * i));
}
for (int i = 0; i < 10; i++) {
System.out.println(i + " squared is " + (i * i));
}
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
while (condition) {
statements;
}
do {
statements;
} while (condition);
break
and
continue
keywords also behave as in Java (do not use these in this course)
function name(parameterName, ..., parameterName) {
statements;
}
function item_cost($qty, $unit_cost, $tax) {
$total_cost = $qty * $unit_cost; # e.g. 4 * 1.25 = 6.00
$tax_amount = $total_cost * $tax; # e.g. 6.00 * 0.1 = 0.60
$result = $total_cost + $tax_amount; # e.g. 6.00 + 0.6 = 6.60
return $result;
}
Very similar to JavaScript functions!
name(expression, ..., expression);
$qty = 4;
$cost = 1.25;
$total = item_cost($qty, $cost, 0.10);
$name = array(); # create
$name = array(value0, value1, ..., valueN);
$name[index] # get element value
$name[index] = value; # set element value
$name[] = value; # append value
$a = array(); # empty array (length 0)
$a[0] = 23; # stores 23 at index 0 (length 1)
$drinks = array("coffee", "tea", "water");
$drinks[] = "hot cocoa"; # add "hot cocoa" to end (at index 3)
array_push($drinks, "hot cocoa"); # identical to above line
array_push(arr, value)
$a = array(); # empty array (length 0)
$a[0] = 23; # stores 23 at index 0 (length 1)
$drinks = array("coffee", "tea", "water");
$drinks[] = "hot cocoa"; # add "hot cocoa" to end (at index 3)
$drink_count = count($drinks); # 4
let a = [];
a[0] = 23;
let drinks = ["coffee", "tea", "water"];
drinks[] = "hot cocoa";
let drinkCount = drinks.length;
int[] a = new int[1]; // need length when creating array in Java!
a[0] = 23;
String[] drinks = new String[]{"coffee", "tea", "water", ""};
drinks[3] = "hot cocoa";
int drinkCount = drinks.length;
Write a PHP function to_string which takes an array as a parameter and returns a comma-separated, bracketed string representation.
For example, if the following array is defined:
$drinks = array(“coffee”, “tea”, “water”);
The call to_string($drinks)
should return the string "[coffee, tea, water]"
.
One solution is given below:
function to_string($arr) {
$result = "[";
for ($i = 0; $i < count($arr) - 1; $i++) {
$result .= $arr[$i] . ", ";
}
if (count($arr) > 0) {
$result .= $arr[count($arr) - 1];
}
return $result . "]";
}
Working example in menu.php
(from php-examples-ii.zip
Function name(s) | Description |
---|---|
count | number of elements in the array |
print_r | print array's contents |
array_pop, array_push, array_shift, array_unshift | using an array as a stack/queue |
in_array, array_search, array_reverse sort, rsort, shuffle | searching and reordering |
array_fill, array_merge, array_intersect, array_diff, array_slice, range | creating, filling, filtering |
array_sum, array_product, array_unique, array_filter, array_reduce | processing elements |
$langs = array("HTML", "CSS", "JS", "PHP");
for ($i = 0; i < count($initials); $i++) {
$langs[$i] = strtolower($langs[$i]);
} # ("html", "css", "js", "php")
$html = array_shift($langs); # ("css", "js", "php")
array_pop($langs); # ("css", "js")
array_push($langs, "html"); # ("css", "js", "html")
array_reverse($langs); # ("html", "js", "css")
sort($langs); # ("css", "html", "js")
$html2 = array_slice($langs, 1, 2); # ("css", "js")
The array in PHP replaces many other data structures in Java
There are two common ways to make AJAX requests to a server.
$_GET
and $_POST
name
, reference it in PHP as
$_GET["name"]
$_POST["name"]
For a GET url with parameters passed, like:
hello.php?name=mowgli&age=2
<?php
$name = $_GET["name"];
$age = (int) $_GET["age"];
$dog_age = $age * 7;
echo "Hi {$name}! You are {$age} years old!\n";
echo "That's {$dog_age} in dog years!";
?>
hello.html (a simple webpage using hello.js to fetch from greeter.php using a GET request). You can practice updating these examples from php-examples-ii.zip!
For a POST to url with parameters passed, like:
let url = ..... // put url string here
let data = new FormData();
data.append("username", "Kyle");
data.append("password", "cse!54webz");
data.append("word", "duck");
data.append("definition", "a debugger friend");
fetch(url, {method: "POST", body: data})
...
PHP Code:
$username = $_POST["username"];
$password = $_POST["password"];
$users_pw_hash = db_lookup_hashed_pw($username);
if (password_hash($password) == $users_pw_hash) {
print("Successfully logged in!");
// code to update word/definition to a file on the server
}