Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP 5 Functions
 
The real power of PHP comes from its functions; it has more than 1000 built-in functions.
 
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.
 
 
Create a User Defined Function in PHP
 
A user defined function declaration starts with the word "function":
 
Syntax
  1. function functionName() {
  2.     code to be executed;
  3. }
 
Note: A function name can start with a letter or underscore (not a number).
 
Tip: Give the function a name that reflects what the function does!
 
Function names are NOT case-sensitive.
 
In the example below, we create a function named "writeMsg()". The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs "Hello world!". To call the function, just write its name:
 
Example
  1. <?php
  2. function writeMsg() {
  3.     echo "Hello world!";
  4. }
  5. writeMsg(); // call the function
  6. ?>
 
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.
 
The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:
 
Example
  1. <?php
  2. function familyName($fname) {
  3.     echo "$fname Refsnes.<br>";
  4. }
  5. familyName("Jani");
  6. familyName("Hege");
  7. familyName("Stale");
  8. familyName("Kai Jim");
  9. familyName("Borge");
  10. ?>
The following example has a function with two arguments ($fname and $year):
 
Example
  1. <?php
  2. function familyName($fname, $year) {
  3.     echo "$fname Refsnes. Born in $year <br>";
  4. }
  5. familyName("Hege", "1975");
  6. familyName("Stale", "1978");
  7. familyName("Kai Jim", "1983");
  8. ?>
 
PHP Default Argument Value
 
The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:
 
Example
  1. <?php
  2. function setHeight($minheight = 50) {
  3.     echo "The height is : $minheight <br>";
  4. }
  5. setHeight(350);
  6. setHeight(); // will use the default value of 50
  7. setHeight(135);
  8. setHeight(80);
  9. ?>
 
 
PHP Functions - Returning values
 
To let a function return a value, use the return statement: 
 
Example
  1. <?php
  2. function sum($x, $y) {
  3.     $z = $x + $y;
  4.     return $z;
  5. }
  6. echo "5 + 10 = " . sum(5, 10) . "<br>";
  7. echo "7 + 13 = " . sum(7, 13) . "<br>";
  8. echo "2 + 4 = " . sum(2, 4);
  9. ?>
 


2016-12-10
531 views




 
     
 
Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy

Catalog.299.ru

The site is a media provider
Яндекс.Метрика
 
English USA
Australia Русский