LondonGuy
LondonGuy

Reputation: 11098

What exactly is passed as arguments to PHP functions?

After several months of trying to get my head around PHP frameworks, moving from one to another with basic php knowledge I decided to call it a day with frameworks and go back to the php books and learn from scratch so in future in the near future I can start building my website mvc style without having to learn some other framework that would be abandoned when a newer version came out.

Anyway I purchased Learn PHP, MySQL and Javascript book by Oriely Media and have been practising and find it very interesting.

HOWEVER...

I am trying to get my head around this:

<?php

echo name_fixer("WILLIAM", "henry", "gAtEs");

function name_fixer($name1, $name2, $name3) {
    $name1 = ucfirst(strtolower($name1));
    $name2 = ucfirst(strtolower($name2));
    $name3 = ucfirst(strtolower($name3));

    return $name1 . " " . $name2 . " " . $name3;
} 

I have an idea what is going on but my question is when the function is called/echoed are the names I'm passing as arguments being passed into the function?

I would like to know exactly what is going on. For some reason this is the only part of the book so far where the writer hasn't gone into detail about exactly what is happening.

A thorough explanation would be appreciated greatly and allow me to move on to the next part of the book.

Upvotes: 1

Views: 72

Answers (3)

CrayonViolent
CrayonViolent

Reputation: 32532

Yes, the names you are passing as arguments get passed into the function.

myFunction ('a','b'); // call myFunction, passing 2 values

// function receives 2 values
function myFunction($argument1, $argument2) { // <-- function declaration
  echo $argument1 . "<br/>";
  echo $argument2 . "<br/>";
}

The idea is that you pass values to the function and then those values are put into the variables you specify in the function's declaration of arguments.

You can call your argument variables anything you want, so they don't have to be $argument1 or whatever. They are assigned values in the same order as what was passed, so 'a' gets assigned to $argument1, and 'b' gets assigned to $argument2.

There's a lot more to it than that, like passing more than just a single value (you can also pass things like arrays and objects), and you can also pass by reference, etc.. but you should get to that in your books. The overall point here though is that you pass a value to the function and the variables specified in the argument area receives those values, so that the function can do stuff with them.

Upvotes: 1

markus
markus

Reputation: 40685

It seems to me (at least based on this example) you've bought the wrong book. The function you're posting uses an anti-pattern and should be written like this:

function fixName($name)
{
    $fixedName = ucfirst(strtolower($name));

    return $fixedName;
}

... and then called for each name.

What it does:

It changes all letters to lowercase and then the first letter to uppercase. It returns the properly formatted name.

Additional tip: Use verbs for function names instead of nouns. It makes for much more readable code. A function usually 'does' something (verb) instead of 'is' something (noun).

fixName($name)

...is much easier to read than

nameFixer($name)

Upvotes: 1

Sam Magura
Sam Magura

Reputation: 990

The first line calls name_fixer with a first, middle, and last name, then prints the function's output. name_fixer takes three names as arguments and does the following to each:

Converts the name to lowercase: strtolower("WeeEeeEE") = "weeeeeee"

Capitalizes the first letter: ucwords("weeeeeee") = "Weeeeeee"

Then the function returns a single string containing the three fixed-names separated by spaces.

Upvotes: 1

Related Questions