Saturday, 7 February 2015

Function Return Values

In previous examples we just print the value from functions ,but if we need to do other tasks like need some results of calculations or some statements ,for this type of scenario in php we use return values in function Example:

<?php
// define a function for calculation
function calculate($val1,$val2,$opr) { 
   
if($opr=='add'){
return $val1+$val2;  // return value 
}
if($opr=='sub'){
return $val1-$val2;  // return value 
}
if($opr=='mul'){
return $val1*$val2;  // return value 
}
    

//call a function where first two parameters values and other mention operation you need to perform

$result = calculate(2,5,'mul');  //multiply operation

echo $result; 

//print output 10
?>


Function Passing Values by Refrence

As we discussed in previous e.g what is function passing by value ,in passing by value we can change value of variable by passing values , but if we want to change our global variable value than we use function by reference,in function by reference we pass parameters to function with the reference of variable not the value of variable as mention in below .

Example:

<?php
//declare gloabal variable 
$myvar= "global variable"; 

// function to show the value
function myfunction(&$myparameter) { 
    $changeparameter = "global vaiable inside"; 
    echo "This is $changeparameter  the function<br />"; 

// call function 
myfunction($myvar); 

// print global variable value
echo "This is $myvar outside the function";
 

?>

In above example (&) with parameter use to set reference for variable,it will modify global variable value


Friday, 6 February 2015

Function Parameter

As we see in precious example simple function example when we call that function its always do the same thing , what if we want to get dynamic values from functions? we use parameters with function,parameter also known as arguments.

Example:



<?php

// define custom function
function myfunction($myparameter) {
  
    echo $myparameter;

  
}
//call a function

myfunction("parameter1");

//each time its change value of function by pass argument
 
echo "<br/>";
myfunction("parameter2");
echo "<br/>";
myfunction("parameter3");
echo "<br/>";
myfunction("parameter4");
echo "<br/>";
myfunction("parameter5");
echo "<br/>";
//print output

parameter1
parameter2
parameter3
parameter4
parameter5
 ?>





This is also known as passing argument to function by value

Functions

As we know that php has its own built in function like other programming languages ,similarly in php we can build own functions, syntax for user defined function as below:



<?php
// define custom function function myfunction() {
   
       ...... // your code  }
//call a function
myfunction();
?>


Example:


<?php
/ define custom function
function myfunction() {
   
    echo"myfunction value";

   
}
//call a function

myfunction();

// print output myfunction value
?>



Monday, 2 February 2015

Remove Sessions

To remove or delete session we use two function destroy and unset first use unset to remove all sessions and then destroy  example:


 
<!DOCTYPE html>

<html>
<body>

<?php
session_start();


$_SESSION['mysession1']='php-guides1';
$_SESSION['mysession2']='php-guides2';
$_SESSION['mysession3']='php-guides3';
$_SESSION['mysession4']='php-guides4';
$_SESSION['mysession5']='php-guides5';
?>

<p style="color:red">Session Values=<?php 
//Remove Or delete Sessions
session_unset();

session_destroy();

echo "<pre style='color:red'>";
print_r($_SESSION);
echo "</pre>";



?>

</p>


</body>
</html>



//print output

Session Values=
Array
(
)

Multiple Session Values

We can set multiple session at one time and easily fetch them in php Example:


<?php
<!DOCTYPE html>

<html>
<body>

<?php
session_start();


$_SESSION['mysession1']='php-guides1';
$_SESSION['mysession2']='php-guides2';
$_SESSION['mysession3']='php-guides3';
$_SESSION['mysession4']='php-guides4';
$_SESSION['mysession5']='php-guides5';
?>

<p style="color:red">Session Values=<?php 
echo "<pre style='color:red'>";
print_r($_SESSION);
echo "</pre>";?>
</p>

</body>
</html>
?>


Output Print: