0% found this document useful (0 votes)
32 views14 pages

Web Programming - Lec 5

The document discusses PHP user-defined functions. It covers: - Creating functions with the function keyword and passing arguments - Functions will not execute on page load but must be called - PHP is loosely typed so types are inferred, but type declarations can be used - Functions can return values using the return keyword - Files can be included in PHP using include() and require() to reuse code

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

Web Programming - Lec 5

The document discusses PHP user-defined functions. It covers: - Creating functions with the function keyword and passing arguments - Functions will not execute on page load but must be called - PHP is loosely typed so types are inferred, but type declarations can be used - Functions can return values using the return keyword - Files can be included in PHP using include() and require() to reuse code

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CSS 5

HTML

MYSQL

PHP

Web Programming Course


Lecture 8
PHP User Defined Functions
Besides the built-in PHP functions, we can create our own functions.
• A function is a block of statements that can be used repeatedly in a program.
• A function will not execute immediately when a page loads.
• A function will be executed by a call to the function.
• A user-defined function declaration starts with the word function

function functionName ( )
{
code to be executed;
}

Note:

• A function name can start with a letter or underscore (not a number).

• Function names are NOT case-sensitive.


PHP User Defined Functions
Besides the built-in PHP functions, we can create our own functions.
• A function is a block of statements that can be used repeatedly in a program.
• A function will not execute immediately when a page loads.
• A function will be executed by a call to the function.
• A user-defined function declaration starts with the word function
PHP User Defined Functions
PHP Function Arguments

• 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.

• The include() Function


• The require() Function

• 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.

When using include () When using Require()


PHP - Strings
Strings are sequences of characters, like "PHP supports string operations".

Using the strlen() function


The strlen() function is used to find the length of a string.
PHP - Strings
Strings are sequences of characters, like "PHP supports string operations".

Using the strpos() function

The strpos() function is used to search for a string or character within a string.

• If a match is found in the string, this function will


return the position of the first match.

• If no match is found, it will return Null.


Thank you 

You might also like