PHP Programming

113 flashcards by MentisLibrary

Modern PHP 8 as it is written today: types and strict mode, arrays and strings, closures and generators, classes, enums and traits, exceptions, PDO, sessions and request handling, Composer and the PSR standards.

What does a PHP file need in order to enter PHP mode?
Everything outside <?php ... ?> is sent to output verbatim. A file containing only PHP opens with <?php and omits the closing tag.
What does declare(strict_types=1); change?
It switches that file from coercive to strict typing for the calls it makes: a string passed to an int parameter throws TypeError instead of being converted.
echo vs print
Both write to output. echo accepts several comma separated arguments and returns nothing; print takes one argument and returns 1, so it can sit inside an expression.
How does PHP treat letter case in identifiers?
Variable names are case-sensitive, so $name and $Name are different. Function, method, class and keyword names are case-insensitive.
== versus ===
=== requires the same type and the same value with no conversion. == converts the operands first, so '1' == 1 is true.
In PHP 8, what does 0 == 'foo' evaluate to?
false. PHP 8 compares a number against a non-numeric string by casting the number to a string, so the test becomes '0' == 'foo'.
What does the spaceship operator <=> return?
An int: negative when the left side is smaller, 0 when they are equal, positive when the left side is larger.
What do ?? and ??= do?
?? returns its left operand when that operand exists and is not null, otherwise the right one. ??= assigns the right operand only when the left is null or unset.
What does the nullsafe operator ?-> do?
If the operand on its left is null, the whole chain short-circuits to null instead of raising an error; otherwise the call proceeds normally.
How does match differ from switch?
match is an expression that returns a value, compares with ===, allows no fallthrough, and throws UnhandledMatchError when nothing matches.
require versus include
require raises a fatal error and halts when the file cannot be loaded; include emits a warning and execution continues. The _once forms skip a file that was already loaded.
const versus define()
const is resolved at compile time, is namespace aware, and is the only form usable inside a class. define() runs at runtime and can build the constant name dynamically.
isset() versus empty()
isset() is false for a variable that is unset OR holds null. empty() is true for null, false, 0, '0', '' and [], and never warns about an undefined variable.
How do you cast a value explicitly in PHP?
Put the target type in parentheses before the value: (int), (float), (string), (bool), (array). intdiv() and intval() are function equivalents with more control.
var_dump() versus print_r()
var_dump() prints the type, length and exact value of everything including nesting; print_r() prints a compact human view with no types and can return a string with a second argument of true.
When does PHP interpolate a variable inside a string?
Only inside double quotes, heredoc, and backtick shell syntax. Single quotes handle just \\' and \\\\.
Heredoc versus nowdoc
<<<EOT (heredoc) behaves like a double-quoted string and interpolates variables. <<<'EOT' (nowdoc) behaves like a single-quoted string and does not.
strlen() versus mb_strlen()
strlen() counts BYTES; mb_strlen() counts characters using the given encoding. For anything beyond ASCII they disagree.
Which functions replace strpos() !== false for substring checks?
str_contains(), str_starts_with() and str_ends_with(), all returning bool.
What does sprintf() do that concatenation does not?
It builds a string from a format template with typed placeholders, giving control over width, padding, precision and argument order.
explode() and implode()
explode($sep, $str) splits a string into an array on a separator; implode($glue, $arr) joins an array back into a string.
What does trim() strip by default, and how do you change it?
By default spaces, tabs, newlines, carriage returns, NUL and vertical tabs from both ends. A second argument replaces that set with your own character list.
What does preg_match() return?
1 if the pattern matched, 0 if it did not, and false on error. Captured groups are written into the array passed by reference as the third argument.
str_replace() versus preg_replace()
str_replace() does literal substring replacement and accepts arrays for search and replace. preg_replace() applies a regular expression and supports backreferences such as $1.
Which function escapes text before printing it into HTML?
htmlspecialchars($text, ENT_QUOTES, 'UTF-8'), which converts &, <, >, " and ' into entities.
What data structure is a PHP array?
An ordered map. The same type serves as list, dictionary, stack and queue; keys are integers or strings and insertion order is preserved.
How does PHP normalise array keys?
Integer-like strings become integers, bool becomes 0 or 1, float is truncated, and null becomes the empty string. Everything else stays a string.
What does array_map() do with keys?
It applies the callback to each value and returns a new array. With ONE input array string keys are preserved; with several arrays the result is renumbered from zero.
How does array_filter() decide what to keep, and what does it do to keys?
It keeps elements whose callback returns a truthy value and PRESERVES the original keys. With no callback it drops every falsy value.
How do you filter an array by its keys?
Pass a mode flag: ARRAY_FILTER_USE_KEY gives the callback the key, ARRAY_FILTER_USE_BOTH gives value then key.