NameSilo

PHP NEWB Question

Spaceship Spaceship
Watch

mistik

Established Member
Impact
0
I am havbing trouble understanding the concepts of function arugments and multiple arguments

Could someone explain the meaning of them for me please. Are they variables that function can only use? Or are they placeholders for when the function is excuted? I am very new to php.(as-if you cant tell.)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
A basic example of a function that accepts 2 arguments:

PHP:
function my_math_function($number1, $number2)
{
      $result = ($number1+$number2);
      return $result;
}

Is your basic function to add 2 numbers together so you don't have to continously re-write the same addition function.

To use this, you would call it like this:

echo my_math_function(5, 5);

should ouput:

10

Regards,
Peter
 
0
•••
0
•••
so then the agurments is like a placeholder then?
 
0
•••
mistik said:
so then the agurments is like a placeholder then?

Yes, basically you can have as many arguments as you desire.

Also, keep in mind you can have dummy arguments in place without having to use them:

Check it out:

function add_something($arg1, $arg2, $arg3 = '', $arg4 = '')
{
...
}

With that above, you are telling this function that you will always use argument $arg1 and $arg2 but $arg3 and $arg4 may or may not be used (notice how I assigned dummy blank values beside them?

Here is a perfect example:

PHP:
function do_math($arg1, $arg2, $arg3 = '', $arg4 = '')
{
    if (isset($arg3) AND $arg3 == 'subtract')
    {
        $result = ($arg1-$arg2);
    }
    else if (isset($arg3) AND $arg3 == 'add')
    {
        $result = ($arg1+$arg2);
    }
    return $result;
}
$result = do_math(5, 2, 'subtract');
or you may need this:
$result = do_math(5, 2, 'add');
 
0
•••
ok im getting it i thought they were like variables holding data when really they are just there to refer back to in the function, thanks for clearing that up for me psalzmann
 
0
•••
Appraise.net

We're social

Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back