Home Tutorial Php PHP Variables Functions

 
 

Share on Google+Share on Google+
PHP Variables Functions
Posted on: January 12, 2010 at 12:00 AM
Advertisement
In this tutorial we learned about how to create functions or how to use predefined functions. If you want to see the real power of PHP you should use PHP functions. There are more than 700 in-built functions in PHP.

PHP Variables Functions

In this tutorial we learned about how to create functions or how to use predefined functions. If you want to see the real power of PHP you should use PHP functions. There are more than 700 in-built functions in PHP. A function is a name given to a block of statement that can be used whenever we required in our application. Using functions in your application saving lot of time instead of writing whole code and also it is more readable. First, we define a function by giving name to it and then call the function to execute the entire code. Let see the example :

<html>

<head>

<title>PHP Function</title>

</head>

<body>

<?php

function Hello()

{

echo "GOOD MORNING PEOPLE!";

}

Hello();

?>

</body>

</html>

The output of the above example is : GOOD MORNING PEOPLE!

In the above example first we define function called Hello() and then we write echo code within the curly braces and at last we call the function Hello(); to execute the entire code. So, this was the example of defining and declaring function in PHP. Lets go more deeper :

Passing Arguments in Functions

<html>

<head>

<title>Adding Parameters in Functions</title>

</head>

<body>

<?php

function Hello($name,$companyname)

{

echo "<h3>GOOD MORNING PEOPLE!</h3>"."<br/>";

echo "This is ".$name." from ".$companyname;

}

 

Hello("Suraj Mittal","Rose India!");

?>

</body>

</html>

The output of the above example is: GOOD MORNING PEOPLE!

                                                  This is Suraj Mittal from Rose India!

 

In the above example, we passed arguments in the function and then function call by writing actual name in the parenthesis.

PHP Functions - Return Values

<html>

<head>

<title>PHP Function Return Values</title>

</head>

<body>

<?php

function sum($x,$y)

{

$add = $x+$y;

return $add;

}

$total = sum("100","200");

echo "x + y = ".$total;

?>

</body>

</html>


The output of the above example is:  x + y = 300.

In the above example a function returns the value. Although, a function can return only one thing, however that thing can be any integer, float, array, string, etc. that you want to use in your application . A little change in the function call will return the value through function. We need to change when the function call, what we need to do is just to store the value into other variable and then echo the variable.

Advertisement

Related Tags for PHP Variables Functions:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 12, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com