PHP Unit 6 Quiz
Modules
-are blocks of code that represent separate concerns/tasks. -are typically incorporated into the program through interfaces
The .inc and .lib extensions
A browser may directly display the source code. They are conventions for external library files. To secure the files, do not place them in any Web document folder. -In XAMPP, do not put them in the htdocs folder. -In IIS, do not put them in the wwwroot/www folder. -In Apache, do not put them in httpd/www folder What two file extensions is this talking about?
Return
A function returns NULL if there is no ____________ statement or there is nothing that follows the ____________ keyword.
Return
A function returns a value other than the NULL value if there is a __________ statement AND there is an expression or a literal following the __________ keyword.
Reuse code Improve logic structures Improve readability Improve maintainability Improve code reliability and consistency Allow abstraction Reduce cost
Advantages of Modularization and Code Reuse.
Design a program that calculates and displays monthly payment of a loan.
An example of Modularization and Code Reuse.
Functions File inclusion Classes
Approaches of Modularization and Code Reuse
.php, .lib, and .inc
Commonly used file extensions for choosing filenames and locations include?
-Keyword function. -Function name: any valid identifier. - Parameters enclosed in parentheses. ------They are variables. ------Multiple parameters are separated with commas. ------Number of parameters: 0, 1, or more. -The set of curly braces forms the function body. ------Required regardless of the number of statements.
Defining the syntax of a function?
-File is secured. A browser won't display the source code. -It does not differentiate library files from other PHP files.
For choosing filenames and locations, the .php extension:
Parameters
How do you pass data into a function?
-Use .php for your code library. -Deny access to the file/directory in .htaccess file (Deny from all) -Add a code snippet at the beginning of the code library file to prevent the file from being called directly in a browser.
If your site is hosted on an ISP(Internet Service Provider) Web server, you may not allow to store files outside the Web document folder. What are some solutions?
Null a value other than NULL. *** In other programming languages, function may or may not return a value. ***
In PHP, all functions return a value to the caller. The return value can be?
Require (is more commonly used.)
Require and include function exactly the same; they behave differently when the file to be included is not found: ___________ displays a fatal error and immediately stops processing the script.
Include
Require and include function exactly the same; they behave differently when the file to be included is not found: ____________ displays a warning and then continues processing the script.
Invoked
Statements in a function do not execute until the function is called _____________?
Defining a function Calling/invoking a function
Steps involved in working with functions:
Organizing Code into Files
Storing code in external files allows code reuse across multiple PHP files. ---Functions ---Variables ---Constants ---HTML code Building code library for your Web site. Grouping common functionality. Creating consistent interface.
Functions
These are examples of what kind of code: Calculating the area of a triangle. Validating a user's credential. Retrieving a record from a database.
Return values from functions
These code examples are from? function isEven($number) { if (($number %2 ) == 0) return TRUE; else return FALSE; }
Return values from functions
These code examples are from? function return_null() { $b = 2 + 3; } function return_null_too() { $b = 2 + 3; return; }
Variable Scopes - Local
This code is an example of: $max = 0; function getMaximum($n1, $n2, $n3) { $max = $n1; if ($n2 > $max) $max = $n2; if ($n3 > $max) $max = $n3; echo "Inside function \$max=$max.<br>". } getMaximum(6, 10, 5); echo "Outside function \$max=$max."; Output: Inside function $max=10. Outside function $max=0.
Variable Scopes - Global
This code is an example of: $max = 0; function getMaximum($n1, $n2, $n3) { global $max; $max = $n1; if ($n2 > $max) $max = $n2; if ($n3 > $max) $max = $n3; echo "Inside function \$max=$max.<br>". } getMaximum(6, 10, 5); echo "Outside function \$max=$max."; Output: Inside function $max=10. Outside function $max=10.
A function
This code is an example of: //define a function that prints the current date function printCurrentDate() { echo "<p>Today is "; echo date('m‐d‐Y'), ".</p>"; } //define a function that adds two numbers function addTwo($op1, $op2) { return ($op1 + $op2); }
Variable Scopes - Local
This code is an example of: function getMaximum($n1, $n2, $n3) { $max = $n1; //some statements omitted return $max; }
passing arguments to function
This code is an example of? <?php function does_nothing() { echo "This function accepts no parameter and returns NULL."; } does_nothing(); ?> Output: This function accepts no parameter and returns NULL.
passing arguments to function
This code is an example of? <?php function does_something($a) { echo "The parameter is ", $a, "."; } does_something(); ?> Output: Warning: Missing argument 1 for does_something() .....
passing arguments to function
This code is an example of? <?php function does_something($a) { echo "The parameter is ", $a, "."; } does_something(5); ?> Output: The parameter is 5.
passing arguments to function
This code is an example of? <?php function does_something($a) { echo "The parameter is ", $a, "."; } does_something(5, 6); ?> Output: The parameter is 5. Note: extra parameters can be retrieved.
Type the name of the function and provide arguments in parentheses. To call the printCurrentDate function: --printCurrentDate(); To call the addTwo function: --addTwo(4, 7);
To call a function, you do what? What are some examples?
False Reminder: library files do not have to reside in Web document folders.
True or False Reminder: library files do have to reside in Web document folders.
False A function should be defined before it can be called.
True or False? A function should be defined anytime it can be called.
True
True or False? In theory, you are free to choose any filenames for your code library.
include (library_file_path_and_name); require (library_file_path_and_name);
Two ways to include library files within your script and name their general syntax:
Variable Scopes - Local
Variables declared within a function. Local to the function. Invisible outside the function.
<?php function function_name (parameters) { statements to be executed; } ?>
What is the general syntax code of a function?
5
What is the return value from the following function call? $return = addTwo(2, 3);
Null
What is the return value from the following function call? $return = printCurrentDate();
Arguments
When a function is called, the __________________ are passed into the function. _________________ must match the function parameters: count, data type, and order.
Variable Scopes - Global
__________ variables declared outside any function. -By default, __________ variables can NOT be accessed from within any function. -To access __________ variables from within a function, declare the variable within the function using keyword global. ----global $max;
*_once: require_once include_once
_____________ versions guarantee that the file in question is included only once in each script file.
Parameters
______________ are variables enclosed in parentheses in the function definition. A function may or may not require ________________.
Modularizing or modular programming
is a software design technique that breaks up a complex program into smaller modules.
A function
program construct that contains multiple PHP statements and performs a single, well-defined task.