|
function is a block of statements to use repeatedly
|
|
|
function functionname(arguments)
|
|
|
function functionname()
|
|
|
<?php
|
|
|
echo 'myname is';
|
|
|
function myfunction(){
|
|
|
echo 'rafigue';
|
|
|
}
|
|
|
myfunction();
|
without writing too many statemenst, by callingthat function we can implement this
|
|
myfunction();
|
|
|
myfunction();
|
|
|
?>
|
|
|
function with arguments
|
|
|
<?php
|
|
|
function add($num1, $num2){
|
|
|
echo $num1 + $num2;
|
|
|
}
|
|
|
add(1,2);
|
|
|
add(5,6);
|
|
|
?>
|
|
|
function with arguments
|
|
|
<?php
|
|
|
function add($num1, $num2){
|
|
|
echo $num1 + $num2;
|
|
|
}
|
|
|
$x= 100;
|
|
|
$y = 400;
|
|
|
add($x, $y);
|
|
|
?>
|
|
|
<?php
|
|
|
function printDate ($day, $month, $year){
|
|
|
echo $day.' '.$month.' '.$year;
|
|
|
}
|
|
|
$d = 5;
|
|
|
$m = 'jan';
|
|
|
$y = 2018;
|
|
|
printdate($d, $m, $y);
|
|
|
?>
|
|
|
//(10+10)/(5+1) =20/10
|
|
|
function add($num1, $num2){
|
|
|
$result= $num1 + $num2;
|
|
|
return $result;
|
|
|
}
|
|
|
function divide ($num1, $num2){
|
|
|
$result = $num1/$num2;
|
|
|
return $result;
|
|
|
}
|
|
|
echo ' divide(add(1,2), add(2,3))'; or
|
|
|
$finalresult= echo '
divide(add(1,2), add(2,3))';
|
|
|
echo 'final result';
|
|
|
global, local and static variable
|
|
|
global
|
variable declared outside the function
|
|
$x = 280;
|
|
|
function myfuntion(){
|
|
|
echo $x;
|
we cant use that variable inside the function
|
|
}
|
|
|
myfunction();
|
|
|
$x = 280;
|
|
|
function myfuntion(){
|
|
|
global $x;
|
we can use global variable
|
|
echo $x;
|
|
|
}
|
|
|
myfunction();
|
|
|
local variable
|
a variable we can use inside the function and it will declared
inside
|
|
function fun1(){
|
|
|
$x = 100;
|
|
|
}
|
|
|
fun1();
|
undefined , because we cant print the local variable which is
used inside the function
|
|
echo $x;
|
|
|
using return we can get the only value
|
|
|
function fun1(){
|
|
|
$x = 100;
|
|
|
return $x;
|
|
|
}
|
|
|
echo fun1();
|
print tha t value only
|
|
or
|
|
|
$y = 250;
|
another programm
|
|
$x = $y+ fun1();
|
|
|
echo $z;
|
|
|
function mytext(){
|
|
|
$x = 0;
|
assigned variable and after printed , value incrimetned
|
|
echo $x .'<br/>'
|
it prints 0, incriments
|
|
$x++;
|
but incrimetned value will not print becaues it after execution
of function the value will deleted
|
|
}
|
|
|
mytext();
|
0
|
|
mytext();
|
0
|
|
mytext();
|
0
|
|
to show incrimented value we have to use
|
|
|
static
|
|
|
function mytext(){
|
|
|
static $x = 0;
|
|
|
echo $x .'<br/>'
|
|
|
$x++;
|
|
|
}
|
|
|
mytext();
|
0
|
|
mytext();
|
1
|
|
mytext();
|
2
|
| function |
| global variable |
| global |
| local] |
| return |
| static variables |
| what is meant by function |
| what is meant by function with arguments |
| what is meant by function without arguments |
| how function is called |
| function for adding two numbers |
| function for adding divide two numbers |
| how to add numbers |
| how to print date |
| how to add numbers |
| how to divide the two numbers |
| we cant use that variable which is declared the outside |
| to use the variable which is declared outside of the function , use global |
| declared variable |
| we cant access the variable which is declared inside the function |
| if we want to access we have to use return method |
| print the value |
| print the value by ading another value |
| for every function executes , the value will be 0, to store that value |
| to show incrimented values use sttic |
No comments:
Post a Comment