PHP Skills Tests: Difference between revisions
(Created page with "Category:PHP == Operator precedence == [http://php.net/manual/en/language.operators.precedence.php PHP's operator precedence]. {| ! Associativity ! Operators ! Additiona...") |
|||
| (33 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
== Operator precedence == | == Operator precedence == | ||
[http://php.net/manual/en/language.operators.precedence.php PHP's operator precedence] | PHP's operator precedence: <ref>[http://php.net/manual/en/language.operators.precedence.php PHP's operator precedence], PHP documenations</ref> | ||
{| | {| class="wikitable" | ||
! Associativity | ! Associativity | ||
! Operators | ! Operators | ||
! Additional information | ! Additional information | ||
|- | |- | ||
| non-associative | | style="white-space:nowrap;" | non-associative | ||
| `clone` `new` | | `clone` `new` | ||
| [http://php.net/manual/en/language.oop5.cloning.php clone] and [http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new new] | | [http://php.net/manual/en/language.oop5.cloning.php clone] and [http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new new] | ||
| Line 24: | Line 24: | ||
| `++` `--` `~` `(int)` `(float)` `(string)` `(array)` `(object)` `(bool)` `@` | | `++` `--` `~` `(int)` `(float)` `(string)` `(array)` `(object)` `(bool)` `@` | ||
| types and increment/decrement | | types and increment/decrement | ||
|- | |||
| non-associative | |||
| `instanceof` | |||
| types | |||
|- | |||
| right | |||
| `!` | |||
| logical | |||
|- | |||
| left | |||
| `*` `/` `%` | |||
| arithmetic | |||
|- | |||
| left | |||
| `+` `-` `.` | |||
| arithmetic and stirng | |||
|- | |||
| left | |||
| `<<` `>>` | |||
| [http://php.net/manual/en/language.operators.bitwise.php bitwise] | |||
|- | |||
| non-associative | |||
| `<` `<=` `>=` `>` | |||
| comparison | |||
|- | |||
| non-associative | |||
| `==` `!=` `===` `!==` `<>` `<=>` | |||
| comparison | |||
|- | |||
| left | |||
| `&` | |||
| [http://php.net/manual/en/language.operators.bitwise.php bitwise] and references | |||
|- | |||
| left | |||
| `^` | |||
| [http://php.net/manual/en/language.operators.bitwise.php bitwise] | |||
|- | |||
| left | |||
| `<nowiki>|</nowiki>` | |||
| [http://php.net/manual/en/language.operators.bitwise.php bitwise] | |||
|- | |||
| left | |||
| `&&` | |||
| http://php.net/manual/en/language.operators.logical.php logical] | |||
|- | |||
| left | |||
| `<nowiki>||</nowiki>` | |||
| [http://php.net/manual/en/language.operators.logical.php logical] | |||
|- | |||
| right | |||
| `??` | |||
| [http://php.net/manual/en/language.operators.comparison.php comparison] | |||
|- | |||
| left | |||
| `? :` | |||
| ternary | |||
|- | |||
| right | |||
| `=` `+=` `-=` `*=` `/=` `.=` `%=` `&=` `<nowiki>|=</nowiki>` `^=` `<<=` `>>=` `=>` | |||
| assignment | |||
|- | |||
| left | |||
| `and` | |||
| [http://php.net/manual/en/language.operators.logical.php logical] | |||
|- | |||
| left | |||
| `xor` | |||
| [http://php.net/manual/en/language.operators.logical.php logical] | |||
|- | |||
| left | |||
| `or` | |||
| [http://php.net/manual/en/language.operators.logical.php logical] | |||
|- | |||
| left | |||
| `,` | |||
| many uses | |||
|} | |||
=== Multiplication, division, addition, and subtraction === | |||
<syntaxhighlight lang="php"> | |||
echo 5 * 6 / 2 + 2 * 3; /* <<< result is 21 */ | |||
/** | |||
* Multiplication, division, and modulus have precedence over addition and subtraction. | |||
* Operations are performed left to right for operators with equal precedence. | |||
* So 5 * 6 = 30, 30 / 2 = 15, 2 * 3 = 6, and 15 + 6 = 21 | |||
* Result is 21. | |||
*/ | |||
</syntaxhighlight> | |||
=== String concatenation and string operators === | |||
<syntaxhighlight lang="php"> | |||
echo (2).(3*(print 3)); // "323" | |||
</syntaxhighlight> | |||
* This example also plays on the obscure fact that the `print` construct always returns the value `1`. | |||
* `echo` evaluates everything to the right. | |||
* First, `print 3` outputs `"3"`. | |||
* Then 3 is multiplied by the return value of `print`, i.e. `1`, for a result of `3`; | |||
* `2` is concatenated to `3` for `"23"` | |||
* `echo` outputs `"23"` after the `"3"` that has already been output, for a result of `"323"`. | |||
---- | |||
'''Q: What will be the output of the following code?''' | |||
<syntaxhighlight lang="php"> | |||
$foo = 5 + "10 things"; | |||
print $foo; // 15 | |||
</syntaxhighlight> | |||
{| | |||
! a. | |||
| 510 things | |||
|- | |||
! b. | |||
| 5 10 things | |||
|- | |||
! c. | |||
| 15 things | |||
|- | |||
! d. | |||
| 15 | |||
|- | |||
! e. | |||
| None of the above | |||
|} | |} | ||
'''A: (d) 15''' | |||
* One trick to this question is that there is no string concatenation operator (`.`) involved. | |||
* Still one thing to keep in mind is that the dot operator has the same precedence as `+` and `-`. | |||
* Non-empty non-numeric strings are converted to `0` when they are part of a mathematical operation. <ref>[http://php.net/manual/en/language.operators.string.php String Operators], PHP documenation</ref> | |||
* But a string is numeric if the numeric part comes at the beginning. | |||
** `"10 things" = 10` | |||
** `"things 10" = 0` | |||
** `"10things" = 10` | |||
** `"10 x 4" = 10` | |||
* `"10 things"` is converted to `10`, then added to `5`. | |||
* The final result is `15`. | |||
== Associativity == | == Associativity == | ||
| Line 30: | Line 171: | ||
TK | TK | ||
== Hexidecimal | == Data types == | ||
=== Initial values === | |||
'''Q: What is the initial value of an integer and float variable?''' | |||
<ol style="list-style-type=lower-latin"> | |||
<li>0</li> | |||
<li>1</li> | |||
<li>NULL</li> | |||
<li>Any value</li> | |||
</ol> | |||
'''A: (C) NULL''' | |||
PHP is loosely typed. An uninitialized variable doesn't have a integer or float type. | |||
A declared class member variable that holds no data: | |||
<syntaxhighlight lang="php"> | |||
class Test { | |||
public static $someVar; | |||
} | |||
var_dump(Test::$someVar); // outputs NULL | |||
</syntaxhighlight> | |||
=== Hexidecimal notation === | |||
Hex notation is indicated by a leading `0x` | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
| Line 39: | Line 209: | ||
This is hexidecimal notation. The value is calculated `0 * 1 = '''0'''` + `0 * 16 = '''0'''` + `5 * 256 = '''1280'''`. | This is hexidecimal notation. The value is calculated `0 * 1 = '''0'''` + `0 * 16 = '''0'''` + `5 * 256 = '''1280'''`. | ||
== | === Octal notation === | ||
Octal notation is indicated by a leading zero. | |||
<syntaxhighlight lang="php"> | |||
vardump($a = 010); // (1 * (8**1)) = 8 | |||
</syntaxhighlight> | |||
'''Q: What is the output of the following code?''' | |||
<syntaxhighlight lang="php"> | |||
echo 0500; // (5 * (8**2)) = 5 * 64 = 320 | |||
</syntaxhighlight> | |||
'''A: 320''' | |||
== `is_numeric()` == | === `is_numeric()` === | ||
* Valid values: `"200"`, `".25e4"`, `"20,2"` | * Valid values: `"200"`, `".25e4"`, `"20,2"` | ||
* Invalid values: "`$200`" | * Invalid values: "`$200`" | ||
== Arrays == | |||
=== Joining array elements into string === | |||
'''Q: What function should you use to join array elements with a glue string?''' | |||
<ol style="list-style-type:lower-latin;"> | |||
<li>join_str</li> | |||
<li>implode</li> | |||
<li>connect</li> | |||
<li>make_array</li> | |||
<li>None of the above</li> | |||
</ol> | |||
''my answer:'' '''b. implode''' | |||
=== Array comparison operators === | |||
'''Q: When comparing two arrays, what is the difference between `==` and `===`? | |||
<ol style="list-style-type:lower-latin;"> | |||
<li>`==` compares keys while `===` compares keys and values</li> | |||
<li>`===` also compares the order and types of the objects</li> | |||
<li>`===` compares the array references</li> | |||
<li>They are identical</li> | |||
<li>None of the above</li> | |||
</ol> | |||
'''A: (b) `===` also compares the order and types of the objects''' | |||
== Bitwise operators == | |||
{| class="wikitable" | |||
! Example | |||
! Name | |||
! Result | |||
|- | |||
| `$a & $b` | |||
| And | |||
| Bits that are set in both `$a` and `$b` are set. | |||
|- | |||
| `<nowiki>$a | $b</nowiki>` | |||
| Or (inclusive or) | |||
| Bits that are set in either `$a` or `$b` are set. | |||
|- | |||
| `$a ^ $b` | |||
| Xor (exclusive or) | |||
| Bits that are set in `$a` or `$b` but not both are set. | |||
|- | |||
| `~ $a` | |||
| Not | |||
| Bits that are set in `$a` are not set, and vice versa. | |||
|- | |||
| `$a << $b` | |||
| Shift left | |||
| Shift the bits of `$a` `$b` steps to the left (each step means "multiply by two") | |||
|- | |||
| `$a >> $b` | |||
| Shift right | |||
| Shift the bits of `$a` `$b` bits to the right (each step means "divide by two") | |||
|} | |||
=== Bitwise operators === | |||
<syntaxhighlight lang="php"> | |||
$a = (1<<0); /* $a = (0001) = (1*1) = 1 */ | |||
$b = (1<<1); /* $b = (0010) = (1*2) = 2 */ | |||
echo $b | $a; /* (0010 | 0001) = (0011) = ((1*2) + (1*1)) = 3 (bits set in either value are set in the result) */ | |||
</syntaxhighlight> | |||
=== Bitwise operators and precedence === | |||
'''Q: What will be the output of the following code?''' | |||
<syntaxhighlight lang="php"> | |||
$a = (1 << 0); /* $a = (1*1) = 1 */ | |||
$b = (1 << $a); /* $b = (1*2) = 2 */ | |||
$c = (1 << $b); /* $c = (1*4) = 4 */ | |||
echo ($c || $b) << 2 * $a | $a; | |||
/* ($c || $b) i.e. "4 or 2". Unless they are both false it will evaluated to true. Result is "true", or 1 */ | |||
/* multiplication > bitwise shift > bitwise or, in terms of precedence */ | |||
/* next operation, multiplication: 2 * $a, or 2 * 1 = 2 */ | |||
/* next operation, bitwise shift: 1 << 2 = 4; */ | |||
/* next operation, bitwise or: 4 | 1 = 100 | 001 = 5 */ | |||
</syntaxhighlight> | |||
'''A: 5''' | |||
=== Bit shifting === | |||
'''Q: What will be the output of the following code?''' | |||
<syntaxhighlight lang="php"> | |||
$a = 0x01; // 1 | |||
$b = 0x02; // 2 | |||
echo $a === $b >> $a; | |||
</syntaxhighlight> | |||
{| | |||
| '''a.''' | |||
| 0 | |||
|- | |||
| '''b.''' | |||
| 1 | |||
|- | |||
| '''c.''' | |||
| Syntax error | |||
|} | |||
'''A: (b) 1''' | |||
* `$a` equals 1 | |||
* `$b >> $a` shifts the bit in 2 over one place, resulting in 1 | |||
* `$a === $b >> $a` translates to "does 1 equal 1?" | |||
* Answer is "yes", i.e. true, i.e. 1 | |||
---- | |||
'''Q: What will be the result of the following code?''' | |||
<syntaxhighlight lang="php"> | |||
$a = 0x01; // 1 | |||
$b = 0x02; // 2 | |||
printf("%x", ($a << $b.$b)); // 400000 | |||
</syntaxhighlight> | |||
* `%x` format for `printf()` means output a hexidecimal value using lowercase letters. | |||
* `$b.$b` evaluates to `22`. | |||
* So, shift a bit representing 1 over 22 places, i.e. 2 to the power of 22. | |||
* When you shift the bit over, you multiply the value by 2, so the progression is like 1, 2, 4, 8, and then `8 * 2 = 16` which in hex is 10. Then `10 * 2` in hex results in 20 which is really `16 * 2` or `2**5` or `32`. | |||
* An algorithm for translating the shift to hex would be `(2**($shift_value % 4)) . str_pad(<nowiki>''</nowiki>, floor($shift_value/4), '0')` | |||
---- | |||
'''Q: What will be the output of the following code?''' | |||
<syntaxhighlight lang="php"> | |||
$a = (1<<0); // 0b0001, or 1 | |||
$b = (1<<1); // 0b0010, or 2 | |||
echo ($b|$a) << 2; | |||
</syntaxhighlight> | |||
{| | |||
! a. | |||
| 1 | |||
|- | |||
! b. | |||
| 2 | |||
|- | |||
! c. | |||
| 3 | |||
|- | |||
! d. | |||
| 4 | |||
|- | |||
! e. | |||
| 8 | |||
|- | |||
! f. | |||
| 12 | |||
|- | |||
! g. | |||
| Syntax error | |||
|} | |||
'''A: (f) 12''' | |||
* `($b|$a)` Or operator set the bit if it's set in either `$b` or `$a`, or `0b0010 | 0b0001` = `0b0011`, or `3` | |||
* Shift 3 two places yields `0b1100` or `12` | |||
* Also, shifting a bit left in effect multiplies the value by 2 for each time the bit is shifted, so `3 * 2 * 2 = 12` | |||
* Also, shifting a bit left if effect multiplies the value by 2 to the power of the shift amount, e.g. `3 * (2**2) = 3 * 4 = 12` | |||
== Magical constants == | == Magical constants == | ||
| Line 52: | Line 410: | ||
Magical constants are constants that change value depending on where in the code they are placed. | Magical constants are constants that change value depending on where in the code they are placed. | ||
The [http://php.net/manual/en/language.constants.predefined.php magical constants] | The magical constants in PHP: <ref>[http://php.net/manual/en/language.constants.predefined.php magical constants], PHP documentation</ref> | ||
* `__LINE__` | * `__LINE__` | ||
| Line 62: | Line 420: | ||
* `__METHOD__` | * `__METHOD__` | ||
* `__NAMESPACE__` | * `__NAMESPACE__` | ||
== Language constructs == | |||
=== Is `print()` a function? === | |||
`print()` is a language construct. <ref>[http://php.net/manual/en/function.print.php print()], PHP documentation</ref> | |||
<syntaxhighlight lang="php"> | |||
echo ((function_exists("print"))?("y"):("n")); /* <<< output is "n" */ | |||
echo ((function_exists("printf"))?("y"):("n")); /* <<< output is "y" */ | |||
echo ((function_exists("echo"))?("y"):("n")); /* <<< output is "n" */ | |||
</syntaxhighlight> | |||
* Both `echo` and `print` are constructs, not functions. This makes parentheses deceiving. | |||
* Both `echo` and `print` will output everything that follows to the right. | |||
* `echo` does not return a value. | |||
* `print` ''always'' returns `1`, never the value to the right of it. | |||
* Also, the string concatenation operator (`.`) has high precedence in PHP. But that's a red herring in the following example. | |||
<syntaxhighlight lang="php"> | |||
echo '1'.print(2)+3; // result: 511 | |||
/** | |||
* `echo` in this example prints everything that comes after it. It's necessary to evaluate everything to the right of 'echo' first. | |||
* `print` also outputs everything to the right, regardless of parentheses. | |||
* (1) Add (2) + 3 results in the value 5 | |||
* (2) print 5 outputs 5 to stdout, and returns the value `1`. | |||
* (3) Concatenate '1' with the return value of 'print' results in "11" | |||
* (4) 'echo' the output after what's already been sent to stdout, so "11" follows the "5". | |||
*/ | |||
</syntaxhighlight> | |||
=== Control structure alternative syntax === | |||
'''Q: Is the following code valid?''' | |||
<syntaxhighlight lang="php"> | |||
$a = 5; | |||
if ($a==5): | |||
echo "Hello"; | |||
echo "1"; | |||
else: | |||
echo "Not hello"; | |||
echo "2"; | |||
endif; | |||
</syntaxhighlight> | |||
'''A: Yes''' | |||
=== Grouping operators === | |||
'''Q: What is empty `()`?''' | |||
<ol style="list-style-type=lower-latin;"> | |||
<li>A function</li> | |||
<li>A language construct</li> | |||
<li>A variable</li> | |||
<li>A reference</li> | |||
<li>None of the above</li> | |||
</ol> | |||
''My first guess was (b) a language construct. After more considerations maybe it's more likely (e) None of the above?'' | |||
They are grouping operators, or delimiting expressions. I have never seen them used without something inside. Maybe the answer is "e". | |||
The question isn't clear about the context. I guess on their own they are meaningless. | |||
=== Gettext === | |||
'''Q: Can PHP use Gettext?''' | |||
<ol style="list-style-type=lower-latin;"> | |||
<li>yes</li> | |||
<li>no</li> | |||
</ol> | |||
I don't even know what this means, or what it's getting at. There is a [http://php.net/manual/en/book.gettext.php documentation page for `gettext`] which says, "The gettext functions implement an NLS (Native Language Support) API which can be used to internationalize your PHP applications." Which implies that PHP can use Gettext? Support is specific to the implementation, but there are gettext functions. | |||
No one offered an answer at [http://hlam.com.ua/expertrating/question.php?id=12925 this cheater's site]. | |||
== Language specifications == | |||
'''Q: What kind of regular expression does PHP include?''' (multiple choice) | |||
<ol style="list-style-type:lower-latin;"> | |||
<li>PCRE</li> | |||
<li>Java REGEX</li> | |||
<li>Ruby REGEX</li> | |||
<li>None of the above</li> | |||
</ol> | |||
'''A: (a) PCRE''' | |||
`preg` is a wrapper for the PCRE (Perl-Compatible Regular Expressions) library. | |||
However, an answer submitted to [http://hlam.com.ua/expertrating/question.php?id=12931 the cheaters' site] says the answer is (d) None of the above. | |||
---- | |||
'''Q: Is the PHP float type guaranteed to be 64 bit IEEE?''' | |||
<ol style="list-style-type:lower-latin;"> | |||
<li>yes</li> | |||
<li>no</li> | |||
</ol> | |||
"64 bit IEEE" translates to "double-precision floating-point format," meaning a float value occupies 64 bits (8 bytes) of memory. | |||
* 1 bit for the sign | |||
* 11 bits for the exponent | |||
* 52 bits for the fraction. | |||
From the [http://php.net/manual/en/language.types.float.php PHP documentation]: | |||
<blockquote>'''Although it depends on the system, PHP typically uses the IEEE 754 double precision format''', which will give a maximum relative error due to rounding in the order of 1.11e-16. </blockquote> | |||
Which to me suggests that while you should assume that it's IEEE 754 double precision, it is not in fact ''guaranteed''. | |||
== Built-in functions == | |||
'''Q: Can feof() be used with a file opened by fsockopen()?''' | |||
{| | |||
! a. | |||
| yes | |||
|- | |||
! b. | |||
| no | |||
|} | |||
'''A: (a) yes''' | |||
From [http://php.net/manual/en/function.feof.php the PHP documentation]: | |||
<blockquote>Parameters: | |||
`$handle` | |||
The file pointer must be valid, and must point to a file successfully opened by `fopen()` or `fsockopen()` (and not yet closed by `fclose()`)</blockquote> | |||
== Security == | |||
'''Q: What will be the output of the following code?''' | |||
<syntaxhighlight lang="php"> | |||
$a = "Hello"; | |||
if (md5($a) === md5($a)) { | |||
print "True"; | |||
} | |||
else { | |||
print "False"; | |||
} | |||
</syntaxhighlight> | |||
{| | |||
! a. | |||
| True | |||
|- | |||
! b. | |||
| False | |||
|} | |||
'''A: (b) False''' | |||
The hash that the `md5()` function produces for any given string will always be the same. | |||
The question is designed to give the suggestion that perhaps `md5()` scrambles a string. | |||
== Notes == | |||
<references /> | |||
Latest revision as of 22:46, 28 March 2016
Operator precedence[edit]
PHP's operator precedence: [1]
| Associativity | Operators | Additional information |
|---|---|---|
| non-associative | clone new
|
clone and new |
| left | [
|
array() |
| right | **
|
arithmetic |
| right | ++ -- ~ (int) (float) (string) (array) (object) (bool) @
|
types and increment/decrement |
| non-associative | instanceof
|
types |
| right | !
|
logical |
| left | * / %
|
arithmetic |
| left | + - .
|
arithmetic and stirng |
| left | << >>
|
bitwise |
| non-associative | < <= >= >
|
comparison |
| non-associative | == != === !== <> <=>
|
comparison |
| left | &
|
bitwise and references |
| left | ^
|
bitwise |
| left | `|
|
bitwise |
| left | &&
|
http://php.net/manual/en/language.operators.logical.php logical] |
| left | ||
|
logical |
| right | ??
|
comparison |
| left | ? :
|
ternary |
| right | = += -= *= /= .= %= &= |= ^= <<= >>= =>
|
assignment |
| left | and
|
logical |
| left | xor
|
logical |
| left | or
|
logical |
| left | ,` | many uses |
Multiplication, division, addition, and subtraction[edit]
echo 5 * 6 / 2 + 2 * 3; /* <<< result is 21 */ /** * Multiplication, division, and modulus have precedence over addition and subtraction. * Operations are performed left to right for operators with equal precedence. * So 5 * 6 = 30, 30 / 2 = 15, 2 * 3 = 6, and 15 + 6 = 21 * Result is 21. */
String concatenation and string operators[edit]
echo (2).(3*(print 3)); // "323"
- This example also plays on the obscure fact that the
printconstruct always returns the value1. echoevaluates everything to the right.- First,
print 3outputs"3". - Then 3 is multiplied by the return value of
print, i.e.1, for a result of3; 2is concatenated to3for"23"echooutputs"23"after the"3"that has already been output, for a result of"323".
Q: What will be the output of the following code?
$foo = 5 + "10 things"; print $foo; // 15
| a. | 510 things |
|---|---|
| b. | 5 10 things |
| c. | 15 things |
| d. | 15 |
| e. | None of the above |
A: (d) 15
- One trick to this question is that there is no string concatenation operator (
.) involved. - Still one thing to keep in mind is that the dot operator has the same precedence as
+and-. - Non-empty non-numeric strings are converted to
0when they are part of a mathematical operation. [2] - But a string is numeric if the numeric part comes at the beginning.
"10 things" = 10"things 10" = 0"10things" = 10"10 x 4" = 10
"10 things"is converted to10, then added to5.- The final result is
15.
Associativity[edit]
TK
Data types[edit]
Initial values[edit]
Q: What is the initial value of an integer and float variable?
- 0
- 1
- NULL
- Any value
A: (C) NULL
PHP is loosely typed. An uninitialized variable doesn't have a integer or float type.
A declared class member variable that holds no data:
class Test {
public static $someVar;
}
var_dump(Test::$someVar); // outputs NULL
Hexidecimal notation[edit]
Hex notation is indicated by a leading 0x
echo 0x500; 1280
This is hexidecimal notation. The value is calculated 0 * 1 = 0 + 0 * 16 = 0 + 5 * 256 = 1280.
Octal notation[edit]
Octal notation is indicated by a leading zero.
vardump($a = 010); // (1 * (8**1)) = 8
Q: What is the output of the following code?
echo 0500; // (5 * (8**2)) = 5 * 64 = 320
A: 320
is_numeric()[edit]
- Valid values:
"200",".25e4","20,2" - Invalid values: "
$200"
Arrays[edit]
Joining array elements into string[edit]
Q: What function should you use to join array elements with a glue string?
- join_str
- implode
- connect
- make_array
- None of the above
my answer: b. implode
Array comparison operators[edit]
Q: When comparing two arrays, what is the difference between == and ===?
==compares keys while===compares keys and values===also compares the order and types of the objects===compares the array references- They are identical
- None of the above
A: (b) === also compares the order and types of the objects
Bitwise operators[edit]
| Example | Name | Result |
|---|---|---|
$a & $b
|
And | Bits that are set in both $a and $b are set.
|
`$a | $b
|
Or (inclusive or) | Bits that are set in either $a or $b are set.
|
$a ^ $b
|
Xor (exclusive or) | Bits that are set in $a or $b but not both are set.
|
~ $a
|
Not | Bits that are set in $a are not set, and vice versa.
|
$a << $b
|
Shift left | Shift the bits of $a $b steps to the left (each step means "multiply by two")
|
$a >> $b
|
Shift right | Shift the bits of $a $b` bits to the right (each step means "divide by two")
|
Bitwise operators[edit]
$a = (1<<0); /* $a = (0001) = (1*1) = 1 */ $b = (1<<1); /* $b = (0010) = (1*2) = 2 */ echo $b | $a; /* (0010 | 0001) = (0011) = ((1*2) + (1*1)) = 3 (bits set in either value are set in the result) */
Bitwise operators and precedence[edit]
Q: What will be the output of the following code?
$a = (1 << 0); /* $a = (1*1) = 1 */ $b = (1 << $a); /* $b = (1*2) = 2 */ $c = (1 << $b); /* $c = (1*4) = 4 */ echo ($c || $b) << 2 * $a | $a; /* ($c || $b) i.e. "4 or 2". Unless they are both false it will evaluated to true. Result is "true", or 1 */ /* multiplication > bitwise shift > bitwise or, in terms of precedence */ /* next operation, multiplication: 2 * $a, or 2 * 1 = 2 */ /* next operation, bitwise shift: 1 << 2 = 4; */ /* next operation, bitwise or: 4 | 1 = 100 | 001 = 5 */
A: 5
Bit shifting[edit]
Q: What will be the output of the following code?
$a = 0x01; // 1 $b = 0x02; // 2 echo $a === $b >> $a;
| a. | 0 |
| b. | 1 |
| c. | Syntax error |
A: (b) 1
$aequals 1$b >> $ashifts the bit in 2 over one place, resulting in 1$a === $b >> $atranslates to "does 1 equal 1?"- Answer is "yes", i.e. true, i.e. 1
Q: What will be the result of the following code?
$a = 0x01; // 1
$b = 0x02; // 2
printf("%x", ($a << $b.$b)); // 400000
%xformat forprintf()means output a hexidecimal value using lowercase letters.$b.$bevaluates to22.- So, shift a bit representing 1 over 22 places, i.e. 2 to the power of 22.
- When you shift the bit over, you multiply the value by 2, so the progression is like 1, 2, 4, 8, and then
8 * 2 = 16which in hex is 10. Then10 * 2in hex results in 20 which is really16 * 2or2**5or32. - An algorithm for translating the shift to hex would be `(2**($shift_value % 4)) . str_pad('', floor($shift_value/4), '0')`
Q: What will be the output of the following code?
$a = (1<<0); // 0b0001, or 1 $b = (1<<1); // 0b0010, or 2 echo ($b|$a) << 2;
| a. | 1 |
|---|---|
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 8 |
| f. | 12 |
| g. | Syntax error |
A: (f) 12
($b|$a)Or operator set the bit if it's set in either$bor$a, or0b0010 | 0b0001=0b0011, or3- Shift 3 two places yields
0b1100or12 - Also, shifting a bit left in effect multiplies the value by 2 for each time the bit is shifted, so
3 * 2 * 2 = 12 - Also, shifting a bit left if effect multiplies the value by 2 to the power of the shift amount, e.g.
3 * (2**2) = 3 * 4 = 12
Magical constants[edit]
Magical constants are constants that change value depending on where in the code they are placed.
The magical constants in PHP: [3]
__LINE____FILE____DIR____FUNCTION____CLASS____TRAIT____METHOD____NAMESPACE__
Language constructs[edit]
Is print() a function?[edit]
print() is a language construct. [4]
echo ((function_exists("print"))?("y"):("n")); /* <<< output is "n" */
echo ((function_exists("printf"))?("y"):("n")); /* <<< output is "y" */
echo ((function_exists("echo"))?("y"):("n")); /* <<< output is "n" */
- Both
echoandprintare constructs, not functions. This makes parentheses deceiving. - Both
echoandprintwill output everything that follows to the right. echodoes not return a value.printalways returns1, never the value to the right of it.- Also, the string concatenation operator (
.) has high precedence in PHP. But that's a red herring in the following example.
echo '1'.print(2)+3; // result: 511 /** * `echo` in this example prints everything that comes after it. It's necessary to evaluate everything to the right of 'echo' first. * `print` also outputs everything to the right, regardless of parentheses. * (1) Add (2) + 3 results in the value 5 * (2) print 5 outputs 5 to stdout, and returns the value `1`. * (3) Concatenate '1' with the return value of 'print' results in "11" * (4) 'echo' the output after what's already been sent to stdout, so "11" follows the "5". */
Control structure alternative syntax[edit]
Q: Is the following code valid?
$a = 5;
if ($a==5):
echo "Hello";
echo "1";
else:
echo "Not hello";
echo "2";
endif;
A: Yes
Grouping operators[edit]
Q: What is empty ()?
- A function
- A language construct
- A variable
- A reference
- None of the above
My first guess was (b) a language construct. After more considerations maybe it's more likely (e) None of the above?
They are grouping operators, or delimiting expressions. I have never seen them used without something inside. Maybe the answer is "e".
The question isn't clear about the context. I guess on their own they are meaningless.
Gettext[edit]
Q: Can PHP use Gettext?
- yes
- no
I don't even know what this means, or what it's getting at. There is a documentation page for gettext which says, "The gettext functions implement an NLS (Native Language Support) API which can be used to internationalize your PHP applications." Which implies that PHP can use Gettext? Support is specific to the implementation, but there are gettext functions.
No one offered an answer at this cheater's site.
Language specifications[edit]
Q: What kind of regular expression does PHP include? (multiple choice)
- PCRE
- Java REGEX
- Ruby REGEX
- None of the above
A: (a) PCRE
preg is a wrapper for the PCRE (Perl-Compatible Regular Expressions) library.
However, an answer submitted to the cheaters' site says the answer is (d) None of the above.
Q: Is the PHP float type guaranteed to be 64 bit IEEE?
- yes
- no
"64 bit IEEE" translates to "double-precision floating-point format," meaning a float value occupies 64 bits (8 bytes) of memory.
- 1 bit for the sign
- 11 bits for the exponent
- 52 bits for the fraction.
From the PHP documentation:
Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16.
Which to me suggests that while you should assume that it's IEEE 754 double precision, it is not in fact guaranteed.
Built-in functions[edit]
Q: Can feof() be used with a file opened by fsockopen()?
| a. | yes |
|---|---|
| b. | no |
A: (a) yes
From the PHP documentation:
Parameters:
$handleThe file pointer must be valid, and must point to a file successfully opened by
fopen()orfsockopen()(and not yet closed byfclose())
Security[edit]
Q: What will be the output of the following code?
$a = "Hello";
if (md5($a) === md5($a)) {
print "True";
}
else {
print "False";
}
| a. | True |
|---|---|
| b. | False |
A: (b) False
The hash that the md5() function produces for any given string will always be the same.
The question is designed to give the suggestion that perhaps md5() scrambles a string.
Notes[edit]
- ↑ PHP's operator precedence, PHP documenations
- ↑ String Operators, PHP documenation
- ↑ magical constants, PHP documentation
- ↑ print(), PHP documentation