Web Programming - Lec 5
Web Programming - Lec 5
HTML
MYSQL
PHP
function functionName ( )
{
code to be executed;
}
Note:
• Information can be passed to functions through arguments. An argument is just like a variable.
• Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
PHP User Defined Functions
PHP is a Loosely Typed Language
• PHP automatically associates a data type to the variable, depending on its value. Since the data types are
not set in a strict sense, you can do things like adding a string to an integer without causing an error.
• In PHP 7, type declarations were added. This gives us an option to specify the expected data type when
declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type
mismatches.
• In the following example we try to send both a number and a string to the function without using strict:
• To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file.
PHP User Defined Functions
PHP Default Argument Value
PHP User Defined Functions
PHP Functions - Returning values
PHP User Defined Functions
PHP Return Type Declarations
• PHP 7 also supports Type Declarations for the return statement. Like with the type declaration for
function arguments, by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.
• To declare a type for the function return, add a colon ( : ) and the type right before the opening curly
( { )bracket when declaring the function.
PHP - File Inclusion
• You can include the content of a PHP file into another PHP file before the server executes it. There are
two PHP functions which can be used to included one PHP file into another PHP file.
• This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can
be reused on multiple pages. This will help developers to make it easy to change the layout of
complete website with minimal effort.
• If there is any change required then instead of changing thousand of files just change included file.
PHP - File Inclusion
The include() Function
• The include() function takes all the text in a specified file and copies it into the file that uses the include
function. If there is any problem in loading a file then the include() function generates a warning but the script
will continue execution.
Menu.php
PHP - File Inclusion
The require() Function
• The require() function takes all the text in a specified file and copies it into the file that uses the include function. If
there is any problem in loading a file then the require() function generates a fatal error and halt the execution of
the script.
The strpos() function is used to search for a string or character within a string.