PHP Skills Tests: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
Line 257: Line 257:


== Arrays ==
== Arrays ==
=== Joining array elements into string ===


'''Q: What function should you use to join array elements with a glue string?'''
'''Q: What function should you use to join array elements with a glue string?'''
Line 269: Line 271:


''my answer:'' '''b. implode'''
''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'''


== Notes ==
== Notes ==
<references />
<references />

Revision as of 22:34, 16 March 2016

Operator precedence

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

Examples

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.
 */

Associativity

TK

Data types

Hexidecimal notation

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

Octal notation is indicated by a leading zero.

vardump($a = 010); // (1 * pow(8, 1)) = 8

Q: What is the output of the following code?

echo 0500; // (5 * pow(8, 2)) = 5 * 64  = 320

A: 320

Bitwise operators

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

$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

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

is_numeric()

  • Valid values: "200", ".25e4", "20,2"
  • Invalid values: "$200"

Magical constants

Magical constants are constants that change value depending on where in the code they are placed.

The magical constants in PHP: [2]

  • __LINE__
  • __FILE__
  • __DIR__
  • __FUNCTION__
  • __CLASS__
  • __TRAIT__
  • __METHOD__
  • __NAMESPACE__

Language constructs

Is print() a function?

print() is a language construct. [3]

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" */

Control structure alternative syntax

Q: Is the following code valid?

$a = 5;
if ($a==5):
    echo "Hello";
    echo "1";
else:
    echo "Not hello";
    echo "2";
endif;

A: Yes

Arrays

Joining array elements into string

Q: What function should you use to join array elements with a glue string?

  1. join_str
  2. implode
  3. connect
  4. make_array
  5. None of the above

my answer: b. implode

Array comparison operators

Q: When comparing two arrays, what is the difference between == and ===?

  1. == compares keys while === compares keys and values
  2. === also compares the order and types of the objects
  3. === compares the array references
  4. They are identical
  5. None of the above

A: (b) === also compares the order and types of the objects

Notes

  1. PHP's operator precedence, PHP documenations
  2. magical constants, PHP documentation
  3. print(), PHP documentation