Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - Functions and Arguments

Functions and Arguments - Images: Japanese

Hey there, everyone!

Next up, let's dive into 'functions' and 'arguments'. These are core concepts in PHP — and in just about every programming language out there.

"So what exactly are functions and arguments?" Good question. In general, a function is a named block of code that receives data (called arguments), performs some kind of processing, and produces a result. An argument is the value you pass into a function when you call it.

That explanation might still feel a little abstract, so let's set the theory aside for now and start by looking at the actual syntax for defining a function in PHP. After that, we'll connect it to the math concepts we've been using throughout this series to get a clearer picture of what functions really are.

Depending on the programming language, a block of code like a function is sometimes called a method.

Here's where it can get a little confusing: when you define a standalone block of code, it's called a function. But when you attach that same block of code to an object (a structure that bundles data and behavior together), it's called a method.

The difference between functions and methods is actually pretty minor — they're almost the same thing. Both let you give a name to a series of steps, save them, and call them later. The main difference is just where that code is stored.

For example, in Ruby — a fully object-oriented language — it's standard to call these methods. PHP isn't as strictly object-oriented, so function is the more common term there.

By the way, calling a function a 'method' (or vice versa) in casual conversation is generally fine and won't cause any real confusion.

With that in mind, here's what function definition looks like in PHP. Written in pseudocode first:

<?php
function functionName(argument){
    // processing goes here
}

And here's what it looks like when you define a function named hoge:

<?php
function hoge(){
    // processing goes here
}

Let's walk through it.

First, you write the keyword 'function'. Pronounced "fun-k-shun".

After that, put a single space and then the function name. PHP is pretty forgiving about whitespace — multiple spaces, tabs, and newlines are all treated the same — but using a single space is the cleanest and most readable approach. Note that you do need at least one space between function and the name, otherwise PHP won't be able to tell them apart as separate tokens.

The naming rules for function names are the same as for variables. If you need a refresher, check out that earlier article.
※ Forgot the variable naming rules? Head over here.

One important thing to note: in PHP, variable names are case-sensitive, but function names are not — uppercase and lowercase are treated as the same name.

Let's test that out. Take a look at this sample:

<?php
function hoge(){
	echo 'hoge';
}

function Hoge(){
	echo 'hoge';
}

In PHP, you can't redefine a function that's already been defined. The sample above throws an error, even though the names are spelled differently ('hoge' vs. 'Hoge'). PHP treats them as the same function name. This is a common gotcha — especially if you're coming from another language where function names are case-sensitive.

Moving on. Right after the function name comes the argument list, inside parentheses '()'. Even if your function takes no arguments at all, you cannot omit the parentheses. You always need to write '()'. When a function takes multiple arguments, separate them with commas, like this:

function functionName(arg1, arg2, arg3){
    // processing goes here
}

One more thing: there's no need to put a ; at the end of a function definition. The semicolon is not required here.

function functionName(arg1, arg2, arg3){
    // processing goes here
}; // The ';' here is not needed.

That said, adding a ; won't cause an error either. PHP allows empty statements, so it just gets treated as a no-op. Still, there's no point in writing it, so leave it out.

This kind of function definition is called a 'function statement' (also: function declaration, function definition).

Keep in mind: simply defining a function does not run it. The code inside the function body won't execute until you actually call the function. That's a common point of confusion, so make sure it sticks.

To call (execute) a function you've defined, the syntax is:

functionName(argument);

For example, to call the hoge function and pass it 1 as an argument:

hoge(1);

To pass multiple arguments, separate them with ,:

hoge(1, 2, 3);

To call a function with no arguments:

hoge();

Whether or not you're passing arguments, you always need the () when calling a function. Think of the parentheses as the trigger that actually fires the function.

The act of running a function or evaluating an expression is formally called evaluation. You'll see this term in textbooks and official documentation.

That said, 'evaluation' can feel a bit abstract, so throughout this site we'll just use the more intuitive word 'run' or 'execute'.

That covers the basics of defining and calling functions. Got the idea? Now let's talk about what functions actually are. We'll use math as a reference, just like we've been doing throughout this series.

Programming and math share the same underlying principles, after all.

Let's start with a simple math expression:

1 + 1 = 2

1 + 1 = 2 — this expression always gives you 2. There's nothing flexible about it. Let's make it more general by turning it into an equation with variables:

x + 1 = y

Now we have an expression where whatever value you plug in for x, adding 1 gives you y.

In basic math class, you'd be given something like "find y when x = 1" and you'd calculate the answer yourself. But in programming, things work a bit differently.

The key difference is this: you don't do the calculating yourself. A programmer's job is to write the expression and let the computer do the math.

Think about it for a moment.

A server runs 24/7, without complaint, faithfully executing whatever code was written for it.

Computers really are remarkable things, when you stop and think about it.

In programming, when you have a block of logic you might want to use more than once, you'll want to be able to call it whenever you need it. The same principle applies to variables — you store commonly used values in variables so you can pull them out at any time. Functions work the same way.

So let's implement that math expression in PHP.

The goal: "define a function that adds 1 to the value of variable x, stores the result in variable y, and outputs it — so we can call this logic anytime."

If we want to be able to call a block of code by name, we first need to give it a name. That's the function name.

Let's define a function called addition. Here's the shell, with an empty body for now:

<?php
function addition(){

}

Don't forget the '()' for the argument list. Now let's fill in the body: "add 1 to variable 'x', assign the result to variable 'y', and output it." Written straightforwardly:

<?php
function addition(){
    $x = 1;
    $y = $x + 1;

    echo $y;
}

Since we need to add something to 'x', we've given it an initial value of '1' as a placeholder. This is a valid function definition. Let's run it:

<?php
function addition(){
    $x = 1;
    $y = $x + 1;

    echo $y;
}

addition(); // Run the function.
2

Variable y outputs correctly. So far so good.

But this isn't very flexible yet. Since x is always hardcoded as 1, y will always be 2. It'd be much more useful if we could pass a different value for 'x' each time we call the function. That's exactly what arguments are for.

Here's the updated version using an argument:

<?php
function addition($x){
    $y = $x + 1;
    echo $y;
}

Notice where variable 'x' is now — it's inside the '()'. By putting it there, 'x' becomes an argument, which means we can pass a different value into the function every time we call it. Let's try passing '10':

<?php
function addition($x){
    $y = $x + 1;
    echo $y;
}

addition(10);

Running this gives:

11

The function adds 1 to whatever value x holds, so passing in 10 correctly outputs 11. By using arguments, you can build much more versatile and reusable functions.

The argument in a function definition is called a formal parameter, or simply a parameter.

The argument passed when calling a function is called an actual argument, or simply an argument.

When people say "argument" without qualification, they usually mean either or both.

<?php
function addition($x){ // '$x' here is a 'parameter' — it's part of the function definition.
    $y = $x + 1;
    echo $y;
}

addition(10); // '10' here is an 'argument' — it's the value passed at call time.

These terms show up in technical books and official documentation, so it's worth having a rough idea of what they mean. You won't hear them much in day-to-day work, though.

Let's get some practice with a slightly more complex example.

As mentioned in an earlier article, let's define a function that takes the base and height of a triangle and outputs its area. We'll call it area_of_triangle.

The formula for the area of a triangle is: base × height ÷ 2. Here's the PHP implementation:

<?php
// $width is the base and $height is the height.
function area_of_triangle($width, $height){
    $area = ($width * $height) / 2;

    echo 'A triangle with base ' . $width . ' and height ' . $height . ' has an area of ' . $area . '.';
}

The syntax might look a bit dense if you're still getting used to it — take your time working through it.

Let's run it to check. Passing a base of 10 and a height of 5:

<?php
// $width is the base and $height is the height.
function area_of_triangle($width, $height){
    $area = ($width * $height) / 2;

    echo 'A triangle with base ' . $width . ' and height ' . $height . ' has an area of ' . $area . '.';
}

area_of_triangle(10, 5);
25

25 — correct! We now have a function that automatically calculates and outputs the area of a triangle given its base and height.

So that's the concept of functions and arguments. If it's still clicking into place, don't worry — that's normal.

Variables let you call up a stored value; functions let you call up an entire block of logic — kind of like plugging values into a math formula. Bundling frequently used logic into a function is one of the most practical things you can do as a programmer.

Even if functions and arguments aren't fully clear yet, they'll start making sense the more code you write. A great exercise is to try turning math formulas you know into PHP functions — it's a solid way to build intuition.

That was a long one — thanks for sticking with it! Things are starting to feel properly programming-like, which is exciting. In the next article, we'll look at loops. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .