PHP Flashcards
What are the three main areas that PHP is used?
Server-side scripting, Command line scripting, and writing desktop applications
How to comment in PHP docs?
// one line c++ style comment # one-line shell-style comment /* multi-line comments */
Explain how PHP tags work
ends php script,
if there is no HTML following last line of PHP code, PHP closing tag can be omitted. (HTML can be written directly in a PHP doc)
How many primitive datatypes does PHP have
10
Name the four scalar types.
bool, int, float (double), string
Name the four compound types.
array, object, callable, iterable
Name the two special types.
resource, null
How are PHP variable types different from other languages?
variable types are not determined by the programmer but are decided at runtime by PHP
How are variables represented?
Variable start with a $, followed by any letter or underscore, followed by any mix of letters, numbers, and underscores
What are constants and how can they be defined?
Constants are variables with values that can not change. They can be declared with either the const keyword or by calling define(); Only the four scalar types can be assigned to variables with the const keyword
What are +$a and -$a?
+$a is the identity operator being applied to a variable called $a. This converts $a into an appropriate int/float. -$a is the negate operator being applied to a variable called $a. This represents the opposite of $a.
What is a ternary operator in PHP?
Same as in JavaScript. expression ? “this if true” : “else, this if not true”
When does a condition get evaluated in a while loop?
Before each iteration.
How is a do-while loop different from a while loop?
The condition gets checked at the end of each iteration. This ensures that the loop runs at least 1 time.
Explain for loop syntax in PHP
for ( expression 1; express 2; express 3) {code block}
for($i=0; $i < 10; $i++) {
echo $i;
}
What are foreach loops used for?
foreach statements are used for iterating over arrays or objects. foreach($array as $value) {} or foreach($array as $key => $value){};
What does adding a number after a break do? (ex: break 2;)
Breaks can except this optional numeric argument which tells it how many nested enclosing structures are to be broken out of. break 2 will exit 2 levels of loops.
Explain continue
continue is used to skip the rest of the current loop iteration and begin again at the next condition check to the next iteration. Like break, continue can accept an optional numeric argument.
What is require? How does it work?
require includes and evaluates the specified file it was called with. (Think importing in JavaScript). Require is different from includes in that require will produce a fatal E_compile_error which will halt the script.
What is include? How does it work? How is it different from require?
Include includes and evaluates the specified file it was called with, just like require. However, unlike require, include will emit an E_warning errorif it can not find the file. This will not halt the script. The program can continue running.