PHP Functions and Scope Flashcards
(25 cards)
What is a PHP function?
A function is a reusable block of code that performs a specific task, defined with the function keyword.
Functions, like function myFunction() { echo “Hello”; }, promote code reuse. In WordPress, functions are used in functions.php for custom logic. Freelancers create functions for client features, while enterprise architects use them for modular, maintainable code in large systems.
How do you define a function in PHP?
Use function functionName() { // code }, like function greet() { echo “Hi”; }.
The function keyword, followed by a name and parentheses, defines a function. In WordPress, freelancers define functions for tasks like enqueuing scripts, while enterprise architects ensure clear naming conventions for scalability.
What is a function call in PHP?
A function call executes a function, like greet(); for function greet() { echo “Hi”; }.
Calling a function runs its code. In WordPress, functions like the_title() are called in templates. Freelancers use calls for dynamic output, while enterprise architects optimize call frequency for performance.
What is a function parameter in PHP?
A parameter is a variable in a function definition, like function greet($name) { echo “Hi, $name”; }.
Parameters accept input values. In WordPress, parameters are used in functions like get_post($id). Freelancers use them for flexible client features, while enterprise architects ensure type safety in parameters.
What is a function argument in PHP?
An argument is a value passed to a function, like greet(“John”); for function greet($name) {}.
Arguments provide data to parameters. In WordPress, arguments are used in WP_Query($args). Freelancers pass arguments for customization, while enterprise architects validate arguments for security.
What is a default parameter in PHP?
A default parameter has a preset value, like function greet($name = “Guest”) { echo “Hi, $name”; }.
If no argument is passed, the default is used. In WordPress, defaults are common in plugin functions. Freelancers use them for optional settings, while enterprise architects ensure defaults don’t cause unexpected behavior.
What is the return statement in PHP?
return sends a value back from a function, like function add($a, $b) { return $a + $b; }.
return stops execution and provides output. In WordPress, functions like get_the_title() return data. Freelancers use returns for reusable logic, while enterprise architects prefer returns over echo for flexibility.
What is variable scope in PHP?
Variable scope defines where a variable is accessible, such as local or global.
Scope affects variable availability in functions. In WordPress, scope is critical in plugins to avoid conflicts. Freelancers manage scope for clean code, while enterprise architects enforce strict scoping for large systems.
What is a local variable in PHP?
A local variable is defined inside a function and only accessible there, like $x = 5; inside function myFunc() {}.
Local variables prevent unintended changes. In WordPress, local variables are used in functions to isolate data. Freelancers use them for temporary calculations, while enterprise architects avoid global pollution.
What is a global variable in PHP?
A global variable is defined outside a function and accessible with the global keyword, like global $x;.
In WordPress, $wpdb is a global variable. Freelancers use globals sparingly to avoid conflicts, while enterprise architects prefer dependency injection for better control.
How do you access a global variable in a function?
Use the global keyword, like function myFunc() { global $x; echo $x; }.
This imports a global variable into a function’s scope. In WordPress, it’s used for globals like $post. Freelancers use it cautiously, while enterprise architects minimize global usage for modularity.
What is the $GLOBALS array in PHP?
$GLOBALS is a superglobal array that accesses global variables, like $GLOBALS[‘x’].
It’s an alternative to the global keyword. In WordPress, it’s rarely used but can access globals like $wpdb. Freelancers avoid it for clarity, while enterprise architects prefer explicit scoping.
What is a static variable in PHP?
A static variable retains its value between function calls, defined with static, like static $count = 0;.
Static variables persist in memory. In WordPress, they track state in recursive functions. Freelancers use them for counters, while enterprise architects ensure they don’t cause memory issues.
What is an anonymous function in PHP?
An anonymous function is a function without a name, like $greet = function($name) { echo “Hi, $name”; };.
Used as callbacks in WordPress (e.g., array_map()). Freelancers use them for dynamic logic, while enterprise architects leverage them for functional programming patterns.
What is a closure in PHP?
A closure is an anonymous function that captures variables from its surrounding scope.
For example, $x = 5; $func = function() use ($x) { echo $x; };. In WordPress, closures are used in filters. Freelancers use them for flexible callbacks, while enterprise architects ensure proper variable binding.
What is the strlen() function in PHP?
strlen() returns the length of a string, like strlen(“Hello”) returns 5.
Useful in WordPress for validating input lengths (e.g., post titles). Freelancers use it for client forms, while enterprise architects ensure multibyte safety with mb_strlen().
What is the strtolower() function in PHP?
strtolower() converts a string to lowercase, like strtolower(“HELLO”) returns “hello”.
Used in WordPress for normalizing data (e.g., slugs). As you’ve asked about this before, it’s key for freelancers in search functionality and enterprise architects for consistent data handling.
What is the array_merge() function in PHP?
array_merge() combines multiple arrays into one, like array_merge($arr1, $arr2).
In WordPress, it merges settings arrays. Freelancers use it for plugin options, while enterprise architects ensure no key conflicts in large datasets.
What is the count() function in PHP?
count() returns the number of elements in an array, like count($array).
Used in WordPress to count posts or items. Freelancers apply it in loops, while enterprise architects optimize it for performance with large arrays.
What is the isset() function in PHP?
isset() checks if a variable is set and not null, like isset($x).
In WordPress, it validates variables (e.g., post meta). Freelancers use it for safe checks, while enterprise architects prevent undefined variable errors in robust systems.
What is the empty() function in PHP?
empty() checks if a variable is empty, like empty($x) for 0, “”, or null.
Used in WordPress to check form inputs. Freelancers validate client data, while enterprise architects ensure accurate emptiness checks in data pipelines.
What is the func_get_args() function?
func_get_args() returns an array of arguments passed to a function.
For example, function myFunc() { return func_get_args(); } captures all arguments. Freelancers use it for flexible functions, while enterprise architects avoid overuse for clarity.
What is a variable function in PHP?
A variable function calls a function named by a string, like $func = “greet”; $func();.
In WordPress, it’s used in dynamic callbacks. Freelancers use it for plugin flexibility, while enterprise architects ensure secure function validation.
What is the call_user_func() function?
call_user_func() calls a function by name or callback, like call_user_func(“greet”, “John”).
Used in WordPress for dynamic function calls (e.g., hooks). Freelancers apply it in plugins, while enterprise architects use it for extensible systems.