Basic Syntax and Types Flashcards

(164 cards)

1
Q

How is the modulus operator used?

A

$m = 5 % 2; //$m == 1 (remainder when dividing)

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

What are bitwise operators?

A

They allow evaluation and manipulation of specific bits within an integer.

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

What is the XOR bitwise operator?

A

“Exclusive OR”. It performs a bitwise comparison between two numeric expressions, and evaluates to true when one and only one of the expressions evaluates to true.

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

Name the bitwise operators and their symbols.

A

and, or, xor, not, shift left, shift right

&, |, ^, ~, <>

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

What is “bit” short for?

A

binary digit

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

How does the bitwise ~ operator work?

A

It takes 1 integer after it, and it reverses each binary digit in an integer, from 0 to 1 or 1 to 0.

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

How do the bitshift operators work?

A

They shift the 0s and 1s in a binary representation of a number to the left or right.

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

In terms of multiplying or dividing, what do the &laquo_space;and&raquo_space; bitwise operators mean?

A

Each step of &laquo_space;means “multiply by two”. Each step of&raquo_space; means “divide by two”.

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

During bit shifting, what gets shifted in on the opposite side?

A

Left shifts have zeros shifted in on the right while the sign bit is shifted out on the left, meaning the sign of an operand is not preserved. Right shifts have copies of the sign bit shifted in on the left, meaning the sign of an operand is preserved.

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

What happens if bitwise operands are not integers?

A

If both operands for the &, | and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string. In all other cases, both operands will be converted to integers and the result will be an integer.

If the operand for the ~ operator is a string, the operation will be performed on the ASCII values of the characters that make up the string and the result will be a string, otherwise the operand and the result will be treated as integers.

Both operands and the result for the &laquo_space;and&raquo_space; operators are always treated as integers.

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

How does placement affect the increase/decrease operators?

A

If the operator is placed in front of the expression ($a += 2), then the variable is increased or decreased first. After expression, the reverse.

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

==

How do these operators differ?

!=
!==

A

With the additional equal sign (!== and ===), the data type of the two operands must also be identical. So, essentially, == means “equal” and === means “identical”.

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

What is the xor logical operator?

A

It evaluates to true if either operand BUT NOT both operands are true.

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

What are the execution operators?

A
  • shell_exec()

- enclosing in backticks

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

How do you assign variables by reference?

A

Attach an & to the beginning of the variable being assigned.

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

What’s the difference between assigning by value and assigning by reference?

A

Assigning by value essentially makes a copy; assigning by reference means that both values point to the same data.

$a = 1;
$b = &$a;
$a++;
//both $a and $b now equal 2

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

How does variable initialization work?

A

It’s not necessary to initialize values, but it’s good practice. Uninitialized values have their type set by default, depending on context. To detect if a variable has already been initialized, use isset().

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

What is the ternary operator?

A

(expression) ? valueiftrue : valueiffalse

SHORT FORM:
(valueiftrue) ?: valueiffalse

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

What do continue and break do?

A

Within loops, continue is used to pass over any remaining code within the iteration and return to the initial condition evaluation step.

Break halts execution of loops utilizing the for, foreach, while, do-while, and switch control structures.

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

What does return() do?

A

Called within a function, it halts execution of the function. Called within global scope, it halts execution of a script.

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

What does empty() consider to be an empty variable?

A

Empty string, empty array, 0, 0.0, “0”, NULL, FALSE, or a variable without an assigned value.

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

What does list() do?

A

It assigns a group of variables in one step. For example:

$info = array('a', 'b', 'c');
list($one, $two, $three) = $info;
//$one == 'a';

List only works on numerical arrays.

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

How are constants named?

A

They start with a letter or underscore, are case sensitive, and contain only alphanumeric characters and underscores. They may be defined and accessed anywhere in a program. They must be defined before use, and cannot be changed subsequently.

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

What are magic constants?

A

Predefined constants. Some can change value depending on where they are used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What are namespaces?
A method of grouping related php code elements within a library or application.
26
How do you declare a namespace?
With the keyword "namespace" at the beginning of the code file. It must be before any other code, except for the "declare" keyword. No non-php code can precede a namespace declaration, not even whitespace.
27
What does a backslash in a namespace declaration mean?
It ensures that the function is called from the global namespace, even if there is a function by the same name in the current namespace.
28
How do you import a namespace?
With the "use" operator.
29
What's the difference between namespaces and classes?
A class is an abstract definition of an object, while a namespace is an environment in which a class, function, or object can be defined.
30
What are extensions?
PHP add-ons added in the php.ini config file.
31
What is PECL?
A repository for PHP extensions.
32
What are core extensions?
Extensions included with the PHP core.
33
What does "userland" refer to?
Applications that run in the user space (not the kernel).
34
What are the userland rules?
- Function names use underscores between words, while class names use both the camelCase and PascalCase rules. - PHP will prefix any global symbols of an extension with the name of the extension, with some exceptions. - Iterators and Exceptions are however simply postfixed with "Iterator" and "Exception." - PHP reserves all symbols starting with __ as magical.
35
What are user .ini files?
.ini files used on a per-directory basis. - PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned. - These files are processed only by the CGI/FastCGI SAPI. - Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.
36
Which directives control the use of user .ini files?
- user_ini.filename sets the name of the file PHP looks for in each directory; if set to an empty string, PHP doesn't scan at all. The default is .user.ini. - user_ini.cache_ttl controls how often user INI files are re-read. The default is 300 seconds (5 minutes).
37
How can you set the value of a configuration option?
Generally, you can use ini_set within the php script. Some settings require php.ini or http.conf.
38
What are the two major factors affecting performance?
- Reduced memory usage. | - Runtime delays.
39
How does garbage collection work in php?
It clears circular-reference variables once prerequisites are met, either when the root buffer is full or gc_collect_cycles() is called. Garbage collection execution hinders performance.
40
What is the opcode cache?
It stores precompiled php code, which often improves performance. It's bundled with php 5.5.0 and later
41
What are the php super global variables?
- $GLOBALS - all variables available in global scope. - $_SERVER - information such as headers, paths, and script locations. - $_GET - variables passed to the script via the URL parameters. - $_POST - variables passed to the script via HTTP POST method. - $_FILES - items uploaded to the current script via HTTP POST. - $_COOKIE - variables passed to the current script via HTTP cookies. - $_SESSION - session variables available to the current script. - $_REQUEST - the contents of $_GET, $_POST, and $_COOKIE. - $_ENV - variables passed to the current script via the environment method.
42
Know the PHP predefined error level constants.
http://php.net/manual/en/errorfunc.constants.php
43
What are two important recent changes to PHP that affect older code?
1. As of PHP 5.4.0, the old HTTP_*_VARS arrays are not available. Instead, the superglobal arrays are used: $_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_ENV, $_REQUEST, $_SESSION. 2. External variables are no longer registered in the default scope by default; as of PHP 4.2.0, register_globals is off by default in php.ini. So, for example, previously, one could access $id from http://www.example.com/foo.php?id=42. Now, $_GET['id'] is used instead.
44
Can environment variables be used in php.ini?
Yes: memory_limit = ${PHP_MEMORY_LIMIT}
45
In php.ini, should you use single quotes or double quotes?
Double quotes.
46
In php.ini what lines are ignored?
Any text on a line after an unquoted semicolon, and text within brackets.
47
How do you set boolean values in php.ini?
TRUE: true, on, yes FALSE: false, off, no, none
48
What are the PHP_INI_* modes, and what is the meaning of each one?
PHP_INI_USER: Entry can be set in user ini scripts. PHP_INI_PERDIR: Entry can be set in php.ini, .htaccess, httpd.conf, or .user.ini. PHP_INI_SYSTEM: Entry can be set in php.ini or httpd.conf. PHP_INI_ALL: Entry can be set anywhere.
49
When running PHP as an Apache module, how can you change configuration settings?
In httpd.conf or .htaccess files. You will need "AllowOverride All" or "AllowOverride Options" privileges to do so.
50
How do you change PHP config values in your script?
ini_set()
51
What are the Apache directives that allow you to change PHP configuration from within the Apache configuration files?
php_value - sets the value of the specified directive. Clear a previously-set value by using "none" as the value. php_flag - sets a boolean configuration directive. php_admin_value - sets a value of a specified directive; can't be used in .htaccess files, and can't be overridden by .htaccess files or ini_set(). Clear a previously-set value by using "none" as the value. php_admin_flag - just like php_admin_value, but sets a boolean configuration directive. EXAMPLE: php_value include_path ".:/usr/local/lib/php" php_admin_flag engine on php_value include_path ".:/usr/local/lib/php" php_admin_flag engine on
52
In httpd.conf, can you use PHP constants?
No.
53
In php.ini, can you use PHP constants?
Yes.
54
How do you change php configure options after installation?
Re-run the configure, make, and make install steps.
55
How do you change php configure options after installation?
Re-run the configure, make, and make install steps.
56
When is php.ini read?
When PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.
57
When is php.ini read?
When PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.
58
For pure PHP code files, why should the closing PHP tag be omitted?
It prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects.
59
What is the most efficient way to output large blocks of text?
Dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print().
60
What PHP tags should you use to embed PHP in XML or XHTML?
61
Do you need to have a semicolon terminating the last line of a PHP block?
No.
62
What types of comments does PHP allow?
``` // comment /* multi-line comment */ # comment ```
63
What types of comments does PHP allow?
``` // comment /* multi-line C-style comment */ # comment ```
64
Can C-style PHP comments be nested?
No.
65
Can C-style PHP comments be nested?
No.
66
When is the short php open tag allowed?
When it's enabled using the short_open_tag php.ini configuration file directive, or if PHP is configured with the --enable-short-tags option.
67
What's an efficient way to output large blocks of text in a php file?
Drop out of PHP parsing mode, rather than sending all of the text through echo() or print().
68
What php opening and closing tags are complaint with XML or XHTML standards?
69
What are the ASP-style PHP opening and closing tags?
They must be enabled via the asp_tags php.ini configuration file directive.
70
What are the short echo tags?
= $variable ?> ##echoes $variable Starting with PHP 5.4, the short echo tag is always recognized and valid.
71
Do you need to have a semicolon terminating the last block of php code?
No, the closing tag of a block of php code automatically implies a semicolon.
72
Does a closing tag for a block of php code include an immediately trailing newline if present?
Yes.
73
Does a closing tag for a block of php code include an immediately trailing newline if present?
Yes.
74
What are the eight primitive types that PHP supports?
4 scalar types: boolean, integer, float, string. 2 compound types: array, object 2 special types: resource, NULL
75
What are the following pseudo types? ``` mixed number callback array|object void $... ```
mixed - a parameter may accept multiple (but not necessarily all) types. number - a parameter can be either integer or float. callback - parameter is a callback function. array|object - parameter can be either array or object. void - as a return type, means that the return value is useless. In a parameter list, means that the function doesn't accept any parameters. $... - in function prototypes, means "and so on". This variable name is used when a function can take an endless number of arguments.
76
What are the following pseudo types? ``` mixed number callback array|object void $... ```
mixed - a parameter may accept multiple (but not necessarily all) types. number - a parameter can be either integer or float. callback - parameter is a callback function. array|object - parameter can be either array or object. void - as a return type, means that the return value is useless. In a parameter list, means that the function doesn't accept any parameters. $... - in function prototypes, means "and so on". This variable name is used when a function can take an endless number of arguments.
77
What is the "double" type?
Same as float.
78
How are variable types usually set?
Usually not by the programmer, but at runtime by PHP depending on the context in which that variable is used.
79
What are these functions for: var_dump(), gettype(), is_type()?
var_dump() - check the type and value of an expression. gettype() - get a human-readable representation of a type for debugging. is_type() - check for a certain type.
80
How do you forcibly convert a variable to a certain type?
Either cast the variable, or use the settype() function on it.
81
How do you explicitly convert a value to boolean?
Use the (bool) or (boolean) casts.
82
When converting to boolean, what is considered false?
``` the boolean FALSE, but not the string "false" integer 0 float 0.0 empty string string "0" array with zero elements object with zero member variables special type NULL SimpleXML objects created from empty tags ``` Every other value is true, including -1 and any resource.
83
How can integers be specified?
Decimal (base 10), hexadecimal (base 16), octal (base 8), binary (base 2), binary integer literal.
84
How can you use octal notation?
Precede the number with a zero.
85
How can you use hexadecimal notation?
Precede the number with a 0x.
86
How can you use binary notation?
Precede the number with 0b.
87
What's the usual maximum value for an integer?
About 2 billion.
88
Does php support unsigned integers?
No.
89
What constant determines integer size?
PHP_INT_SIZE.
90
What constant determines maximum integer value?
PHP_INT_MAX.
91
What happens if PHP encounters a number beyond the bounds of the integer type?
It will be treated as a float instead.
92
What happens if an operation results in a number beyond the bounds of the integer type?
It will return a float instead.
93
How do you explicitly convert a value to an integer?
Use either the (int) or (integer) casts, or the intval() function.
94
If an operator, control structure, or function requires an integer argument, but is not given one, what will happen?
It will be automatically converted to an integer.
95
What happens if a resource is converted to an integer?
The result will be the unique resource number assigned to the resource by PHP at runtime.
96
What are the integer values of boolean FALSE and TRUE?
0 and 1.
97
What happens when a float is converted to an integer?
It is rounded towards zero.
98
What happens when a float is beyond the boundaries of an integer, but it is converted to an integer?
No warning or notice is issued. The result is undefined.
99
If you're converting to integer from something other than strings, floats, booleans, and resources, what happens?
The behavior is undefined, and can change without notice.
100
How can floats be specified?
With any of the following syntaxes: ``` $a = 1.234; $b = 1.2e3; $c = 7E-10; ```
101
Why can't you completely trust floating number results to the last digit?
Because base 2 is used internally, and and floats can't be converted into their internal binary counterparts without a small loss of precision. Thus, testing floating point values for equality is problematic.
102
How are types besides strings converted to floats?
The value is first converted to integer, and then to float.
103
How are types besides strings converted to floats?
The value is first converted to integer, and then to float.
104
How should floats be compared for equality?
Declare a "unit roundoff" as the smallest acceptable difference in calculations, like this: ``` $a = 1.23456789; $b = 1.23456780; $epsilon = 0.00001; ``` if(abs($a-$b)
105
What is the constant NAN?
An undefined or unrepresentable value in floating-point calculations. Any comparisons of this value against any other value, including itself, will have a value of FALSE.
106
Does PHP offer native Unicode support?
No.
107
How large can strings be?
Up to 2GB.
108
How can string literals be specified?
- single quoted - double quoted - heredoc syntax - nowdoc syntax
109
When are backslashes not treated literally in strings?
When they are followed by a single quote or a backslash.
110
What escape sequences are interpreted in double quoted strings?
``` \n - linefeed \r - carriage return \t - horizontal tab \v - vertical tab \e - escape \f - form feed \\ - backslash \$ - dollar sign \" - double quote \[0-7]{1,3} - the sequence of characters matching the regular expression is a character in octal notation \x[0-9A-Fa-f]{1,2} - the sequence of characters matching the regular expression is a character in hexadecimal notation ```
111
What is the heredoc syntax?
It's used to delimit strings: >>>, then an identifier, then a newline. Then the string, and the same identifier again to close the quotation. The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. The line with the closing identifier must contain no other characters, except a semicolon. The identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. The first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline. If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line. Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.
112
How does heredoc text behave?
Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.
113
What is the nowdoc syntax?
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc.
114
How are nowdocs identified?
A nowdoc is identified with the same sequence used for heredocs, but the identifier which follows is enclosed in single quotes. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
115
Are variables parsed within heredoc?
Yes.
116
What is complex variable parsing?
The complex syntax can be recognised by the curly braces surrounding the expression. It allows the use of complex expressions. Functions, method calls, static class variables, and class constants inside {$} work. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined.
117
How are nowdocs significantly different than heredocs?
Unlike heredocs, nowdocs can be used in any static data context. The typical example is initializing class properties or constants.
118
How are variable names parsed?
If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. The same rules apply to object properties as to simple variables.
119
Describe complex syntax.
Any scalar variable, array element or object property with a string representation can be included via this syntax. It can be used to reference multi-dimensional arrays and class properties within strings, as well as functions, method calls, static class variables and class constants. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$.
120
When you're referencing a multi-dimensional array or a class property within a string, what should you do?
Always use braces around it.
121
What do $str[8] and $str{8} mean?
The eighth zero-based offset character of the string $str. You can access and modify strings like this. The offsets have to be either integers or integer-like strings, or a warning will be thrown.
122
What do $str[8] and $str{8} mean?
The eighth zero-based offset character of the string $str. You can access and modify strings like this. The offsets have to be either integers or integer-like strings, or a warning will be thrown.
123
How can you convert a value to a string?
Use the (string) cast or the strval() function. String conversion is automatically done in the scope of an expression where a string is needed, like when using the echo() or print() functions, or when a variable is compared to a string.
124
What is a boolean TRUE value converted to as a string?
The string "1".
125
What is a boolean FALSE value converted to as a string?
The string "" (the empty string).
126
What is an integer or float value converted to as a string?
A string representing the number textually (including the exponent part for floats).
127
What are arrays converted to as strings? Objects? Resources?
The string "Array" or "Object", or "Resource id#1", where 1 is the resource number assigned to a resource by php at runtime.
128
What is NULL converted to as a string?
"" (the empty string).
129
What happens when a string is evaluated in numeric context?
If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
130
What happens when a string is evaluated in numeric context?
If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float. The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
131
How are string literals encoded?
string will be encoded in whatever fashion it is encoded in the script file. Thus, if the script is written in ISO-8859-1, the string will be encoded in ISO-8859-1 and so on. However, this does not apply if Zend Multibyte is enabled; in that case, the script may be written in an arbitrary encoding (which is explicity declared or is detected) and then converted to a certain internal encoding, which is then the encoding that will be used for the string literals.
132
How do functions that operate on text handle encoding?
- Some functions assume that the string is encoded in some (any) single-byte encoding, but they do not need to interpret those bytes as specific characters. - Some functions are passed the encoding of the string, possibly they also assume a default if no such information is given. - Some functions use the current locale, but operate byte-by-byte. - Some functions just assume the string is using a specific encoding, usually UTF-8.
133
What is valid as an array key?
An integer or a string.
134
What is valid as an array value?
Any type.
135
What casts occur in an array?
- Strings containing valid integers will be cast to the integer type. - Floats are cast to integers, with the fractional part truncated. - Booleans are cast to integers. - Null is cast to the empty string. - Arrays and objects can not be used; doing so will result in a warning.
136
What happens if multiple elements in an array declaration use the same key?
Only the last one will be used, as all others are overwritten.
137
What is valid as an array value?
Any value of any type.
138
What happens if multiple elements in an array declaration use the same key?
Only the last one will be used, as all others are overwritten.
139
Can php arrays contain mixed keys of integers and strings?
Yes.
140
What happens if a key is not specified in an array?
PHP will use the increment of the largest previously-used integer key. That integer does not need to currently exist in the array, as long as it once existed in it.
141
Can square brackets and curly braces be used interchangeably for accessing array elements?
Yes.
142
What happens when you attempt to access an array key which has not been defined?
An E_NOTICE-level error message is issued, and the result will be NULL.
143
How do you remove a key/value pair from an array?
Call unset() on it.
144
How do you delete an entire array?
Call unset() on it.
145
What's the difference between using unset() and array_values() on an array?
Unset does not cause the array to be re-indexed, whereas array_values does.
146
Why is this wrong, even though it will work: $foo[bar]
In this case, bar is a constant. PHP is converting the bare string into a proper string, as long as there is no constant named 'bar'.
147
Why is this wrong, even though it will work: $foo[bar]
In this case, bar is a constant. PHP is converting the bare string (an unquoted string which does not correspond to any known signal) into a string which contains the bare string, as long as there is no constant named 'bar'.
148
When should you not quote an array key?
When the key is a constant or a variable.
149
How are types converted to arrays?
For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue). If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.
150
How are types converted to arrays?
For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue). If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.
151
What is NULL when converted to an array?
An empty array.
152
How can you change the values of an array directly?
Pass them by reference, like this: foreach($colors AS &$color) { $color = strtoupper($color); } Previously, you had to do this: foreach($colors AS $key => $color) { $colors[$key] = strtoupper($color); }
153
How can you change the values of an array directly?
Pass them by reference, like this: foreach($colors AS &$color) { $color = strtoupper($color); } Previously, you had to do this: foreach($colors AS $key => $color) { $colors[$key] = strtoupper($color); }
154
Does array assignment involve copying by value or reference?
Value. ``` $arr1 = array(2, 3); $arr2 = $arr1; $arr2[] = 4; // $arr2 is changed, // $arr1 is still array(2, 3) ``` ``` $arr3 = &$arr1; $arr3[] = 4; // now $arr1 and $arr3 are the same ```
155
What happens when values are converted to objects?
If an object is converted to an object, it is not modified. If a value of any other type is converted, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values, with the exception of numeric keys which will be inaccessible unless iterated. For any other value, a member variable named scalar will contain the value.
156
What is a resource?
A special variable that holds a reference to an external resource.
157
Do resources have to be freed manually?
No. A resource with no more references to it is detected automatically, and it is freed by the garbage collector. Therefore it is rarely necessary to free resources manually. Persistent database links are an exception to this rule; they are NOT destroyed by the garbage collector.
158
What is the type null?
It represents a variable with no value. The case-insensitive constant NULL is the only possible value of type null.
159
When is a variable considered null?
- It has been assigned to the constant NULL. - It has not been set to any value yet. - It has been unset().
160
How do you cast a variable to the null type?
Use unset(). This will not remove the variable or unset its value. It will only return a NULL value.
161
What is a callback function?
A callback is a function that is passed as an argument to another function. Callbacks are denoted by the "callable" type hint.
162
Which type casts are allowed in PHP?
- (int), (integer) - (bool), (boolean) - (float), (double), (real) - cast to float - (string) - (array) - (object) - (unset) - cast to NULL Tabs and spaces are allowed inside the parentheses.
163
Which type casts are allowed in PHP?
- (int), (integer) - (bool), (boolean) - (float), (double), (real) - cast to float - (string) - (array) - (object) - (unset) - cast to NULL Tabs and spaces are allowed inside the parentheses.
164
What is an alternate way to cast to string?
Enclose in double quotes. ``` $a = 10; //integer $b = "$a"; //string ```