PHP Functions






User-defined
Library : String handling , math
 
Types of Function
1. Without argument non returning
2. With argument non returning
3. With argument with return (to return multiple value)
4.Default function(function vat($price, $tax=.0675))
{
$total = $price + ($price * $tax);
echo "Total cost: $total";
}

5. Reference Function  Ex function &call(){    }

<?php
class foo {
   public $value = 42;

   public function &getValue() {
       return $this->value;
   }
}

$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;                // prints the new value of $obj->value, i.e. 2.
?>

6. Closure(PHP 5.3 is known as a closure, or anonymous function:allows you to create functions with no specified names. We can assign an anonymous
function to a variable
) $person_info = function($name, $age, $eyecolor)
{
echo "greetings: " . $name . "<br/>";
echo "You are : " . $age . " years old<br/>";
echo "and your eye color is: " . $eyecolor . "<br/><br/>";
};
$person_info('Peter', '43', 'brown');
$person_info('Dawn', '15', 'green');

PHP Library
include(),include_once(),require(),require_once()

  • header(),  exit() or die()   
  • call by value ,  call by reference,
  • scope of variable: global (two ways to access ), static, local

To avoid confusion prefer
Second way to access global variable:
<?php
$a=1;
$b=2;
$c=3;

function call(){
  echo  $GLOBALS['a'];
     echo $GLOBALS['b'];
        echo $GLOBALS['c'];


}
call();
?>

use of func_num_args()// returns number of args passed in function
use of func_get_arg(index)// return argument value at particular index

<?php
function call(){
   if(func_num_args()==0){
       echo "No argument Specified";
   }elseif (func_num_args()==1) {
       $arg=  func_get_arg(0);
       echo "Hello ".$arg;
   }elseif (func_num_args()==2) {
       $arg1=  func_get_arg(0);
       $arg2= func_get_arg(1);
       echo "Hello ".$arg1.$arg2;
   }
   }
call();//No argument Specifeied
call("aashish");//Hello aashish
call("aashish","mittal");// Hello aashish mittal
?>

Validation in php
  • ctype_alnum — Check for alphanumeric character(s)
  • ctype_alpha — Check for alphabetic character(s)
  • ctype_cntrl — Check for control character(s)
  • ctype_digit — Check for numeric character(s)
  • ctype_graph — Check for any printable character(s) except space
  • ctype_lower — Check for lowercase character(s)
  • ctype_print — Check for printable character(s)
  • ctype_punct — Check for any printable character which is not whitespace or an alphanumeric character
  • ctype_space — Check for whitespace character(s)
  • ctype_upper — Check for uppercase character(s)
  • ctype_xdigit — Check for character(s) representing a hexadecimal digit

is_numeric(),empty(),is_int(),is_string()  isset(),unset()
is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(), is_resource(), is_scalar(),
and is_string().

Type hinting


What’s the difference between include and require?







Functions

What happens when you pass a large array to a function?
<?
$array = array_fill(0, 100000, 'foobar');
echo 'Memory usage : ' . memory_get_usage() . ' bytes '.'<br>';//1Memory usage : 9294736 bytes

foo($array);//Function calling

echo 'Memory usage : ' . memory_get_usage() . ' bytes '. '<br>';//6Memory usage : 9294776 bytes

function foo($bar) {
   echo 'Memory usage : ' . memory_get_usage() . ' bytes '. '<br>';//2Memory usage : 9294736 bytes
   echo $bar[0] . '<br>';//3foobar
   echo 'Memory usage : ' . memory_get_usage() . ' bytes '. '<br>';//4Memory usage : 9294736 bytes
   $bar[0] = 'barfoo';
   echo 'Memory usage : ' . memory_get_usage() . ' bytes '. '<br>';//5Memory usage : 18343584 bytes
}

//Memory usage : 9294736 bytes
//Memory usage : 9294776 bytes
//foobar
//Memory usage : 9294776 bytes
//Memory usage : 18343584 bytes
//Memory usage : 9294776 bytes
?>
Which shows that the function parameter isn’t copied until it’s value changes. This is the deferred-copy mechanism at work.
now

Variables

The same effect occurs with simply copying variables :
<?
$array1 = array_fill(0, 10000, 'foobar');
echo 'Memory usage : ' . memory_get_usage() . ' bytes '. '<br>';//Memory usage : 1173744 bytes
$array2 = $array1;
echo 'Memory usage : ' . memory_get_usage() . ' bytes '. '<br>';//Memory usage : 1173744 bytes
$array1[0] = 'barfoo';
echo 'Memory usage : ' . memory_get_usage() . ' bytes '. '<br>';//Memory usage : 2105144 bytes

This code shows that the array is copied for real when one of the 2 copies is changed. The code outputs :
//Memory usage : 1173744 bytes
//Memory usage : 1173832 bytes
//Memory usage : 2105144 bytes
?>

Conclusion

Thanks to deferred-copy, you don’t have to be afraid for performance issues when passing large variables by value in PHP. Only when you change the variable, the variable is copied for real.
CONSTANT Variable
<?php
//$name="aashish";
define("NAME","aashish");
function call(){
//echo $name;//undefined variable name if simple variable
echo NAME;
}
call();


?>


Reference Variable


$a=10;
$b=&$a;
$b=20;
echo $a;//20
$a=10;
$b=&$a;
$b=20;
echo $a;//20


According to the PHP5, by-reference activity is often slower than using the same by-value activity. The cause would be a mechanism to optimize by-value assignments called “deferred-copy”.

Note:In most programming languages arrays are passed by reference. In PHP on the other hand, arrays are passed by value. Since arrays are often used for storing lots of values, we’ll be using them in some tests.

Reactions:

0 comments:

Post a Comment