Basic Syntax and Types Flashcards
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.
What are namespaces?
A method of grouping related php code elements within a library or application.
How do you declare a namespace?
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.
What does a backslash in a namespace declaration mean?
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 do you import a namespace?
With the “use” operator.
What’s the difference between namespaces and classes?
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.
What are extensions?
PHP add-ons added in the php.ini config file.
What is PECL?
A repository for PHP extensions.
What are core extensions?
Extensions included with the PHP core.
What does “userland” refer to?
Applications that run in the user space (not the kernel).
What are the userland rules?
- 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.
What are user .ini files?
.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.
Which directives control the use of user .ini files?
- 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 can you set the value of a configuration option?
Generally, you can use ini_set within the php script. Some settings require php.ini or http.conf.
What are the two major factors affecting performance?
- Reduced memory usage.
- Runtime delays.
How does garbage collection work in php?
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.
What is the opcode cache?
It stores precompiled php code, which often improves performance. It’s bundled with php 5.5.0 and later
What are the php super global variables?
- $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.
Know the PHP predefined error level constants.
http://php.net/manual/en/errorfunc.constants.php
What are two important recent changes to PHP that affect older code?
- 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.
- 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.
Can environment variables be used in php.ini?
Yes:
memory_limit = ${PHP_MEMORY_LIMIT}
In php.ini, should you use single quotes or double quotes?
Double quotes.
In php.ini what lines are ignored?
Any text on a line after an unquoted semicolon, and text within brackets.
How do you set boolean values in php.ini?
TRUE: true, on, yes
FALSE: false, off, no, none
What are the PHP_INI_* modes, and what is the meaning of each one?
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.
When running PHP as an Apache module, how can you change configuration settings?
In httpd.conf or .htaccess files. You will need “AllowOverride All” or “AllowOverride Options” privileges to do so.
How do you change PHP config values in your script?
ini_set()
What are the Apache directives that allow you to change PHP configuration from within the Apache configuration files?
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
In httpd.conf, can you use PHP constants?
No.
In php.ini, can you use PHP constants?
Yes.
How do you change php configure options after installation?
Re-run the configure, make, and make install steps.
How do you change php configure options after installation?
Re-run the configure, make, and make install steps.
When is php.ini read?
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.
When is php.ini read?
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.
For pure PHP code files, why should the closing PHP tag be omitted?
It prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects.
What is the most efficient way to output large blocks of text?
Dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print().
What PHP tags should you use to embed PHP in XML or XHTML?
Do you need to have a semicolon terminating the last line of a PHP block?
No.
What types of comments does PHP allow?
// comment /* multi-line comment */ # comment
What types of comments does PHP allow?
// comment /* multi-line C-style comment */ # comment
Can C-style PHP comments be nested?
No.
Can C-style PHP comments be nested?
No.