PHP Set 4 Flashcards
What is the rule for naming functions?
Must start with a letter or an underscore
Are function names case sensitive?
No they are not.
Ex: If you have a function called writeMsg() you could call it with WritemsG(); and it would still work
What are the 4 variable scopes that are available in PHP?
- Local variables: Only valid in the part of the code they are introduced
a. Variables inside a function are local inside the function’s curly braces - Global variables that are valid outside of functions
a. Variables outside any function are global - Superglobal variables are built-in variables that are always available in all scopes
- Static variables are local variables that retain their value between separate calls of the function
a. Static variables should always be initialized with a value
How can you include HTML markup in the body of a PHP function definition?
<?php
function html_output() {
?>
<h1>HELLO<h1>
<p>This is HTML<p>
<?php
}
html_output();
?>
How can you pass in a variable reference and what is the difference from passing in a variable value?
Using ‘&’ like &$var1
When passing in a variable by reference, changes to the argument also change the variable that was passed in. If you only pass by value the variable is not modified outside of the function scope
How can you pass in a variable number of arguments to a function and how is it handled by PHP?
Using ‘…’ such as function sum(…$numbers) denotes a function that accepts a variable number of arguments. The arguments are passed into the given variable as an array
What is an anonymous function?
Also known as closures, they are functions that have no specified function name but still have inputs and an output. You can assign an anonymous function to a variable
Can functions be passed to other functions as arguments?
Yes they can
Ex: function apply ($f ,$x ,$y) {
return $f($x ,$y );
}
where $f is a function argument
What is the difference between include and require when importing a file to a PHP script?
If a file is not found when it is required it will throw an E_COMPILE_ERROR and will halt the script
If a file is not found when it is included it will throw a warning but the rest of the script will run
What is the difference between include and include_once when importing a file to a PHP script?
include_once will not include the code from a file if that code has already been included. It is used where the same file might be included and evaluated more than once during execution of a script