Functions in PHP - An Overview and Usage

Functions in PHP - An Overview and Usage
Slide Note
Embed
Share

A function in PHP is a block of code that performs a specific task and can be reused multiple times in a program. PHP offers built-in functions like var_dump, fopen(), print_r, and user-defined functions, allowing customization and efficiency in code. Understanding function creation, parameters, and arguments are essential for effective programming in PHP.

  • PHP functions
  • PHP programming
  • User-defined functions
  • Built-in functions
  • Code efficiency

Uploaded on Mar 09, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Functions in PHP Presented by Mrs. RautAarti P. Assistant Professor Department of Comp. Sci. & IT. Deogiri College, Aurangabad

  2. Functions in PHP A function is a block of code written in a program to perform some specific task. A function is a block of statements that can be used repeatedly in a program. PHP function is a piece of code that can be reused many times. It can take input as argument list and return value. There are two parts of function: 1)Creating a function 2)calling a function

  3. Contii PHP provides us with two major types of functions: Built-in functions : PHP provides us with huge collection of built-in library functions. These functions are already coded and stored in form of functions. There are thousands of built-in functions in PHP like, var_dump, fopen(), print_r(), gettype() and so on. Eg:

  4. Conti User Defined Functions : Apart from the built-in functions, PHP allows us to create our own customized functions called the user-defined functions. Using this we can create our own packages of code and use it wherever necessary by simply calling it. Syntax function functionname() { //code to be executed }

  5. Creating a Function While creating a user defined function some rules are given below: 1)Any name ending with an open and closed parenthesis is a function. 2)A function name always begins with the keyword function. 3)To call a function we just need to write its name followed by the parenthesis 4)A function name cannot start with a number. It can start with an alphabet or underscore. 5)A function name is not case-sensitive.

  6. Function Parameters or Arguments The information or variable, within the function s parenthesis, are called parameters. These are used to hold the values executable during runtime. We can pass the information in PHP function through arguments which is separated by comma.These parameters are used to accept inputs during runtime. An argument is a value passed to a function and a parameter is used to hold those arguments. In common term, both parameter and argument mean the same. for every parameter, we need to pass its corresponding argument. Syntax: function function_name($first_parameter, $second_parameter) { executable code; }

  7. PHP Function Arguments We can pass the information in PHP function through arguments which is separated by comma. PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length argument list. We pass single argument in PHP function. eg:<?php function sayHello($name){ echo "Hello $name<br/>"; } sayHello("Sham"); sayHello("Vimal"); sayHello("John"); ?>

  8. PHP Call By Reference Value passed to the function doesn't modify the actual value by default (call by value). But we can do so by passing value as a reference. By default, value passed to the function is call by value. To pass value as a reference, you need to use ampersand (&) symbol before the argument name. <?php function adder(&$str2) { $str2="ok"; } $str="This is"; adder($str); echo $str; ?>

  9. PHP Function: Default Argument Value We can specify a default argument value in function. While calling PHP function if we don't specify any argument, it will take the default argument. <?php function sayHello($name= XYZ"){ echo "Hello $name<br/>"; } sayHello("Rajesh"); sayHello();//passing no value sayHello("John"); ?>

  10. PHP Function: Returning Value Functions can also return values to the part of program The return keyword is used to return value back to the part of program, from where it was called. The returning value may be of any type including the arrays and objects. The return statement also marks the end of the function and stops the execution after that and returns the value. Eg:

  11. Recursive Function in PHP PHP supports recursive function to call current function within function. It is also known as recursion. Recursive function is a function which calls itself again and again until the termination condition arrive. Eg: <?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); ?>

  12. Advantage of PHP Functions Code Reusability: PHP functions are defined only once and can be invoked many times, like in other programming languages. Less Code: It saves a lot of code because you don't need to write the logic many times. By the use of function, you can write the logic only once and reuse it. Easy to understand: PHP functions separate the programming logic. So it is easier to understand the flow of the application because every logic is divided in the form of functions.

  13. What is Array? Array variables are "special" because they can hold more than one value in one single variable. This makes them particularly useful for storing related values. An array stores multiple values in one single variable. Array is a collection of homogenous data item. We can access the values by referring to an index number. In PHP, the array() function is used to create an array: Syntax: array();

  14. PHP Array Types There are 3 types of array in PHP. 1)Indexed Array: Arrays with a numeric index 2)Associative Array: An array where each key is associated with a value. 3)Multidimensional Array:Arrays containing one or more arrays

  15. Question

  16. Thank you

More Related Content