0% found this document useful (0 votes)
55 views

PHP Language (Functions)

This document discusses functions in PHP programming. It covers two types of functions - built-in functions and custom functions. Functions provide a well-defined structure, add reusability, and define the syntax for name, parameters, and return value. Examples are given for common functions like mail, printf, and header to set HTTP headers. Input validation functions like floatval, intval, and strval are also mentioned. The document discusses manipulating strings with functions like substr, strlen, and str_replace. It provides an example of using Luhn's algorithm with functions to validate a credit card number.

Uploaded by

tzaraujo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

PHP Language (Functions)

This document discusses functions in PHP programming. It covers two types of functions - built-in functions and custom functions. Functions provide a well-defined structure, add reusability, and define the syntax for name, parameters, and return value. Examples are given for common functions like mail, printf, and header to set HTTP headers. Input validation functions like floatval, intval, and strval are also mentioned. The document discusses manipulating strings with functions like substr, strlen, and str_replace. It provides an example of using Luhn's algorithm with functions to validate a credit card number.

Uploaded by

tzaraujo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PHP-language, functions

Jouni Juntunen
Oulu University of Applied Sciences
School of Business and Information Management
Functions
• Two types of functions
– Built-in (in PHP more that 700 prewritten
functions available)
– Custom

• Gives well-defined structure to software


• Adds reusability
Function syntax

Return value Name Parameters


Example: mail-function
• Send email from you application without any spesific
knowledge about email-protocols or low-level details

<?
$to="[email protected]";
$from=”[email protected]”;
$subject=”The subject”;
$message=”Test”;
$headers='From:' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
if (mail($to,$subject,$message,$headers))
.
.
.
?>
Example: printf-function
• Print formatted string
• Examples:

• More examples on
http://www.w3schools.com/PHP/func_string_
printf.asp
Set HTTP-headers
• Header-function can be used to set HTTP
headers for server’s reply in PHP
• Example: Relocate user (Example and source)
• Example: Promt for username and password
(Example and source on course material and
Ilmari)
Input validation with functions
• Check if input given by user is numerical
• Validation user following PHP-functions
– floatval(), intval(), strval()
• Example
• Source code
Manipulating strings
• For example: substr, strlen and str_replace
• More string functions on
http://us2.php.net/manual/en/ref.strings.php
Example: Is credit card valid? (Luhn’s formula)

4920 1900 7526 7276


1. 4 * 2 = 8
2. 9 * 1 = 9
3. 2 * 2 = 4
4. 0 * 1 = 0
5. 1 * 2 = 2
6. 9 * 1 = 9
7. 0 * 2 = 0
8. 0 * 1 = 0
9. 7 * 2 (= 14) = 1+ 4 = 5
10. 5 * 1 = 5
11. 2 * 2 = 4
12. 6 * 1 = 6
13. 7 * 2 (=14) = 1 + 4 = 5
14. 2 * 1 = 2
15. 7 * 2 (=14) = 1 + 4 = 5
6 last number
= 64
(64 + 6) %10 ==0

You might also like