NameSilo

PHP NEWB Question

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
0
Close
so then the agurments is like a placeholder then?
 
0
Close
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
Close
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
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

A browser tab with a favicon next to the page title
Search members
You can now search for members by name, company, location and more. Open the search at the top of any page and choose Members. You will...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back