PHP - Defining Function Parameters

Introduction

Functions can optionally accept one or more arguments.

You specify one or more corresponding parameters when you define your function.

A parameter is a variable that holds the value passed to it when the function is called.

To specify parameters for your function, insert one or more variable names between the parentheses:


function myFunc($oneParameter, $anotherParameter) {
          // (do stuff here)
}

You can include as many parameter variables as you like.

For each parameter you specify, a corresponding argument needs to be passed to the function when it's called.

The arguments passed to the function are then placed in these parameter variables. Here's an example:

Demo

<?php

function helloWithStyle($font, $size) {
    echo $font . "\n";
    echo $size . "\n";
}
helloWithStyle("Helvetica", 2);
helloWithStyle("Times", 3);
helloWithStyle("Courier", 1.5);
?>/*from   w w  w  .ja  v  a  2 s. com*/

Result

Related Topic