How to Create and Call PHP Functions

Part 1 of 3:

Creating a Function

  1. How to Create and Call PHP Functions Picture 1
    Create a new PHP file on your web server, and open it in your favorite text editor.
  2. How to Create and Call PHP Functions Picture 2
    Start your file by typing the open and close PHP tags with some space in between to work with.
  3. How to Create and Call PHP Functions Picture 3
    Type this on a new line between the two PHP tags.
  4. How to Create and Call PHP Functions Picture 4
    Whenever you want to create a function, always start on a new line with the word "function", hit the space bar and type its unique and descriptive name followed by two parenthesis, then type an open curly brace. In this case i chose to call the function "my_function," in your scripts you should give your functions better names.You may have noticed the "$input" variable in between the parenthesis. This is called an argument. It is the input that the function will return as an output. PHP functions can have multiple arguments, as long as they are separated by commas. Observe the example above.
  5. How to Create and Call PHP Functions Picture 5
    Now type this code inside the curly braces.The command "return" does exactly what is says, it returns the output. In this case the output is $input minus two, times 10. Take note that once the output has been returned the function is exited, and no code after it will be executed.
Part 2 of 3:

Calling a Function

  1. How to Create and Call PHP Functions Picture 6
    Type the code below on a new line after the closing curly brace of the function.This line of code is making a call to "my_function." In this case we will give my_function an input of 8, and PHP will echo the returned output value. Test the script, you will get a value of 60.
  2. How to Create and Call PHP Functions Picture 7
    Now type this code on a new line.The magic of coding with functions is the ability to reuse and recycle the same code over and over. We are now calling my_function two more times and reusing its code. Also notice how the in the code above the call to my_function is treated like a number. Always treat calls to functions as the datatype you expect to get back (whether it is a number, string, boolean, or resource). Test the script, you will get a value of 60 followed by a value of 260.
Part 3 of 3:

Functions and Variables

  1. How to Create and Call PHP Functions Picture 8
    Add three lines of code to your script.
  2. How to Create and Call PHP Functions Picture 9
    Add these before any other code at the beginning of your function.
  3. How to Create and Call PHP Functions Picture 10
    Add this line right after the closing curly brace of your function.
  4. How to Create and Call PHP Functions Picture 11
    For sake of clarity here is the entire code sample.
  5. How to Create and Call PHP Functions Picture 12
    A variable declared inside a function cannot be used outside the function. For example if i tried to echo $input outside of the function, PHP would throw an error because i have not created that variable outside of the function. So note that variables created in a function (including the arguments) can only be used inside that function. Variables declared outside of a function can only be used outside of functions (unless you use them as an argument of course).That said, there is a command that allows you, in a function, to use a variable created outside a function. That command is "global", in the code above we use global with the variable $num so we could change it or access its value in our function. If you test the code, you will get the values 50 and 240.
  6. How to Create and Call PHP Functions Picture 13
    Play around with functions! Build your own function that is really useful. Use them in your future PHP projects. Using functions can improve code modularity and the size of the overall project.
4.5 ★ | 2 Vote

May be interested

  • JavaScript functionsJavaScript functions
    a function is a group of reusable code that can be called anywhere in your program. this makes it unnecessary to write the same code over and over again. it helps programmers write modular code. functions allow a programmer to divide a large program into small and manageable functions.
  • Summary of trigonometric functions in ExcelSummary of trigonometric functions in Excel
    radians trigonometric functions, degrees, cos, ... in excel will help users to calculate quickly, compared to manual methods.
  • How to use functions in Less CSSHow to use functions in Less CSS
    functions greatly simplify the programming experience, especially when writing css code. here's how to use functions in less css.
  • Update activity in GitUpdate activity in Git
    tom performs the simulation operation and sees a new file string.c. he wants to know who added this file to the repository and for what purpose, so he runs the git log command.
  • How to create a quick reply to a call on iPhoneHow to create a quick reply to a call on iPhone
    on the iphone there is a quick reply function by texting calls that you cannot listen to, with the option to edit the message content.
  • How to create and delete tables in ExcelHow to create and delete tables in Excel
    how to create and delete tables in excel. excel is a great tool for calculating, it supports many calculation functions, logical functions ... but for the numbers to be presented scientifically and beautifully you need to create tables for data. this article helps you to create and delete tables in the table
  • Complete financial functions in Excel you should knowComplete financial functions in Excel you should know
    fv, date, time, hour, workday, ... functions are the basic financial functions with the same way of getting color codes in excel. show you how to use these financial functions. click view now!
  • Call command in WindowsCall command in Windows
    the call command calls a batch program from another program without stopping the original batch program. the call command accepts the label as its target.
  • Free Conference Call - Download Free Conference Call here.Free Conference Call - Download Free Conference Call here.
    free conference call is a free online meeting tool that allows screen sharing. this award-winning tool allows you to host an unlimited number of meetings with up to 1,000 participants.
  • How to call a contact on the iPhone main screenHow to call a contact on the iPhone main screen
    to quickly call a certain number of contacts from the iphone home screen, you can actually go through the intermediate website.