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.
Explain require_once and include_once.
Both do the same as require and include, however, PHP will check to see if the file has already been included and will not include/require again if so
Explain anonymous function in PHP
Also known as closures, Anonymous functions are mostly used for callbacks (passing a function as an argument). Anonymous functions don’t have names (ex: function ($a, $b) {}). Anonymous functions are made possible by the closure class.
Explain arrow functions and when were they introduced?
Arrow functions are anonymous function syntax that was introduced in PHP 7.4.
Explain classes in PHP
Classes are object models with added features such as visibility, abstract, final classes and methods.
How does $this work in PHP
When a method of a class is called, $this refers to the calling object
What are __contruct() and __destruct()
__construct() is a method that is used to initialize a new instance of a class object. When the new operator is called with a class, the __contruct() method is called to initialize the new object. The __destruct() method gets called as soon as there are no other references to a particular object.
What is property visibility?
Classes allow you to set the visibility of a variable with either, public, private, or protected.
What are superglobals?
Super globals are built-in variables that can be accessed from any scope.
What is $GLOBALS?
$GLOBALS references all variable available in a global scope
What is $_SERVER?
$_SERVER is an array containing information about the server and execution environment. The array can include information such as headers, paths, and script locations. The entries in this array are created by the server and are not always guaranteed to provide any of the entries. See PHP documentation for a list of all possible entries.
What is $_GET?
$_GET provides an associative array of variables passed to the script from the URL parameters (query string). (HTTP get variables)
What is $_POST?
$_POST provides HTTP post variables in the form of an associative array. These are passed to the current script by using the HTTP post method on form controls.
What is $_FILES?
$_FILES gives file upload variables. Returns an associative array of items uploaded to the script via the HTTP post method
What is $_COOKIE?
An associative array of variables passed to the current script via HTTP Cookies.
What is $_SESSION?
An associative array containing session variables available to the current script.
What is $_REQUEST?
HTTP request variables. An associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE.
What is $_ENV?
An associative array of variables passed to the current script via the environment method.
list all PHP data types.
String, Boolean, integer, floating-point number, Object, Array, Null
Explain array()
Creates an array. Accepts are values as arguments and returns an array of the arguments.
Explain array_keys()
Returns an array of all keys in an array if no other parameters are specified other than the search array.
Explain array_reverse()
Returns a new reversed array.
Explain array_search()
Searches an array for a search value. Returns the key of the search value if the value exists in the array. Otherwise returns false
Explain array_slice()
Returns a sequence of elements from an array
Explain array_push() & array_pop()
push: adds value to the beginning of the array (returns the new number of elements in the array).
pop: removes and returns the last value in the array.
Explain count()
returns the number of elements in an array
Explain indexed vs associative arrays
indexed arrays use numeric values for keys whereas associative array keys are named (strings).
Explain explode()
Breaks up a string into an array based on a substring. Similar to string.split(“”) in javascript.
Explain str_split()
Breaks a string into a character array. The length of each chunk can be specified with a second parameter.
.htaccess vs php.ini
.htaccess is a configuration file for an apache server.
php.ini is a file for making changes to PHP default settings.
How to do connect to MySQL database in a PHP script.
connect with MySQL database by using mysqli_connect.
What are some common errors types in PHP?
Notices, warnings, and fatal errors
explain strlen();
Returns the length of a string