Php – Functions

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-functions/
PINTEREST
PINTEREST
INSTAGRAM

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations

Up to now we have write simple and little codes but a real applications can not be accomplished with this type of code. We need to write a lot of code and use some part of the code again and again without same copies. Functions gives us the ability to use given part of code to run multiple time and from different parts of the applications. Functions also provides some abstraction so we only provide required parameters and get the result we do not need to now the implementation details.

Define Function

Functions have body where the function code is implemented. Function is defined with function keyword. After that the name of the function and the parameters are provided inside brackets. And the last and the biggest part is the function body in curly brackets. Here is syntax.

In the example we will create a function name message which have no parameter and only prints some text. Parameters in function definition are optional so if we do not need them just () is enough. We will call our function with its name and parameters but there is no parameter.

LEARN MORE  Windows Dir Command Tutorial With Examples To List Files and File Information

Pass Parameters

Previous example we do not provided parameter to the function or defined it.  Most functions generally requires parameters to complete given job. We can define parameters after function name in brackets () like a normal variable name.

In this example we will define two variables named $a and $b . The function name will be sum because we will sum the given variables and print the result inside function.

Return Value

In previous example we have completed related operations inside function. But the general usage is a bit more different. Functions generally used to return some data or result. We will use return keyword in order to return result from the function.

The example is the same with previous one but we have returned result and printed outside of the function which makes things more clear and easy to understand.

Pass Parameters By Reference

Parameters normally passed by copying their values into given function parameter variables and any change in the function parameter variables do not affects the provided parameters. In some situations we may want to change provided parameters in the function. For this we should provide parameters by reference. We will use & in the function definition before parameter names.

LEARN MORE  What is Microsoft Powershell ? How Can I Automate Tasks?

In the example we will provide variables to the function named increase as reference.

This will print 6 because $x is passed as reference to the $a parameter and the $a parameter is increased one.

Default Parameter Values

While providing parameters to function some parameters may get generally the same value. While calling this function multiple times things will be trivial task. We can set parameters default value if they are not explicitly defined.

In this example we set the function sum parameter $b as 5 as default but if we want we can specify explicitly like normal function call.

Dynamic Function Call

Dynamic function call is an interesting feature about Php functions. We can call a function by specifying its name as string value.

In this example we will call the function sum with a string variable named $functionname .

Recursive Functions

Some original problems need more original and efficient solutions. Recursive functions used to dive parse a big problem into little parts just calling itself again and again. Here is an recursive example

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-functions/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)