Writing Functions In PHP – Introduction
Thewebsqueeze blog post: This beginner PHP tutorials will teach you how to define functions, how to call functions, how to pass some simple arguments into a function and how to develop a function that can be reusable. This is the first tutorial in a series!
You are here: Home » Blog » Tutorials » Writing Functions In PHP – Introduction
The real power of PHP comes from its functions. If you’re looking for a way to save time when you program, look no further. Functions are going to allow us to be able to write code in one place and reuse it as many times as we want by just calling the function.
Creating functions lets you reuse code that you’ve used before without having to rewrite the whole thing again and again. When you understand and know how to use functions, you will be able to save a ton of time and write code in a more readable format!
User-Defined Functions
In PHP, there are more than 700 built-in functions. We are not going to use any of it in this tutorial but we are going to build our very own function.
Let’s take a look at the following typical structure of a function. When we want to declare a function, we would say function to tell PHP that we are defining a function, followed by the name of the function and any of the arguments that belong to the function.
<?php
function name($arguments){
statement;
}
?>
The name of a function can be just about anything you want. It can contain letters, numbers, underscores and dashes. It can’t contain any spaces. It must start with a letter or an underscore. Unlike string names, function names are going to be case insensitive which means that we can define a function with capital letters in it and actually called it with all lower case letters and it still finds the function and knows which one we meant. But that’s a bad programming practice and a bad habit.
So if you defined a function in capital letters, you call it with all capital letters. If you defined them with lowercase letters, you call it with lowercase letters. Let’s start defining our simple function.
Function Without Arguments
<html>
<head>
<title>Writing Simple Function In PHP</title>
</head>
<body>
<?php
function say_hello(){
echo "Hello World!";
}
say_hello(); /* calling out the function */
?>
</body>
</html>
Output: Hello Word!
This simple function was executed by calling the function with say_hello(); echo out “Hello World!”. Next we will look into another type of function with an argument passed to the function.
Function With Arguments
The reason we want to use arguments is that we want to give our functions flexibility. Flexibility is really a good and powerful thing for us because we can have code that we can reuse for different circumstances. Let’s update our first example with arguments.
<html>
<head>
<title>Writing Simple Function In PHP</title>
</head>
<body>
<?php
function say_hello($word){
echo "Hello {$word}!";
}
say_hello("Everyone"); /* calling out the function */
?>
</body>
</html>
Output: Hello Everyone!
The same output will be printed out on your browser for this function. The only different thing about this function is its flexibility. This function has flexibility built into it because it can pass an argument and based on that argument, it can do different things in the code. This is what we called a dynamic function.
Defining Functions
Before we move on to a more advanced concept of function, you need to know this basic idea of defining a function.
- A function needs to be defined before we can call it.
- You can’t define a function more than once.
- You can call a function as many time as you like.
- You can declare function inside if else statement.
- You can declare functions inside other functions.
What’s Next?
So far, you’ve learned how to define functions, how to call functions, how to pass some simple arguments into a function and how to develop a function that can be reusable.
In the next tutorial, I’ll be talking about more advanced tips that you can do with passing arguments into a function and some other things that we can do with functions. Stay tuned!