PHP Function Arguments

Transforming Data with the Magic of Parameters

If you haven’t read my introduction to user-defined functions article, make sure to do that before continuing on. If you understand what functions are, keep going.

Let’s revisit the automotive plant that we looked at in the previous article.

Think about an automotive plant:

  1. You bring the materials to the plant (you are here).
  2. You feed the plant with those materials (and here).
  3. The plant takes the materials and somehow produces a car.

The automotive plant is the function that can be invoked to produce something. The plant() function that was created before had a local variable $type_of_plant that stored the type of plant that was to be echoed out by the plant() function.

<?php

function plant() {
    $type_of_plant = "automotive";

    echo "I'm an " . $type_of_plant . " plant<br>";
}

plant();
plant();

?>

Wouldn’t it be great if we could use the plant() function, pass it some string, and have it echo out whatever we passed to it? We want to deliver a note to our plant. The employee inside of the plant receives the note and is able to read that note. The note tells the plant what type of plant it is.

That’s where arguments come to play. An argument is the data that we pass to the function when we invoke the function. Let’s look at the anatomy of the function a little closer.

function function_name( $parameter_1, $parameter_2, ... ) {
  // function body
}

We looked at the overall function declaration in the last article. The only new concept should be the parameters that are declared between the parentheses. You may have noticed that earlier I was talking about arguments and now I’m talking about parameters. When we call the function, we pass arguments; when we declare a function, we declare parameters. Quite a few programmers get that confused.

Functions can have multiple parameters. Each parameter is separated by a comma. The parameter naming convention is identical to the variable naming convention.

When passing arguments to the function, just envision that you’re assigning values to the parameters.

function_name("Subscribe", "Please");

Let’s modify the plant() function so that it can accept an argument and echo it out. We’ll declare a new function called plant(). That function will have one parameter $type_of_plant. The function will echo out whatever is assigned to the $type_of_plant parameter. The plant($type_of_plant) function will be called twice. Each time, a new argument will be supplied.

<?php

function plant($type_of_plant) {
    echo "I'm an " . $type_of_plant . " plant<br>";
}

plant("Automotive");
plant("Vegetable");

?>

Let’s look at the code above in more detail.

  1. PHP skips over the function declaration.
  2. PHP encounters the plant(“Automotive”) function call on line 7.
  3. PHP finds the plant() function that’s being invoked and assigns the value “Automotive” to the $type_of_plant parameter on line 3.
  4. PHP enters the the function body and echoes “I’m an Automotive plant.”
  5. PHP reaches the end of the function and returns to line 7. The statement terminates with the semi-colon.
  6. PHP moves to the next line and encounters the next function call, plant(“Vegetable”).
  7. PHP finds the plant() function that’s being invoked and assigns the value “Vegetable” to the $type_of_plant parameter on line 3.
  8. PHP enters the function body and echoes “I’m an Vegetable plant.”
  9. PHP reaches the end of the function body and returns to line 8. The statement terminates with the semi-colon.

What if no argument is passed to the function plant()? Since it expects an argument, an error will be thrown. A default value can be assigned to the $type_of_plant parameter during function declaration. If plant() is invoked without an argument, the default value would be displayed. If plant() is invoked with an argument, the argument would override the default value, and the argument would be displayed. Assigning a default value to a parameter is syntactically identical to assigning a value to a variable ($type_of_plant = “Automotive”).

<?php

function plant_two( $type_of_plant = "Automotive" ) {
    echo "I'm a(n) " . $type_of_plant . " plant<br>";
}

plant_two();
plant_two("Subaru");

?>

In the example above, when plant_two() is invoked without an argument, “I’m a(n) Automotive plan” is echoed out. When plant_two(“Subaru”) is invoked with an argument, “I’m a(n) Subaru plant” is displayed.

A function can be declared with some parameters containing default values and others without. The default-value-parameters will have to be pushed to the end of the declaration. Your required parameters will have to appear in the beginning. When working with multiple parameters, you have to provide arguments in the order that the parameters were declared. If the default parameters were in the beginning, and you skipped over them, how would PHP know what you skipped over? It just sees values coming in. There are programming languages where you can explicitly assign arguments to parameters when invoking functions, but PHP is not one of those languages. As a matter of fact, most programming languages follow the same rules as PHP does.

function func_name($p_1, $p_2, $p_3 = "Default") { }

Let’s look at an example where we mix both types of parameters. We’re going to create a function called car(). That function will be able to accept three arguments: the vehicle brand, the vehicle model, and the vehicle transmission. The $brand and $model parameters will be required. The $transmission parameter is optional and will default to “Manual.” The function body will echo out “You just a bought a BRAND MODEL in a(n) TRANSMISSION transmission.”

?php

function car( $brand, $model, $transmission = "Manual" ) {
    echo "You just bought a " . $brand . " " . $model . " in a(n) " .
        $transmission . " transmission<br>";
}

car( "Subaru", "WRX STi" );
car( "Jeep", "Wrangler", "Automatic" );

?>

During the first car(“Subaru”, “WRX STi”) function call, the $transmission argument is omitted. The $transmission parameter is assigned the default value “Manual.” As a side note, the STi only comes in a Manual transmission. Good job Subaru! The string “You just bought a Subaru WRX STi in a(n) Manual transmission” is echoed out.

The Jeep Wrangler comes in both options and the person that’s buying it just happens to want an automatic transmission. The car(“Jeep”, “Wrangler”, “Automatic”) function is called and all three arguments are specified. PHP outputs “You just bought a Jeep Wrangler in a(n) Automatic transmission.”

Function arguments are an integral part of programming. Make sure that you understand them.

PHP Tutorial Series

Continue your learning with these articles

PHP User Defined Functions

EMPOWERING CODERS TO SHAPE LOGIC THEIR WAY

PHP – P35:USER DEFINED FUNCTIONS

Functions can be a difficult topic to grasp if you’re a new programmer. I’m sure you were exposed to functions in math, but do you really understand what the term “function” means?

PHP Function Arguments

Transforming Data with the Magic of Parameters

PHP – P36: Function arguments

An argument is the data that we pass to the function when we invoke the function. You’re not arguing with anyone; you’re just passing data to your function.

PHP Functions Returning Values

NAVIGATING CODE WATERS FOR TREASURED RETURNS

PHP – P37:FUNCTIONS RETURNING VALUES

unctions can return values: there I said it. When we looked at functions previously, those functions just echoed some string. Although that is one use of a function, functions are more powerful than simply echoing out statements. We can use functions to do something and then return a calculated value to us.

Leave a Reply