function function_name(parameters)

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
guvna
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Mar 10, 2017 7:42 am

Wed Dec 04, 2019 4:22 am

I'm trying to learn and understand the correct terminology for some php script being used to create a pdf document using fpdf.

function show_other_info($sale_id, $prepayment_info, $pdf, $y_start=180, $fnt_sz, $style=4){

What is the correct term to use for the items in the parenthesis?

Are they variables, strings, arguments or something else altogether?

Do items in the parenthesis have to be defined in the function and/or somewhere else in the same script?

I've searched online using things like w3schools but they just cover a basic function syntax, php.net seems to reference 'passing arguments' so any comments or pointing in the right direction would be greatly appreciated.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 864
Joined: Mon Feb 22, 2016 5:52 pm

Wed Dec 04, 2019 9:50 am

What is the correct term to use for the items in the parenthesis?

Are they variables, strings, arguments or something else altogether?
They are called 'parameters' or 'arguments'.
Do items in the parenthesis have to be defined in the function and/or somewhere else in the same script?
No, as they are declared in the parenthesis; without wishing to confuse you and answer a question you haven't answered; they become local variables, local and available only within the function.

When you call the function, you supply a value which can either be a literal or a variable.

Code: Select all

$variable = 'My variable value';
my_function ($variable, 'literal');
the above code supplies the first parameter as a variable which needs to be declared before calling the function, the second parameter is supplied as a literal string; sometimes this is known as hard coded since it does not change.

Supplying a literal to a function call is a bit limiting (since it is hard coded), so not normally done.

You will often find terms which mean the same thing as some languages use slightly different terminology to mean the same thing, but it suits the way they do things.
guvna
New php-forum User
New php-forum User
Posts: 17
Joined: Fri Mar 10, 2017 7:42 am

Thu Dec 05, 2019 8:53 am

OK, so any parameter in a function() needs to be predefined before the function is called.
Presumably either:
- within the same php tag as a declared variable or another function() that has been called within the php tag.
- from an include().
- from a require().

If a parameter is not defined in a function() would this generate a runtime error such as:
undefined variable ... line nnn?

to continue based on your example:
$variable = 'My variable value';
$x=12;
my_function ($x, $variable, 'literal'); //$x could be redefined later within the script.

could also be scripted as:
$variable = 'My variable value';
my_function ($x=12, $variable, 'literal'); //$x is set at this value.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 864
Joined: Mon Feb 22, 2016 5:52 pm

Mon Dec 09, 2019 11:50 am

OK, so any parameter in a function() needs to be predefined before the function is called.
Kind of correct if slightly re-worded:

Functions need to be supplied with the correct number of values. Some values can set to a default.

Code: Select all

function threevalues ($x, %y, $z){
  //Must supply function call with three values
  return $x + $y + $z;
}

echo threevalues (1, 2, 3); // will execute correctly
echo threevalues (1, 2); // will cause an error

Code: Select all

function defaults ($x, $y = 2, $z = 3){
  // must be supplied with at least one value
  return $x + $y + $z;
}

echo defaults (); // will cause an error
echo defaults (1); // will use default values for $y and $z
echo defaults (1, 3); // will use default value for $z
echo defaults (1, 3, 5);
echo defaults (1, 3, 5, 6); // will cause an error
Presumably either:
- within the same php tag as a declared variable or another function() that has been called within the php tag.
- from an include().
- from a require().
Variables defined in a function, stay in the function and cannot be used anywhere else unless declared as global, and will reset between each call unless declared static.

Static and global variables are best left until you get a handle on function calls, I will say that global variables are considered bad practice, although they seem quite appealing.
If a parameter is not defined in a function() would this generate a runtime error such as:
undefined variable ... line nnn?
Yes
to continue based on your example:
$variable = 'My variable value';
$x=12;
my_function ($x, $variable, 'literal'); //$x could be redefined later within the script.

Code: Select all

function my_function ($x, $variable, $literal) {
  echo $x, ', ', $variable, ', ',$literal;
  $x = 'Something else';
}
$variable = 'My variable value';
$x=12;
echo my_function ($x, $variable, 'literal'), '<br>';
echo my_function ($x, $variable, 'literal'); 
Thinks of it as - the value of $x supplied to the function can only be re-defined within the function. Essentially, you will have 2 variables named $x, one within the function and one outside of it. I don't want to complicate things further, but, you can pass variables by value and by reference (meaning you would have one variable named $x), this is very rarely needed and is similar to global variables where they are considered bad practice.
could also be scripted as:
$variable = 'My variable value';
my_function ($x=12, $variable, 'literal'); //$x is set at this value.
I wouldn't suggest doing this as you may get different results in different versions of PHP (including future versions). It looks like a bug waiting to happen.


The best way to get your head around all of this is to keep trying things and see what happens, the worst thing that can happen is that you get an error. Keep trying things to see where you can break it.

Do take the advice to ignore Global, Static and References at this time, it will only help confuse the matter; I had to mention them for completeness.
Post Reply