Basic Syntax and Types Flashcards

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
Q

What are namespaces?

A

A method of grouping related php code elements within a library or application.

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

How do you declare a namespace?

A

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.

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

What does a backslash in a namespace declaration mean?

A

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.

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

How do you import a namespace?

A

With the “use” operator.

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

What’s the difference between namespaces and classes?

A

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.

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

What are extensions?

A

PHP add-ons added in the php.ini config file.

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

What is PECL?

A

A repository for PHP extensions.

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

What are core extensions?

A

Extensions included with the PHP core.

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

What does “userland” refer to?

A

Applications that run in the user space (not the kernel).

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

What are the userland rules?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What are user .ini files?

A

.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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Which directives control the use of user .ini files?

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How can you set the value of a configuration option?

A

Generally, you can use ini_set within the php script. Some settings require php.ini or http.conf.

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

What are the two major factors affecting performance?

A
  • Reduced memory usage.

- Runtime delays.

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

How does garbage collection work in php?

A

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.

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

What is the opcode cache?

A

It stores precompiled php code, which often improves performance. It’s bundled with php 5.5.0 and later

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

What are the php super global variables?

A
  • $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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Know the PHP predefined error level constants.

A

http://php.net/manual/en/errorfunc.constants.php

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

What are two important recent changes to PHP that affect older code?

A
  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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Can environment variables be used in php.ini?

A

Yes:

memory_limit = ${PHP_MEMORY_LIMIT}

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

In php.ini, should you use single quotes or double quotes?

A

Double quotes.

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

In php.ini what lines are ignored?

A

Any text on a line after an unquoted semicolon, and text within brackets.

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

How do you set boolean values in php.ini?

A

TRUE: true, on, yes
FALSE: false, off, no, none

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

What are the PHP_INI_* modes, and what is the meaning of each one?

A

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.

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

When running PHP as an Apache module, how can you change configuration settings?

A

In httpd.conf or .htaccess files. You will need “AllowOverride All” or “AllowOverride Options” privileges to do so.

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

How do you change PHP config values in your script?

A

ini_set()

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

What are the Apache directives that allow you to change PHP configuration from within the Apache configuration files?

A

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

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

In httpd.conf, can you use PHP constants?

A

No.

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

In php.ini, can you use PHP constants?

A

Yes.

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

How do you change php configure options after installation?

A

Re-run the configure, make, and make install steps.

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

How do you change php configure options after installation?

A

Re-run the configure, make, and make install steps.

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

When is php.ini read?

A

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.

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

When is php.ini read?

A

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.

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

For pure PHP code files, why should the closing PHP tag be omitted?

A

It prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects.

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

What is the most efficient way to output large blocks of text?

A

Dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print().

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

What PHP tags should you use to embed PHP in XML or XHTML?

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

Do you need to have a semicolon terminating the last line of a PHP block?

A

No.

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

What types of comments does PHP allow?

A
// comment
/* multi-line comment */
# comment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

What types of comments does PHP allow?

A
// comment
/* multi-line C-style comment */
# comment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

Can C-style PHP comments be nested?

A

No.

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

Can C-style PHP comments be nested?

A

No.

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

When is the short php open tag allowed?

A

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
Q

What’s an efficient way to output large blocks of text in a php file?

A

Drop out of PHP parsing mode, rather than sending all of the text through echo() or print().

68
Q

What php opening and closing tags are complaint with XML or XHTML standards?

A
69
Q

What are the ASP-style PHP opening and closing tags?

A

They must be enabled via the asp_tags php.ini configuration file directive.

70
Q

What are the short echo tags?

A

= $variable ?> ##echoes $variable

Starting with PHP 5.4, the short echo tag is always recognized and valid.

71
Q

Do you need to have a semicolon terminating the last block of php code?

A

No, the closing tag of a block of php code automatically implies a semicolon.

72
Q

Does a closing tag for a block of php code include an immediately trailing newline if present?

A

Yes.

73
Q

Does a closing tag for a block of php code include an immediately trailing newline if present?

A

Yes.

74
Q

What are the eight primitive types that PHP supports?

A

4 scalar types: boolean, integer, float, string.
2 compound types: array, object
2 special types: resource, NULL

75
Q

What are the following pseudo types?

mixed
number
callback
array|object
void
$...
A

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
Q

What are the following pseudo types?

mixed
number
callback
array|object
void
$...
A

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
Q

What is the “double” type?

A

Same as float.

78
Q

How are variable types usually set?

A

Usually not by the programmer, but at runtime by PHP depending on the context in which that variable is used.

79
Q

What are these functions for: var_dump(), gettype(), is_type()?

A

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
Q

How do you forcibly convert a variable to a certain type?

A

Either cast the variable, or use the settype() function on it.

81
Q

How do you explicitly convert a value to boolean?

A

Use the (bool) or (boolean) casts.

82
Q

When converting to boolean, what is considered false?

A
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
Q

How can integers be specified?

A

Decimal (base 10), hexadecimal (base 16), octal (base 8), binary (base 2), binary integer literal.

84
Q

How can you use octal notation?

A

Precede the number with a zero.

85
Q

How can you use hexadecimal notation?

A

Precede the number with a 0x.

86
Q

How can you use binary notation?

A

Precede the number with 0b.

87
Q

What’s the usual maximum value for an integer?

A

About 2 billion.

88
Q

Does php support unsigned integers?

A

No.

89
Q

What constant determines integer size?

A

PHP_INT_SIZE.

90
Q

What constant determines maximum integer value?

A

PHP_INT_MAX.

91
Q

What happens if PHP encounters a number beyond the bounds of the integer type?

A

It will be treated as a float instead.

92
Q

What happens if an operation results in a number beyond the bounds of the integer type?

A

It will return a float instead.

93
Q

How do you explicitly convert a value to an integer?

A

Use either the (int) or (integer) casts, or the intval() function.

94
Q

If an operator, control structure, or function requires an integer argument, but is not given one, what will happen?

A

It will be automatically converted to an integer.

95
Q

What happens if a resource is converted to an integer?

A

The result will be the unique resource number assigned to the resource by PHP at runtime.

96
Q

What are the integer values of boolean FALSE and TRUE?

A

0 and 1.

97
Q

What happens when a float is converted to an integer?

A

It is rounded towards zero.

98
Q

What happens when a float is beyond the boundaries of an integer, but it is converted to an integer?

A

No warning or notice is issued. The result is undefined.

99
Q

If you’re converting to integer from something other than strings, floats, booleans, and resources, what happens?

A

The behavior is undefined, and can change without notice.

100
Q

How can floats be specified?

A

With any of the following syntaxes:

$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
101
Q

Why can’t you completely trust floating number results to the last digit?

A

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
Q

How are types besides strings converted to floats?

A

The value is first converted to integer, and then to float.

103
Q

How are types besides strings converted to floats?

A

The value is first converted to integer, and then to float.

104
Q

How should floats be compared for equality?

A

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
Q

What is the constant NAN?

A

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
Q

Does PHP offer native Unicode support?

A

No.

107
Q

How large can strings be?

A

Up to 2GB.

108
Q

How can string literals be specified?

A
  • single quoted
  • double quoted
  • heredoc syntax
  • nowdoc syntax
109
Q

When are backslashes not treated literally in strings?

A

When they are followed by a single quote or a backslash.

110
Q

What escape sequences are interpreted in double quoted strings?

A
\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
Q

What is the heredoc syntax?

A

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
Q

How does heredoc text behave?

A

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
Q

What is the nowdoc syntax?

A

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
Q

How are nowdocs identified?

A

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
Q

Are variables parsed within heredoc?

A

Yes.

116
Q

What is complex variable parsing?

A

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
Q

How are nowdocs significantly different than heredocs?

A

Unlike heredocs, nowdocs can be used in any static data context. The typical example is initializing class properties or constants.

118
Q

How are variable names parsed?

A

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
Q

Describe complex syntax.

A

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
Q

When you’re referencing a multi-dimensional array or a class property within a string, what should you do?

A

Always use braces around it.

121
Q

What do $str[8] and $str{8} mean?

A

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
Q

What do $str[8] and $str{8} mean?

A

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
Q

How can you convert a value to a string?

A

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
Q

What is a boolean TRUE value converted to as a string?

A

The string “1”.

125
Q

What is a boolean FALSE value converted to as a string?

A

The string “” (the empty string).

126
Q

What is an integer or float value converted to as a string?

A

A string representing the number textually (including the exponent part for floats).

127
Q

What are arrays converted to as strings? Objects? Resources?

A

The string “Array” or “Object”, or “Resource id#1”, where 1 is the resource number assigned to a resource by php at runtime.

128
Q

What is NULL converted to as a string?

A

”” (the empty string).

129
Q

What happens when a string is evaluated in numeric context?

A

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
Q

What happens when a string is evaluated in numeric context?

A

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
Q

How are string literals encoded?

A

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
Q

How do functions that operate on text handle encoding?

A
  • 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
Q

What is valid as an array key?

A

An integer or a string.

134
Q

What is valid as an array value?

A

Any type.

135
Q

What casts occur in an array?

A
  • 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
Q

What happens if multiple elements in an array declaration use the same key?

A

Only the last one will be used, as all others are overwritten.

137
Q

What is valid as an array value?

A

Any value of any type.

138
Q

What happens if multiple elements in an array declaration use the same key?

A

Only the last one will be used, as all others are overwritten.

139
Q

Can php arrays contain mixed keys of integers and strings?

A

Yes.

140
Q

What happens if a key is not specified in an array?

A

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
Q

Can square brackets and curly braces be used interchangeably for accessing array elements?

A

Yes.

142
Q

What happens when you attempt to access an array key which has not been defined?

A

An E_NOTICE-level error message is issued, and the result will be NULL.

143
Q

How do you remove a key/value pair from an array?

A

Call unset() on it.

144
Q

How do you delete an entire array?

A

Call unset() on it.

145
Q

What’s the difference between using unset() and array_values() on an array?

A

Unset does not cause the array to be re-indexed, whereas array_values does.

146
Q

Why is this wrong, even though it will work: $foo[bar]

A

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
Q

Why is this wrong, even though it will work: $foo[bar]

A

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
Q

When should you not quote an array key?

A

When the key is a constant or a variable.

149
Q

How are types converted to arrays?

A

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
Q

How are types converted to arrays?

A

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
Q

What is NULL when converted to an array?

A

An empty array.

152
Q

How can you change the values of an array directly?

A

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
Q

How can you change the values of an array directly?

A

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
Q

Does array assignment involve copying by value or reference?

A

Value.

$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
             // $arr1 is still array(2, 3)
$arr3 = &amp;$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
155
Q

What happens when values are converted to objects?

A

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
Q

What is a resource?

A

A special variable that holds a reference to an external resource.

157
Q

Do resources have to be freed manually?

A

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
Q

What is the type null?

A

It represents a variable with no value. The case-insensitive constant NULL is the only possible value of type null.

159
Q

When is a variable considered null?

A
  • It has been assigned to the constant NULL.
  • It has not been set to any value yet.
  • It has been unset().
160
Q

How do you cast a variable to the null type?

A

Use unset(). This will not remove the variable or unset its value. It will only return a NULL value.

161
Q

What is a callback function?

A

A callback is a function that is passed as an argument to another function. Callbacks are denoted by the “callable” type hint.

162
Q

Which type casts are allowed in PHP?

A
  • (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
Q

Which type casts are allowed in PHP?

A
  • (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
Q

What is an alternate way to cast to string?

A

Enclose in double quotes.

$a = 10; //integer
$b = "$a"; //string