October 15 - ndgriffeth/Class-Notes-and-Lectures GitHub Wiki
Turn in two possible questions and correct answers for the midterm, which will be held on Wednesday, October 23. I may use some of them if they are good enough.
The questions must satisfy:
-
They are on material that we have covered in class -- use the class lecture notes.
-
They are either discussion questions or code questions (no multiple choice, fill in the blank, one word, or true-false questions).
Example discussion question: What are the four layers in the Internet model and what function does each provide?Answer: From bottom to top, link layer (host to host connections on the same network), Internet layer (host to host connections on possibly different networks), transport layer (process to process connections over the Internet), and application layer (services that permit application clients and servers or peers to communicate with each other).
Example code question: Explain what the following code does. What are the possible outcomes, and why does each happen?
Answer: Stores a user's name and password in the file $_SERVER['DOCUMENT_ROOT']."/../users.txt" and creates a directory whose name is the same as the user's name, in $_SERVER['DOCUMENT_ROOT']."/../". Possible outcomes: It can't create the directory (because it already exists or isn't permitted), it can't update the file (because it doesn't exist or isn't permitted), or it is successful.
Neither of the above questions will count toward your assignment -- but I might use one or both on the midterm.
If you don't already have one, get a github account.
Download the github native app.
Start it running and configure with your github account.
Browse to my github repositories and fork the Examples repository to your account.
Clone the repository to your computer. This will give you all the examples.
Challenge question: How do you refresh it so you get the latest changes? (I'm not sure.)
For including any kind of code in a page. You must include all directives, including the php tags!
include: include the code contained in a file and process it. If the file can't be found, issue a warning.
require: same as include, but halt the program if the file can't be found.
Exercises:
- Include files containing either HTML or PHP code (use files you already have).
- Include files that are not in the document root hierarchy.
- Write a php program that conditionally includes one file if a boolean variable is set to true and a different one if it is set to false. Just make it a variable in the program and set it first to true and then to false to test it.
Note that the above suggests a way to hide files that require a login to access. In fact, you can associate files with a specific user. A simple way to do this is to create a directory outside of the document root and user root directories, named for the user, when (s)he registers (note that mkdir and chmod are php functions.
phpinfo();
$fp = fopen("my_file.txt", "a");
$astring = fread($fp,1024);
Function names must be spelled exactly as they appear in the prototype (but function names are not case-sensitive, while variable names are case-sensitive). A function may not exist in the version of PHP that you are using.
Exercise: Find the version of PHP running on your computer.
Here is how to define a function:
function <function_name>([<type1>] <arg1>,...,[<typek>] <argk>) { anything that is legal in a php script }
The function name:
- must not be the same as an existing function name.
- must contain only letter, digits, and underscores.
- must not begin with a number.
You can optionally specify the types of the arguments, but not the return type. PHP will check the arguments for compatibility with their types and give a fatal error if they are incorrect.
Exercises:
- Write a function called "whoami" to return a string containing your name and email address, separated by '<br />'.
- Write a function to add the numbers passed to it in an array and return the sum.
What is a function prototype?
bool phpinfo ([ int $what = INFO_ALL ] )
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
How do php function prototypes differ from Java and C prototypes?
Exercise: Define a php function
function test($val)
to return "positive" if $val > 0 and "negative" if $val < 0.
See what happens with each of the following calls to the function:
- test(1)
- test(-1)
- test("a")
Try again, but define it as
function test(int $val)
Note that you can't type the return value, but (in documentation) the function prototype should provide it.
- Variables defined in the top level of a file (i.e., outside of any functions) are visible throughout the file (global scope).
- Variables defined inside a function are visible from where they are defined to the closing brace of the function (function scope).
- Variables defined outside a function are not visible inside the function.
- A keyword global used in defining a variable inside a function can give it global scope.
- Super-globals are visible throughout the file.
- Code that is included obeys scoping rules as if it was copied into the file.
All variables used in a function must be passed to it or defined inside it.
Exercises:
- Write a php program to demonstrate that a variable defined outside a function is not visible inside the function, unless it is declared as global inside the function. (Hint: you may need to use the error log to see what went wrong. You are looking for a message that tells you the variable is undefined.)
- Write a php program to demonstrate that a variable defined in a function is not visible outside the function, even after the function definition.
Not commonly required in Web applications, so we'll skip it.
-
Write php code that conditionally includes the file "/var/log/apache2/error_log" if a boolean variable $test is true and "/var/log/apache2/access_log" if it is false.
-
What is a function prototype, and give two ways that it differs from a function.
-
Write a php function that takes an email address as a parameter and returns an array containing the username and the domain name as the first and second entries in the array.
-
In the following code, what is the scope of the variable $a? The variable $b? The variable $_POST?
$a=$_POST['name']; echo test($a); function test($x) { $b = ereg("abc", $x); return strlen($b); }