Variables, Constants, and Expressions Flashcards

1
Q

What is a valid variable name?

A

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

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

What is a special variable that can’t be assigned?

A

$this

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

What is a special variable that can’t be assigned?

A

$this

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

What is the difference between assigning by value and assigning by reference?

A

By default, variables are always assigned by value; the entire value of the original expression is copied into the destination variable. After assigning one variable’s value to another, changing one of those variables will have no effect on the other.

When you assign by reference, the new variable points to the original variable. Changes to the new variable affect the original, and vice versa.

To assign by reference, simply prepend an ampersand to the beginning of the source variable.

Only named variables (and not unnamed expressions or functions) may be assigned by reference.

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

Why are superglobals called superglobals?

A

Because they are automatically

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

Why are superglobals called superglobals?

A

Because they are automatically available in every scope.

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

Why are superglobals called superglobals?

A

Because they are automatically available in every scope.

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

What is the scope of variables within a user-defined function?

A

The scope is limited to that function.

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

Does the default scope span included and required files?

A

Yes.

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

What do you need to do to use a global variable within a function?

A

Declare it as global within the function.

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

What is a second way to access variables from the global scope?

A

Use the special PHP-defined $GLOBALS associative array. $GLOBALS is a superglobal.

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

What is a static variable?

A

.

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

What is a static variable?

A

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

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

What is a recursive function?

A

A function which calls itself.

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

Can you assign values to a static variable if those values are the result of an expression.

A

No. This will result in a parse error.

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

Are the static and global modifiers implemented in terms of value or reference?

A

Reference.

17
Q

What are the four different scope types for PHP variables/

A

Local, function, global, and static.

18
Q

What is a variable variable?

A

A variable name that can be set and used dynamically.

$a = 'hello';
\$\$a = 'world'; //Now there is a variable $hello with value 'world'
19
Q

What is the ambiguity problem when using variable variables with arrays?

A

If you write $$a[1], the parser doesn’t know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable, and [1] as the index from that variable. So, for the first case, the syntax for resolving the ambiguity is ${$a[1]}, and for the second case, it’s ${$a}[1].

20
Q

When accessing class property names with variable variables, what is the scope?

A

The variable property name will be resolved within the scope from which the call is made.

21
Q

Where can variable variables NOT be used?

A

With PHP’s Superglobal arrays within functions or class methods. The variable $this also cannot be referenced dynamically.

22
Q

If a variable name in a GET or POST contains a period, what is it converted to?

A

An underscore.

23
Q

If an image is used as a form submit button, what additional data gets submitted with the form?

A

The variables sub_x and sub_y, which contain the coordinates of the user click within the image.

24
Q

If an image is used as a form submit button, what additional data gets submitted with the form?

A

The variables sub_x and sub_y, which contain the coordinates of the user click within the image.

25
Q

When can the setcookie() function be called?

A

Because cookies are part of the HTTP header, setcookie() must be called before any output is sent to the browser.

26
Q

How can you assign multiple values to a single cookie variable?

A

Assign it as an array:

setcookie(“MyCookie[foo]”, ‘Testing 1’, time()+3600);

This creates two separate cookies, although MyCookie will now be a single array in the script. To set just one cookie with multiple values, you can use serialize() or explode() on the value first.

27
Q

When will a cookie not replace a previous cookie by the same name?

A

When the path or domain is different.

28
Q

What is a bare string?

A

An unquoted string which does not match any known key or reserved words.

29
Q

What is a constant?

A

An identifier for a simple value that cannot change during execution of the script.

30
Q

How is a constant formatted?

A

A constant is case-sensitive by default, and always uppercase by convention. The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

31
Q

What is the scope of constants?

A

Global, just like superglobals.

32
Q

What is the scope of constants?

A

Global, just like superglobals.

33
Q

How is an expression defined?

A

“Anything that has a value”. Almost anything you write in PHP is an expression.

34
Q

What is a scalar value?

A

A value that you can’t break down into smaller pieces, unlike an array.

35
Q

Which scalar and non-scalar (composite) values does PHP support?

A

scalar: integer, float, string, boolean
composite: array, object

36
Q

What is the ternary conditional operator?

A

$first ? $second : $third

If $first is true, then $second is evaluated. If not, then $third is evaluated.

37
Q

What is the ternary conditional operator?

A

$first ? $second : $third

If $first is true, then $second is evaluated. If not, then $third is evaluated.

38
Q
$a = 1;
$b = $a++;

What does $b equal? What does $a equal?

A

Now, $b equals 1, and $a equals 2.

$a is assigned to $b BEFORE $a is incremented.