PHP Variables Functions


 

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.

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;

}

0

 

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

?>

1

</body>

</html>

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

2

                                                  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>

3

<head>

<title>PHP Function Return Values</title>

</head>

4

<body>

<?php

function sum($x,$y)

5

{

$add = $x+$y;

return $add;

6

}

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

echo "x + y = ".$total;

7

?>

</body>

</html>

8


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.

Ads