Php functions
<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
Hello PHP Function
PHP
Function Arguments
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
Output
Hello Sonoo
Hello Vimal
Hello John
Php function with arguments
<?php
function sayHello($name,$age){
echo "Hello $name, you are $age years old<br/>";
}
sayHello("Sonoo",27);
sayHello("Vimal",29);
sayHello("John",23);
?>
Output
Hello Sonoo, you are 27 years old
Hello Vimal, you are 29 years old
Hello John, you are 23 years old
Php call by reference
<?php
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'Hello ';
adder($str);
echo $str;
?>
Hello Call By Reference
PHP
Function: Default Argument Value
<?php
function sayHello($name="Sonoo"){
echo "Hello $name<br/>";
}
sayHello("Rajesh");
sayHello();//passing no value
sayHello("John");
?>
Output
Hello Rajesh
Hello Sonoo
Hello John
PHP
Function: Returning Value
<?php
function cube($n){
return $n*$n*$n;
}
echo "Cube of 3 is: ".cube(3);
?>
Cube of 3 is: 27
function using input
<?php
//add() function with two parameter
function add($x,$y)
{
$sum=$x+$y;
echo "Sum = $sum <br><br>";
}
//sub() function with two parameter
function sub($x,$y)
{
$sub=$x-$y;
echo "Diff = $sub <br><br>";
}
//call function, get two argument through input box and click on add or sub button
if(isset($_POST['add']))
{
//call add() function
add($_POST['first'],$_POST['second']);
}
if(isset($_POST['sub']))
{
//call add() function
sub($_POST['first'],$_POST['second']);
}
?>
<form method="post">
Enter first number: <input type="number" name="first"/><br><br>
Enter second number: <input type="number" name="second"/><br><br>
<input type="submit" name="add" value="ADDITION"/>
<input type="submit" name="sub" value="SUBTRACTION"/>
</form>
PHP Call By Value
<?php
function adder($str2)
{
$str2 .= 'Call By Value';
}
$str = 'Hello ';
adder($str);
echo $str;
?>
Hello
php call by refernce
<?php
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'This is ';
adder($str);
echo $str;
?>
This is Call By Reference
$a .= $b => $a = $a.$b
ist str loads by this is and addr functions calls with argument of $str, it gets the value which is in address of variable a, variable stores some value, that variable has address
the value loads in loads in str2, concatenate and prints
calles the address
call by referencve
- <?php
- function increment(&$i)
- {
- $i++;
- }
- $i = 10;
- increment($i);
- echo $i;
- ?>
output
11
PHP Variable Length Argument Function
- <?php
- function add(...$numbers) {
- $sum = 0;
- foreach ($numbers as $n) {
- $sum += $n;
- }
- return $sum;
- }
- echo add(1, 2, 3, 4);
- ?>
to do factorials
- <?php
- function factorial($n)
- {
- if ($n < 0)
- return -1; /*Wrong value*/
- if ($n == 0)
- return 1; /*Terminating condition*/
- return ($n * factorial ($n -1));
- }
- echo factorial(5);
- ?>
first it retursn 5*factorial(5-1), in second loop 5 alrady there, 4* factorial(3)
loop
No comments:
Post a Comment