Functions allow programmers to organize and reuse code. PHP has many built-in functions for common string and date/time operations. Programmers can also define their own functions. Functions can accept arguments, use default values, be called by value or reference, and be recursive. Variable scope inside functions prevents naming collisions with external variables. Well-formed PHP functions follow naming conventions and can contain other functions and classes.
2. PAPER NAME : PROGRAMMING IN PHP
STAFF NAME : MS. V. SHANTHI M.sc, M.phil,M.tech,PGDAS.
CLASS : III BCA
SEMESTER : VI
3. Why Functions?
PHP has stacks of built-in functions that we use all
the time.
We write our own functions when our code spreads
a certain level of difficulty.
echo strrev(" .dlrow olleH"); echo str_repeat("Hip ", 2);
echo strtoupper("hooray!");
echo strlen("intro");
echo "n";
Hello world.
Hip Hip
HOORAY!
5
OUTPUT
4. Built-InFunctions...
Much of the influence of PHP comes from its built-in
functions.
Many are modeled after C string collection functions
(i.e. strlen()).
echo strrev(" .dlrow olleH"); echo str_repeat("Hip ", 2); echo
strtoupper("hooray!");
echo strlen("intro");
echo "n";
Hello world.
Hip Hip
HOORAY!
5
OUTPUT
5. Functions can choose to receive optional influences.
Within the function definition the variable names are
successfully “aliases” to the values passed in when the
function is called.
Arguments
function howdy($lang) {
if ( $lang == 'es' ) return "Hola";
if ( $lang == 'fr' ) return "Bonjour";
return "Hello";
}
print howdy('es') . " Glennn";
print howdy('fr') . " Sallyn";
Hola Glenn
Bonjour Sally
OUTPUT
6. Default arguments
No args passed would mean using the defaulting
values
Sometimes useful
Again, nothing about types...
<?php
function myfunction1($arg1="D"){
echo $arg1 . "<br>";
}
myfunction1("bla bla bla");
$strA="A";
myfunction1($strA);
$intA=12;
myfunction1($intA);
myfunction1();
?>
7. Call By Value
The quarrel variable within the function is an
“alias” to the real variable.
But even advance, the alias is to a *copy* of the
real variable in the function call.
function double($alias) {
$alias = $alias * 2;
return $alias;
}
$val = 10;
$dval = double($val);
echo "Value = $val Doubled = $dvaln";
Value = 10 Doubled = 20
OUTPUT
8. Call By Reference
Sometimes we want a function to change one of
its influences, so we indicate that an quarrel is
“by reference” using ( & ).
function triple(&$realthing) {
$realthing = $realthing * 3;
}
$val = 10;
triple($val);
echo "Triple = $valn";
Triple = 30
OUTPUT
9. Recursive functions
Be enormously careful as the program might
not stop calling itself!!
Avoid recursive function/method calls with over 100-
200 recursion levels as it can smash the stack and
cause a dissolution of the present script.
10. <?php
function recur($intN){
if ($intN ==1)
return "this is a power of 2<br>";
elseif ($intN%2 == 1)
return "not a power of 2<br>";
else {
$intN /=2;
return recur($intN);
}
}
echo "256: " . recur(256);
echo "1024: " . recur(1024);
echo "1025: " . recur(1025);
?>
11. Variable Scope
In general, variable names used privileged of function
code do not mix with the variables external of the
function to evade unexpected side effects if two
programmers use the similar variable name in
dissimilar parts of the code.
We call this “name spacing” the variables. The
function variables are in one “name space” whilst the
main variables are in additional “name space”.
http://php.net/manual/en/language.variable
s.scope.php
12. Any legal PHP code may seem inside a function,
even other functions and class explanations.
A legal function name twitches with a letter or
underscore, followed by any number of letters,
numbers, or unrscores.
PHP does not sustenance function overloading,
nor is it possible to undefine or redefine formerly-
professed functions.
Both variable number of arguments and
avoidance arguments are maintained in functions.
User-definedFunctions
13. All functions besides classes in PHP have the global
scope.
Functions need not be defined before they are
referenced, excepting when a function is conditionally
demarcated as shown.
User-definedFunctions