What is PHP, and what kind of pages is it used for?
PHP: Hypertext Preprocessor — the most popular server-side scripting language for creating dynamic web pages. It is open source and platform independent.
Where is PHP code executed, and how is it delimited?
It is interpreted by the server before the page is delivered to the client. Code goes between <?php and ?>; files end with .php.
How are PHP variables written and created?
They are preceded by a $ and are created the first time they are encountered. Names are case sensitive.
PHP comment styles
Single-line: // or #. Multiline: /* … */.
What is interpolation in PHP?
When a variable appears inside a double-quoted string, PHP inserts the variable's value where its name appears. It happens on the server before the document is sent.
What does "loosely typed" mean for PHP variables?
They can contain different types of data at different times.
List the PHP data types.
int/integer, float/double/real, string, bool/boolean, array, object, resource, NULL.
What are the resource and NULL types?
resource: an external source — usually information from a database. NULL: no value.
What do gettype and settype do?
gettype returns the current type of its argument. settype takes a variable and its new type and converts it.
What happens when settype converts "3.5 seconds" to double, then to integer?
To double: 3.5 (the number at the beginning of the string). To integer: 3 — doubles are truncated, so data can be lost. A string with no leading number evaluates to 0.
settype vs. casting
settype changes the variable's type (and value). Casting, e.g. (integer) $data, does not change the variable's content — it creates a temporary copy of its value in memory.
Which operator concatenates strings in PHP?
The concatenation operator, a period (.).
How does define work, and what happens if you assign to a constant later?
define(name, value[, caseInsensitive]) creates a named constant; constants are case sensitive by default. Assigning a value to a constant after it is declared is a syntax error.
What value does an uninitialized variable have in a numeric context?
0 — e.g., multiplying a number by an uninitialized variable results in 0, so initialize variables before use.
Where may PHP keywords not be used?
As function, method, class or namespace names.
How do you create an array element, and append one?
Assigning to an element of a nonexistent array creates the array; assigning with the index omitted, e.g. $first[] = "three";, appends a new element to the end.
What do count and array do?
count returns the total number of elements in an array. array creates an array containing its arguments; the first is at index 0.
What is an associative array, and how is it created?
An array with nonnumeric indices, created with =>: index on the left, value on the right, e.g. array( "January" => "first" ).
reset, key and next
Each array has an internal pointer. reset sets it to the first element; key returns the index of the current element; next moves it to the next element.
foreach with one variable vs. two variables after as
Two variables: the first gets the element's index, the second its value. One variable: it gets the element's value.
What does strcmp return?
-1 if the first string alphabetically precedes the second; 0 if they are equal; 1 if the first alphabetically follows the second.
What is a regular expression?
A series of characters that serve as pattern-matching templates (search criteria) in strings, text files and databases.
preg_match: arguments
The regular-expression pattern and the string to search; an optional third argument is an array that stores the matches (element 0 = the whole match, then each parenthetical part left to right).
Metacharacters ^, $ and .
^ matches the beginning of a string; $ matches the end; . matches any single character.
Bracket expressions, ranges and \b
A bracket expression [ … ] matches any single character from the list; a range uses a dash, e.g. [a-z]; \b before and after a group marks the beginning and end of a word (whole words).
How are character classes written?
Enclosed by [: and :], e.g. [:alpha:]; placed inside another set of brackets ([[:alpha:]]) they match all characters in the class.
preg_replace: arguments and return value
Three arguments: the pattern to match, the replacement string, and the string to search. It returns the modified string.
What are superglobal arrays? Give two.
Associative arrays predefined by PHP that hold variables from user input, the environment or the web server, accessible in any variable scope. $_GET and $_POST hold data from HTTP get and post requests.
What does die do?
Terminates script execution; its optional string argument is printed as the script exits.
Why must important form data be validated on the server?
Business logic must ensure invalid information is not stored in databases, and JavaScript may be disabled by the client.
mysql_connect: arguments and return value
The server's hostname, a username and a password. It returns a database handle, or false if the connection fails.
mysql_query and mysql_fetch_row
mysql_query runs a query on a database and returns a result resource (or false); it can also run INSERT or DELETE. mysql_fetch_row returns an array of the column values in the current row of the result.
What is a cookie, and which cookies can a server access?
Information a server stores in a text file on the client's computer to maintain information during and between browsing sessions. A server can access only the cookies it placed.
Session cookie vs. persistent cookie
A session cookie has no expiration date and lasts until the user closes the browser; a persistent cookie has an expiration date.
How is a cookie deleted, and why must setcookie come before any output?
Calling setcookie with only the name deletes it. Cookies are sent with the HTTP header, so setcookie must be called before any other output.
What should cookies not store?
E-mail addresses, passwords or private data.
isset and $$variable
isset determines whether $_POST contains keys for the form fields. $$variable is a variable variable: the value of the variable whose name equals the value of $variable.
What does mysql_real_escape_string do?
Inserts a backslash (\) before any special characters in the passed string.