function function_name(parameters)

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
guvna
New php-forum User
New php-forum User
Posts: 18
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: 903
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: 18
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: 903
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.
guvna
New php-forum User
New php-forum User
Posts: 18
Joined: Fri Mar 10, 2017 7:42 am

Mon Dec 23, 2019 11:39 am

OK I've had a go with parameters defined in () and without and keep getting fatal error message: called to undefined function.

//Variable font settings:
function fnt_var($fnt_name, $fnt_sz_xsm, $fnt_sz_sml, $fnt_sz_med, $fnt_sz_mlg, $fnt_sz_lge, $fnt_sz_xlg, $fnt_sz_xxl){
// function fnt_var(){
$fnt_name = 'Arial'; // default font
$fnt_sz_xsm = 7.5;
$fnt_sz_sml = 9;
$fnt_sz_med = 10;
$fnt_sz_mlg = 11;
$fnt_sz_lge = 12;
$fnt_sz_xlg = 14;
$fnt_sz_xxl = 20;
}

// fnt_var($fnt_name, $fnt_sz_xsm, $fnt_sz_sml, $fnt_sz_med, $fnt_sz_mlg, $fnt_sz_lge, $fnt_sz_xlg, $fnt_sz_xxl);
// code not permitted from this point in script.

//Page header
function header() {
fnt_var($fnt_name, $fnt_sz_xsm, $fnt_sz_sml, $fnt_sz_med, $fnt_sz_mlg, $fnt_sz_lge, $fnt_sz_xlg, $fnt_sz_xxl);
// fnt_var();
rest of script............ }

A pointer to what I should be doing may be required!
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Tue Dec 24, 2019 9:55 am

I wasn't sure how to approach an answer for this without making it seem really bad. So please accept this as constructive :)

Put your code in blocks using the </> button on the post a reply form; this makes it much easier for us to read your code. It also helps us see if it is one script or several scripts which is not clear from what you have posted.

Post the full error code you received, this includes more information such as line number. Include any steps that you took, and if you have modified the code since the error (i.e. if the code posted did not produce the error). Briefly explain what the code is supposed to do, say what it does do as well as what it doesn't do where applicable.

The code that you put produced the following error for me which was executed as file.php from the browser:
Fatal error: Cannot redeclare header() in /path/to/code/file.php on line 24
This is because you cannot use reserved keywords as function names; header() is already a function in PHP, so you cannot use that name for your own function.

I used the function name smeg as follows:

Code: Select all

<?php

//Variable font settings:
function fnt_var($fnt_name, $fnt_sz_xsm, $fnt_sz_sml, $fnt_sz_med, $fnt_sz_mlg, $fnt_sz_lge, $fnt_sz_xlg, $fnt_sz_xxl) {
  // function fnt_var(){
  $fnt_name = 'Arial'; // default font
  $fnt_sz_xsm = 7.5;
  $fnt_sz_sml = 9;
  $fnt_sz_med = 10;
  $fnt_sz_mlg = 11;
  $fnt_sz_lge = 12;
  $fnt_sz_xlg = 14;
  $fnt_sz_xxl = 20;
}

// fnt_var($fnt_name, $fnt_sz_xsm, $fnt_sz_sml, $fnt_sz_med, $fnt_sz_mlg, $fnt_sz_lge, $fnt_sz_xlg, $fnt_sz_xxl);
// code not permitted from this point in script.
//Page header
function smeg() {
  fnt_var($fnt_name, $fnt_sz_xsm, $fnt_sz_sml, $fnt_sz_med, $fnt_sz_mlg, $fnt_sz_lge, $fnt_sz_xlg, $fnt_sz_xxl);
// fnt_var();
//rest of script............
}

smeg();

which gave me the following errors:
Notice: Undefined variable: fnt_name in /path/to/code/file.php on line 21

Notice: Undefined variable: fnt_sz_xsm in /path/to/code/file.php on line 21

Notice: Undefined variable: fnt_sz_sml in /path/to/code/file.php on line 21

Notice: Undefined variable: fnt_sz_med in /path/to/code/file.php on line 21

Notice: Undefined variable: fnt_sz_mlg in /path/to/code/file.php on line 21

Notice: Undefined variable: fnt_sz_lge in /path/to/code/file.php on line 21

Notice: Undefined variable: fnt_sz_xlg in /path/to/code/file.php on line 21

Notice: Undefined variable: fnt_sz_xxl in /path/to/code/file.php on line 21
As the error states, the variables are not defined; note: this is the line which calls the function fnt_var() from inside the function smeg, to stop this error you should declare the variables inside smeg() or provide literals:

Code: Select all

function smeg() {
  $fnt_name = 'Arial'; // default font
  $fnt_sz_xsm = 7.5;
  $fnt_sz_sml = 9;
  $fnt_sz_med = 10;
  $fnt_sz_mlg = 11;
  $fnt_sz_lge = 12;
  $fnt_sz_xlg = 14;
  $fnt_sz_xxl = 20;
  
  fnt_var($fnt_name, $fnt_sz_xsm, $fnt_sz_sml, $fnt_sz_med, $fnt_sz_mlg, $fnt_sz_lge, $fnt_sz_xlg, 
  $fnt_sz_xxl);
}
Post Reply