Basic Syntax and Types Flashcards
(164 cards)
How is the modulus operator used?
$m = 5 % 2; //$m == 1 (remainder when dividing)
What are bitwise operators?
They allow evaluation and manipulation of specific bits within an integer.
What is the XOR bitwise operator?
“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.
Name the bitwise operators and their symbols.
and, or, xor, not, shift left, shift right
&, |, ^, ~, <>
What is “bit” short for?
binary digit
How does the bitwise ~ operator work?
It takes 1 integer after it, and it reverses each binary digit in an integer, from 0 to 1 or 1 to 0.
How do the bitshift operators work?
They shift the 0s and 1s in a binary representation of a number to the left or right.
In terms of multiplying or dividing, what do the «_space;and»_space; bitwise operators mean?
Each step of «_space;means “multiply by two”. Each step of»_space; means “divide by two”.
During bit shifting, what gets shifted in on the opposite side?
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.
What happens if bitwise operands are not integers?
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 «_space;and»_space; operators are always treated as integers.
How does placement affect the increase/decrease operators?
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 do these operators differ?
!=
!==
With the additional equal sign (!== and ===), the data type of the two operands must also be identical. So, essentially, == means “equal” and === means “identical”.
What is the xor logical operator?
It evaluates to true if either operand BUT NOT both operands are true.
What are the execution operators?
- shell_exec()
- enclosing in backticks
How do you assign variables by reference?
Attach an & to the beginning of the variable being assigned.
What’s the difference between assigning by value and assigning by reference?
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 does variable initialization work?
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().
What is the ternary operator?
(expression) ? valueiftrue : valueiffalse
SHORT FORM:
(valueiftrue) ?: valueiffalse
What do continue and break do?
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.
What does return() do?
Called within a function, it halts execution of the function. Called within global scope, it halts execution of a script.
What does empty() consider to be an empty variable?
Empty string, empty array, 0, 0.0, “0”, NULL, FALSE, or a variable without an assigned value.
What does list() do?
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 are constants named?
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.
What are magic constants?
Predefined constants. Some can change value depending on where they are used.