Chapter 3 - Introduction to PHP Flashcards

1
Q

What does the GET method do?

A

Appennds the form data to the URL, changing the URL, by adding a query string.

By default, it follows the pattern ?value=property&value1=property1

Typically used when sending data to trigger a retrieval of more data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the POST method do?

A

Appends the form data inside the web request, thus not affecting the URL

Is theoretically limited only by the amount of data that PHP is set to accept and process

Typically used:

  • when sending data for storage/analysis
  • actions where the user will generally be forwarded back to the listing of sorts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give a functionally complete tag:

A

On the display end, avoid styling tag directly, add additional markup (i.e.<div>) instead. This keeps the tag focused only on logic and removes it’s dependance on display
</div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Once form data has been submitted, it is loaded onto what arrays?

A

$_GET annd $_POST

These are the first of the Superglobal arrays that PHP provides.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What makes arrays associative?

A

having strings for keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What other array is populated, but should not be used over $_GET and _POST?

A

$_REQUEST

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is echo used for?

A

Used to output information from the script. It is not a proper function, and thus does not use parantheses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can strings be contained in double AND single quotes?

A

Yes, but quotes must match in order to complete a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which quotes are unprocessed?

A

Single quotes are unprocessed - the contents are output verbatim.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which quotes are processed?

A

Double quotes are processed for variables, processed for special characters.

Example:

$test “Hello World”;

echo ‘$test’;
OUTPUT: $test

echo “$test”;
OUTPUT: Hello World

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is concatenation performed in PHP?

A

With the . operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is concatenation performed in PHP?

A

With the . operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why should we be careful outputting user submitted information?

A

The browser does not have a concept of where data comes from, it just processes whatever test, HTML and JS it receives from the web server.

Use htmlspecialchars as a simple way of nullifying user submitted data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How are comments added?

A

//this is a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What characters can you use to open and close comments almost anywhere to cover more than one line?

A

/* and */

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you create an array in PHP?

A

$team = array(‘Bill’, ‘Mary’, ‘Mike’, ‘Chris’, ‘Anne’);

OR

array();

PRINT

echo $team[3]; // Displays the name Chris

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you create a two-dimensional array in PHP?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you create a two-dimensional array in PHP?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the four rules you must follow when creating PHP variables?

A
  • Variable names must start with a letter of the alphabet or the _ (underscore) character.
  • Variable names can contain only the characters a–z, A–Z, 0–9, and _ (underscore).
  • Variable names may not contain spaces. If a variable must comprise more than one word, it should be separated with the _ (underscore) character (e.g., $user_name).
  • Variable names are case-sensitive. The variable $High_Score is not the same as the variable $high_score.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Describe the magic constant LINE

A

The current line number of the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Describe the magic constant FILE

A

The full path and filename of the file. If used inside an include, the name of the included file is returned. In PHP 4.0.2, __FILE__ always contains an absolute path with symbolic links resolved, whereas in older versions it might contain a relative path under some circumstances.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Describe the magic constant DIR

A

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Describe the magic constant

A

The function name. (Added in PHP 4.3.0.) As of PHP 5, returns the function name as it was declared (casesensitive). In PHP 4, its value is always lowercase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Describe the magic constant __CLASS__

A

The class name. (Added in PHP 4.3.0.) As of PHP 5, returns the class name as it was declared (casesensitive). In PHP 4, its value is always lowercase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Describe the magic constant __METHOD__

A

The class method name. (Added in PHP 5.0.0.) The method name is returned as it was declared (casesensitive).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Describe the magic constant __NAMESPACE__

A

The name of the current namespace (case-sensitive). This constant is defined at compile time. (Added in PHP 5.3.0.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the difference between echo and print commands?

A

Print is a function -like construct that takes a single parameter and has a return value which is always 1, whereas echo is purely a PHP language construct.

Both commands are constructs, so neither require the use of parentheses.

28
Q

What does this statement do?

$b ? print “TRUE” : print “FALSE”;

A

The value of a variable is TRUE or FALSE using print.

The question mark is simply a way of interrogating whether variable $b is true or false.

Whichever command is on the left of the following colon is executed if $b is true, whereas the command to the right is executed if $b is false.

29
Q

Would this command work if print was replaced by echo?

$b ? print “TRUE” : print “FALSE”;

A

No, it would display a Parse Error message.

30
Q

What is a function?

A

Functions are used to separate out sections of code that perform a particular task.

31
Q

How do you create a function?

A
32
Q

How do you create a function?

A
33
Q

What are local variables?

A

Local variables are variables that are created within, and can only be accessed by, a function.

34
Q

What are global variables?

A

When you want all of your code to access a variable, this should be used.

Also, some data may be large and complex, so you don’t want to keep passing it back and forth as arguments to functions.

35
Q

How do you declare a global variable?

A

global $is_logged_in;

36
Q

What are the predefined variables called?

A

superglobal variables

37
Q

Describe the superglobal $GLOBALS

A

All variables that are currently defined in the global scope of the script. The variable names are the keys of the array.

38
Q

Describe the superglobal $_SERVER

A

Information such as headers, paths, and script locations. The entries in this array are created by the web server, and there is no guarantee that every web server will provide any or all of these.

39
Q

Describe the superglobal $_GET

A

Variables passed to the current script via the HTTP GET method.

40
Q

Describe the superglobal $_POST

A

Variables passed to the current script via the HTTP POST method.

41
Q

Describe the superglobal $_FILES

A

Items uploaded to the current script via the HTTP POST method.

42
Q

Describe the superglobal $_COOKIE

A

Variables passed to the current script via HTTP cookies.

43
Q

Describe the superglobal $_SESSION

A

Session variables available to the current script.

44
Q

Describe the superglobal $_REQUEST

A

Contents of information passed from the browser; by default, $_GET, $_POST, and $_COOKIE.

45
Q

Describe the superglobal $_ENV

A

Variables passed to the current script via the environment method.

46
Q

Which is the only superglobal that does not include an underscore _?

A

$GLOBALS

47
Q

What tag is used to cause PHP to start interpreting program code? And what is the short form of the tag?

A

The tag used to start PHP interpreting code is , which can be shortened to … ?> but is not recommended practice.

48
Q

What are the two types of comment tags?

A

You can use // for a single-line comment or /* … */ to span multiple lines.

49
Q

Which character must be placed at the end of every PHP statement?

A

All PHP statements must end with a semicolon (;).

50
Q

Which symbol is used to preface all PHP variables?

A

With the exception of constants, all PHP variables must begin with $.

51
Q

What can a variable store?

A

Variables hold a value that can be a string, a number, or other data.

52
Q

What is the difference between $variable 1 and $variable ==1?

A

$variable = 1 is an assignment statement, whereas $variable == 1 is a comparison operator. Use $variable = 1 to set the value of $variable. Use $variable == 1 to find out later in the program whether $variable equals 1. If you mistakenly use $variable = 1 where you meant to do a comparison, it will do two things you probably don’t want: set $variable to 1 and return a true value all the time, no matter what its previous value was.

53
Q

Why do you suppose an underscore is allowed in variable names?

A

A hyphen is reserved for the subtraction operators. A construct like $currentuser would be harder to interpret if hyphens were also allowed in variable names and, in any case, would lead programs to be ambiguous.

54
Q

Are variable names case-sensitive?

A

Variable names are case-sensitive. So, for example,$This_Variable is not the same as $this_variable.

55
Q

Can you use spaces in variable names?

A

You cannot use spaces in variable names, as this would confuse the PHP parser. Instead, try using the _ (underscore).

56
Q

How do you convert one variable type to another?

A

To convert one variable type to another, reference it and PHP will automatically convert it for you.

57
Q

What is the difference between ++$j and $j++?

A

There is no difference between ++$j and $j++ unless the value of $j is being tested, assigned to another variable, or passed as a parameter to a function. In such cases, ++$j increments $j before the test or other operation is performed, whereas $j++ performs the operation and then increments $j.

58
Q

Are the operators && and and interchangeable?

A

Generally, the operators && and and are interchangeable except where precedence is important, in which case && has a high precedence, while and has a low one.

59
Q

How can you create a multiline echo or assignment?

A

You can use multiple lines within quotations marks or the

60
Q

Can you redefine a constant?

A

You cannot redefine constants because, by definition, once defined they retain their value until the program terminates.

61
Q

How do you escape a quotation mark?

A

You can use ' or " to escape either a single or double quote.

62
Q

What is the difference between the echo and print commands?

A

The echo and print commands are similar in that they are both constructs, except that print behaves like a PHP function and takes a single argument, while echo can take multiple arguments.

63
Q

What is the purpose of functions?

A

The purpose of functions is to separate discrete sections of code into their own, selfcontained sections that can be referenced by a single function name.

64
Q

How can you make a variable accessible to all parts of a PHP program?

A

You can make a variable accessible to all parts of a PHP program by declaring it as global.

65
Q

If you generate data within a function, what are a couple of ways to convey the data to the rest of the program?

A

If you generate data within a function, you can convey the data to the rest of the program by returning a value or modifying a global variable.

66
Q

What is the result of combining a string with a number?

A

When you combine a string with a number, the result is another string.

67
Q

What is the result of combining a string with a number?

A

When you combine a string with a number, the result is another string.