PHP Tutorial Part 7 [PHP Functions, with Arguments, with more than one argument, Functions having Return values, Global Functions]

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...

PHP Video Tutorial Part 7

Part 6: Documentation + Video  [PHP Functions, with Arguments, with more than one argument, Functions having Return values, Global Functions]

YouTube Preview Image

Download Link :

Part 7  download

Size: 7.34 MB
And Also  you can check all older and Incoming tutorials here :

Tutorial Links

 

“Documentation”


Hello Friends,

Welcome To PHP Tutorial Part 7

Topic Covered: PHP Functions, with Arguments, with more than one argument, Functions having Return values, Global Functions

Well I think you read my first tutorial PHP all part, if not then kindly download from here:

Download Older One’s

  • Go to PHP folder and download rest all Parts of PHP tutorial.

______________________________________________________________________________________________

Today covered in this tutorial PHP Functions, with Arguments, with more than one argument, Functions having Return alues, Global Functions

PHP Functions:

The powerful feature of any programming language is the ability to define and use functions.PHP function is a bit of code that we can reuse quickly and easily in PHP pages when we need it.

For creating functions:

1. All functions start with the word “function()” .

2. The name can start with a letter or underscore (not a number).

3. Unlike string names the function names are case in sensitive.

4. Function code starts after Curl brackets “{” and end by closing a Curl brackets”}”.

5. The name of the function can be anything you want as similar to strings name can contain letters, numbers, underscores and dashes, but it cannot contain any spaces.


<?php
function hello()                    // Function declaration
{
echo “Hello Mr.Saini”;
}
// no output when we only declare the function, output is there only when we call it. let check
// the title is changed but no output mean code will not execute:)
hello();     //Function calling
?>

Now come to PHP Functions with Arguments:

For more flexibility and more functionality to functions we add arguments or parameters, these are like variables, and we can define more than one functions. But two function names must be different if same then it will error like:

“Fatal error: cannot redeclare.”

<?php

function hello($name)                    // Function declaration

{

echo “Hello $name”;

}

// no output when we only declare the function, output is there only when we call it.

hello (“Mr.Saini”) ;     //Function calling

echo “<br />”;

// you can use  variable or text in place of arguments thats why its flexible

// we can also use vairables in the arguments and more than one calls to same function.

hello(“Everyone”);     //Function calling 2nd time

echo “<br />”;

$user = “Manan Saini”;

hello(“$user”);      // use variable ; you see the flexibity with arguments

?>

You can see functions with argument has more flexible than static one, and function must be defined before calling it otherwise it will show error “fatal error: Undefined function” like that.

PHP Functions with more than one argument:

<?php

function hello($wishes, $name, $punct) // Function declaration

{

echo $wishes . $name . $punct . “<br />”;

}

$user = “Manan Saini”;  // variable declared

hello(“Gud Luck”,$user,”!”);       // function calling

/*

The no. of arguments must be same as no. of declared arguments.

Check what happens when we declare less argument. Delete any one of the above argument.

You see error missing arguments.

*/

?>

PHP Functions – Return values:

Functions can also be used to return values.

<?php

function addition($var1,$var2)

{

$sum = $var1 + $var2;

return $sum;

}

$total =addition(4,5); // declare variable we can use again & again in the code.

echo $total;

/* return can only return one value for returning more than one function we can use arrays. Take a look the below code.

*/

function add_sub($var1,$var2)

{

$add = $var1 + $var2;

$sub = $var1 – $var2;

$result = array($add,$sub);

return $result;

}

$total =add_sub(4,5);

echo “<br />” . “Addition is” . $total[0]. “<br />”;

echo “Subtraction is” . $total[1]. “<br />”;

?>

PHP Functions – Global :


<?php

$var = “Global”;

function glo() {

$var = “Local”;

}

glo();

echo $var ;

// just predict the output. What we do if we want to show locally let’s check.

?><br /><br />

<?php

$var = “Global”;

function glo1() {

global $var;                         // now it will show local

$var = “Local”;

}

glo1(); // because function name must be different so :P

echo $var ;

// another method to declare locally is:

?><br /><br />

<?php

$var1 = “Global”;

function glo2($var)

{

$var = “Local”;

return $var;

}

$var1 = glo2($var1);

echo $var1;

?>

Hope you enjoying the tutorial. If any problems mail me at:       encarta.mann@gmail.com

Thanks for reading this documentation.                                                           Continue in Next Edition ….

Thanks,

Tutorials By: Manan Saini Join us: www.lmtutorials.com

www.logicmatters.org

Thanks,

LM Team!

If you like it, then why not share with others and bookmark it :

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogosphere News
  • email
  • Internetmedia
  • LinkedIn
  • Live
  • MSN Reporter
  • MySpace
  • PDF
  • SphereIt
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • RSS
  • blogmarks
  • Great Effort

You can follow any responses to this entry through the RSS 2.0 feed.

Trackbacks / Pingbacks