PHP Flashcards

(212 cards)

1
Q

PHP can be run without a server.

A

False.

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

Who originally authored PHP, and when?

A

Rasmus Lerdorf, in 1994.

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

PHP is an open source product.

A

True.

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

PHP is a client-side scripting language whose scripts are embedded in HTML documents.

A

False, PHP is a server-side scripting language.

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

Is PHP statically or dynamically typed?

A

Dynamic.

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

PHP is purely interpreted, meaning what?

A

PHP is executed directly by the interpreter without being compiled into machine code first.

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

In what version of PHP was OOP functionality first added?

A

PHP 3.

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

In what version of PHP was object handling completely rewritten?

A

PHP 5.

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

How is internal PHP code embedded in HTML documents?

A

By inserting the code between the “<?php” and “?>” tags.

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

How is external PHP code embedded in HTML documents?

A

<?php include(“script.php”); ?>

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

What are the three ways of writing comments in PHP?

A

//…, #…, or /* … */

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

PHP variable names must always be preceded by a ‘$’.

A

True.

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

The following unassigned (unbound) variable has what value?
$x;

A

Null.

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

The following are both valid print methods:
print “Hello World! < br >”;
echo “Hello World! < br >”;

A

True.

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

The following are both valid print methods:
print (“Hello World! < br >”);
echo (“Hello World! < br >”);

A

True.

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

The following will print the value stored in $x:
echo “The value is: $x”;

A

True.

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

The following will print the value stored in $x:
echo “The value is: “ + $x;

A

False - this will print ‘0’, as the compiler attempts a numerical addition.

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

The following will print the value stored in $x:
echo “The value is: “.$x;

A

True.

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

The following will print the value stored in $x:
echo “The value is: “, $x;

A

True.

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

How many primitive types are there in PHP?

A

8.

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

What are the 3 categories of primitive types in PHP?

A

Scalar types, compound types, and special types.

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

What are the 4 (primitive) scalar types in PHP?

A

Boolean, Integer, Double, String.

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

What are the 2 (primitive) compound types in PHP?

A

Array and Object.

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

What are the 2 (primitive) special types in PHP?

A

Resource and NULL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
7E2 and 7e2 are both valid numeric literals in PHP.
True.
26
7,000 and 7,000,000 are both valid numeric literals in PHP.
False, neither are.
27
7.2E-2 is a valid numeric literal in PHP.
True.
28
0x0A and 0X0A are both valid numeric literals in PHP.
True.
29
By default, how many bytes per character in PHP?
1
30
The native PHP string is "Unicoded" (outputted as a Unicode string).
False.
31
You cannot output a Unicode string in PHP.
False - you can.
32
What character is used to delimit String literals in PHP?
Either single or double quotes.
33
What is the difference between single and double quote delimiters for string literals in PHP?
They do not allow the same substitutions.
34
What will be printed to the html document? echo 'he\\'s cool \n';
he's cool \n
35
What will be printed to the html document? $x = 'me'; echo 'he\\'s "cool\\" like $x';
he's "cool\\" like $x
36
What will be printed to the html document? echo "he\\'s cool \n";
he\\'s cool
37
What will be printed to the html document? $x = 'me'; echo "he\\'s \\"cool\\" like $x";
he\\'s "cool" like me
38
What will be displayed on the browser? $sum = 100; print('The sum is $sum \ Is that ok?');
The sum is $sum Is that ok?
39
The following will result in how many empty lines between characters 'a' and 'b'? print("a\n"); print("\"); print("b\n");
0
40
What is the output of the following code on the browser? $a = "Guns N\\' Roses"; print("\\$a");
$a
41
Boolean values in PHP are case senstive.
False.
42
In PHP, 0 and 0.0 evaluate to Boolean false.
True.
43
In PHP, both "" and NULL evaluate to Boolean false.
True.
44
In PHP, "0" evaluates to Boolean false.
True.
45
In PHP, '0' evaluates to Boolean false.
True.
46
The following code sets $x's value to what? unset($x);
NULL.
47
When does the following expression return true? isset($x);
When $x's value isn't NULL.
48
NULL is coerced to 0 if the context specifies a number.
True.
49
NULL is coerced to "null" if the context specifies a string.
False - it is coerced to an empty string.
50
What is phpinfo()?
phpinfo() is a predefined function that outputs information about the PHP configuration and environment.
51
What does round(5.5) return?
6
52
What does PHP's built in srand() function do?
srand() seeds a random number generator. There is no reason to do this past PHP 4.2.0.
53
What does rand() return if we don’t pass any arguments?
Nothing, rand() requires 2 arguments.
54
Which PHP function rounds doubles up, and which rounds down?
ceil() and floor().
55
Which php function returns the absolute value of a number?
abs()
56
Which PHP functions return the smallest/greatest values out of a list of values?
min() and max()
57
What operator is used in PHP for string concatenation?
.
58
Can commas be used for string concatenation?
No, but echo can take multiple arguments separated by commas.
59
The second line exhibits a valid method of concatenation. $a = "Hello "; $a .= "World!"
True.
60
What does strlen($str) return?
The length of $str.
61
What does strcmp($str1, $str2) return?
0 if the strings are identical, a negative number if the first string "comes" before the second (in the ASCII sequence) and a positive number if the second string comes before the first.
62
What does strpos($str1, $str2) return?
The character position of $str2's position in $str1, if it exists.
63
What does substr($str, $int) return?
The substring of $str, from index $int to the end.
64
What does chop($str) return?
$str but will all whitespace removed from its end.
65
What does trim($str) return?
$str but will all whitespace removed from both ends.
66
What does ltrim($str) return?
$str but will all whitespace removed from its beginning.
67
What does strtolower($str) return?
$str in all lowercase.
68
What does strtoupper($str) return?
$str in all uppercase.
69
what does the following code output? echo substr("Apples are good", 7, 2);
ar
70
what does the following code output? echo strcmp('a', 'c');
-2
71
What does strpos($str1, $str2) return if $str2 isn't found in $str1? (PHP)
False.
72
What does the following code output? $str = 'Look at the sea'; $str[strlen($str)-1] = 'e'; echo $str;
Look at the see
73
What is the output of the following code? $x = "hi"[1]; echo $x;
i
74
In PHP, how many ways are there to explicitly convert (cast) a value?
3
75
Write 3 ways to cast the value in $x to an integer in PHP.
(int) $x intval($x) settype($x, "integer")
76
How many ways are there to determine the type of a variable in PHP? What are they?
2 - gettype($x) or is_integer($x).
77
gettype($x) returns what if $x’s type can't be determined?
"unknown"
78
When can a string be converted (or coerced) to an integer? (PHP)
If it begins with an integer.
79
In PHP, when can a string be converted (or coerced) to a double? (4 cases)
If the string begins with a double, a period, an e or an E.
80
If a PHP string does not begin with a sign or a digit, then what number is it implicitly converted (coerced) to?
0
81
What is NULL coerced to if the context specifies a number?
0
82
What is NULL coerced to if the context specifies a string?
"" (empty string)
83
What is the output of the following? $str = '5.1'; print($str+1);
6.1
84
What is the output of the following? $str = '5.1'; print((int)$str);
5
85
What is the output of the following? $str = '5.5'; print((int)$str);
5 - casting rounds down.
86
Which is faster, echo or print?
echo - but only slightly.
87
When using printf, what does %10s specify?
A character string field of 10 characters.
88
When using printf, what does %6d specify?
An integer field of six digits.
89
When using printf, what does %5.2f specify?
A float or double field of 5 digits before the decimal, and 2 digits after the decimal.
90
What's the difference between != / == and !== / === ?
!= & == are type coercing.
91
Are >= & <= type coercing?
Yes.
92
Which is coerced when a string and a number are compared?
The string is coerced to a number, and numeric comparison is used.
93
If two strings are compared, PHP will never coerce both into numbers.
False - both will be coerced into numbers if both "look like" numbers.
94
Comparison operators should generally be used to compare strings.
False - strcmp() is (generally) preferred.
95
and, or, and xor are valid PHP Boolean operators.
True.
96
Which has higher precedence between && / || and the 'and' / 'or' Boolean operators?
&& / || have higher precendence.
97
switch ($val) { case "0": echo "zero"; break; default: echo "not zero"; } Is this a valid PHP switch statement structure?
Yes.
98
The following constructs a valid array: $list = array(1, 3, 5, 7, 9);
True.
99
The following constructs a valid array: $list = array(1 => "Cessna", 2 => "C210", 3 => 1960, 4 => "sold");
True.
100
The following keys can be replaced with strings: $list = array(1 => "Cessna", 2 => "C210", 3 => 1960, 4 => "sold");
True.
101
foreach($list as $key) allows you to iterate over the keys in $list.
False, this notation will iterate over the values, where each value can referred to by the $key variable.
102
Write a foreach loop that iterates over the keys AND values of a $list.
foreach($list as $key => $value)
103
The following allows you to iterate over keys: foreach($list as $key =>)
False - this results in a parse error.
104
Where is the value '5' placed in $list? $list[] = 5;
At the last available index.
105
$nums = array(1, 2, 3); Write a single line of code that assigns each value in $nums to its own variable, such as $a, $b, $c.
list($a, $b, $c) = $nums;
106
What method accepts an array, and returns an array of the keys from the passed array?
array_keys()
107
What method accepts an array, and returns an array of the values from the passed array? (PHP)
array_values()
108
What values are used as keys for the arrays constructed by array_keys() and array_values()?
Integer numbers, starting from 0.
109
What method returns true if the passed value exists as a key in the passed array?
array_key_exists()
110
What is the order of parameters passed to array_key_exists()?
array_key_exists($key, $list)
111
How can you delete an array in PHP?
unset($list);
112
The following is valid: unset($list[4]);
True.
113
How do you check whether $list is an actual array?
is_array($list)
114
How do you check whether the value $x exists in $list?
in_array($x, $list)
115
How do you get the length of an array?
sizeof($list)
116
What is the output of the following code? $list[4] = 1; $list[1] = 17; $list[2] = 3; unset($list[2]); foreach($list as $key => $value) echo "$key $value \";
4 1 1 17
117
Create an array of every word in $str (words separated by spaces).
explode(" ", $str)
118
Create a string containing every value in $list (separate each value by a space).
implode(" ", $list)
119
What is the output of the following code? $list = array("A", "B", "C", "D"); echo current($list);
A
120
What is the output of the following code? $list = array("A", "B", "C", "D"); echo next($list);
B
121
What does each($list) return?
A key-value pair of the "current" element. This method however has been deprecated.
122
Write another way to accomplish the following: $list[] = $x;
array_push($list, $x);
123
Write another way to accomplish the following: unset($list[sizeof($list) - 1]);
array_pop($list);
124
sort() sorts the array based off what? Are key/value relationships preserved?
sort() sorts the values and removes existing keys.
125
asort() sorts the array based off what? Are key/value relationships preserved?
asort() sorts the values and keeps key/value relationships - intended for hashes.
126
When is rsort() used?
To sort the values of an array into reverse order.
127
When is ksort used?
To sort the elements of an array by the keys, maintaining the key/value relationships.
128
When is krsort() used?
To sort the elements of an array by the keys into reverse order.
129
PHP functions can be called before being defined.
True - the entire document is parsed prior to execution.
130
Can you overload functions in PHP?
No.
131
Can PHP functions have a variable number of parameters?
Yes.
132
Can PHP function definitions be nested?
Yes.
133
Are PHP function names case sensitive?
No.
134
Can the return keyword be omitted from PHP function definitions?
Yes - its omission implies a void function.
135
Does PHP crash if more parameters are passed to a function than it is defined to accept?
No - the extra parameters are ignored.
136
Does PHP crash if less parameters are passed to a function than it is defined to accept?
No - the unmatched formal parameters are unbound (unless they've been given default values).
137
In PHP, by default, are parameters passed by value, or by reference?
By value.
138
How can you specify a pass-by-reference for a specific parameter in PHP?
Precede it with an ampersand: function f(&$x) { ... }
139
In PHP functions, can objects and arrays be returned in the same way as other primitive types?
Yes.
140
How can a function be made to return a reference?
Preceding the name of the function with an ampersand: function &f() { ... }
141
What is the output of the following code? $a = 1; $b = 2; $c = 0; function maxx(&$max,$a,$b) { if($a>$b) $max = $a; else $max = $b; $a = 0; } maxx($c,$a,$b); echo $c." ".$a;
2 1
142
Do function variables live (exist) past the end of the function execution?
Normally, no.
143
What is the scope of an undeclared variable in a function?
It has the scope of the function.
144
How could you access a variable $x defined outside a function, from within that function.
global $x; (Prior to the manipulation of $x).
145
What is the output of the following code? $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b;
3
146
What PHP function checks whether a string matches a specified regex? What is the order of its parameters?
preg_match(regex, str [, array]);
147
What does the optional 3rd parameter do in preg_match()?
Passing an empty array as a third parameter means that array will be filled with all matches of pattern matching operation.
148
What does preg_match() return?
A Boolean (either true or false).
149
What PHP method splits a string into an array using a specified regex as a delimiter?
preg_split(regex,str);
150
In PHP, forms may be handled by the same document that creates the form.
True.
151
What 2 methods can be used to transmit form data in PHP?
GET or POST.
152
How are names associated with their values in a query string?
By an equals sign: name = value
153
What types of characters are substituted with something else in query strings?
Special characters and spaces.
154
In query strings, what are special characters and spaces replaced by?
By % followed by the hex code for that character.
155
What common name must you give to all checkbox widgets to obtain an array of their values.
Something followed by '[]'.
156
Write a PHP class header for a Person object.
class Person { ... }
157
How would you define 2 attributes (a1 & a2) within a class definition in PHP?
public $a1; public $a2;
158
Define a constructor for a PHP class, C1, that has two attributes (a1 & a2).
public function __construct($a1, $a2) { $this->a1 = $a1; $this->a2 = $a2; }
159
Write a public class method that returns a string containing both its attributes, a1 & a2.
public function greeting() { return "{$this->a1} {$this->a2}"; }
160
Can class methods be static in PHP?
Yes.
161
What keyword should never be used in a static class method in PHP?
this (followed by ->)
162
How are cookies created in PHP?
setcookie() method.
163
What are the parameters accepted by the setcookie() method?
setcookie(cookie_name, cookie_value, lifetime)
164
Can cookies be created at any time?
No, they must be created before any other HTML is created by the script.
165
How could you iterate through all cookies?
Using the $_COOKIE array.
166
What does a cookie set look like in the http transaction? (server to client)
Set-Cookie: name=value;expires= …;path=/
167
What does a cookie get look like in the http transaction? (client to server)
Cookie: name=value
168
With your browser, you can view, delete and block cookies.
True.
169
Where are cookies stored (passed)?
In the HTTP request header.
170
Set a cookie to expire in a day from now.
setcookie("key", "value", time() + 86400);
171
How can you check whether a specific cookie has been set?
isset($_COOKIE["key"])
172
How can you delete a cookie?
setcookie("key", "", time() - 1);
173
Does JS have built in ways of setting/getting cookies?
No.
174
Write a JS function to set cookies.
function setCookie(c_name,value,exdays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; }
175
Create an array of all cookies in JS, similar to $_COOKIES in PHP.
ARRcookies=document.cookie.split(";");
176
Are cookies returned to all servers?
No, only to the servers that created them.
177
Why may systems that depend on cookies fail?
If the browser refuses to store them.
178
How are sessions similar to cookies?
Holds info about one single user, and passes info between pages.
179
How are sessions different from cookies?
Information is stored server side, and session info is deleted after the user has left the website (if not saved).
180
How does PHP track sessions?
PHP creates and maintains a session tracking id.
181
Where is the session id stored?
Either stored in a cookie or propagated in the URL.
182
What PHP function creates a session id?
session_start()
183
Following session id creation, what do subsequent calls to session_start() do?
Subsequent calls to session_start() retrieve any session variables that were previously registered in the session.
184
How do you register a session variable?
using $_SESSION['var_name'].
185
What restriction is put on the session_start() call if cookie-based sessions are being used?
It must be called before outputting anything to the browser.
186
How do you retrieve a session variable?
using $_SESSION['var_name'].
187
How do you delete a specific session variable?
unset($_SESSION['name']);
188
How do you completely destroy a session?
session_destroy();
189
Does session_destroy() unset global variables? What about the session cookie?
No, but all other data associated with the current session is destroyed.
190
How can session variables be used again following session_destory()?
By first calling session_start() again.
191
What method returns a "file pointer"?
fopen();
192
What are the parameters of fopen()?
fopen("filename.txt", "x"); where x is the use indicator.
193
List all the file use indicators.
r, r+, w, w+, a, a+.
194
Describe the 'r' use indicator.
Read only. File pointer initialized to the beginning of the file.
195
Describe the 'r+' use indicator.
Read and write and existing file. File pointer initialized at the beginning - read and write operations both move the same pointer.
196
Describe the 'w' use indicator.
Write only, initializes file pointer to the beginning of the file. Creates the file if it does not exist.
197
Describe the 'w+' use indicator.
Write and read, initializes file pointer to the beginning of the file. Creates the file if it does not exist. Always initializes file pointer to beginning before writing - destroying existing data.
198
Describe the 'a' use indicator.
Write only, initializes file pointer to the end of the file if it exists. Creates the file if it does not exist, and puts file pointer to the beginning.
199
Describe the 'a+' use indicator.
Read and write, creating the file if necessary; new data is written to the end of existing data.
200
How do you check if a file exists?
file_exists($filename)
201
How do you close a file?
f_close($file);
202
what are the parameters of the fread() method?
fread($file_var, $bytestoread)
203
What method returns the size of a file (in bytes)?
filesize($file);
204
What method returns the next car of an open $file?
fgetc($file)
205
Write a while loop that reads characters until the end of the file.
while(!feof($file_var)) { $ch = fgetc($file_var); }
206
Use fseek() to jump to the beginning of $file.
fseek($file, 0);
207
Use fseek() to jump to the 1000th byte of $file.
fseek($file, 1000);
208
Use fseek() to jump 100 bytes ahead of the current position in $file.
fseek($file, 100, SEEK_CUR);
209
Use fseek() to jump 100 bytes behind of the current position in $file.
fseek($file, -100, SEEK_CUR);
210
Use fseek() to jump 100 bytes behind the end of $file.
fseek($file, -100, SEEK_END);
211
Use a PHP function to write "Hello World" to $file.
fwrite($file, "Hello World");
212
Does fwrite() return anything?
Yes, it returns the number of bytes written.