Anda di halaman 1dari 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="jouni.juntunen@students.oamk.fi";
$from=”jouni.juntunen@oamk.fi”;
$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

Anda mungkin juga menyukai