1

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You echo $argument1 twice, don't know if you meant to do that
So since I pass the values as arguments and they have a before state e.g. "WILLIAM", "henry", "GaTe" they aren't echoed back to the browser this way. They are echoed and display the way you would expect them to after the strtolower, ucfirst functions have been applied. So does this mean the values are passed into the function as arguments then back out?
Since you are passing static values directly into the function, there is no reference or pointer or variable to refer to for those original values. But nonetheless, you are working with copies of the values. Copies of the values are manipulated, and then those altered values are being returned to where you called it, and whatever you do with those values from there have nothing to do with what went on inside the function, not tied to $argument1.
The return line is what is passing the processed variables back to the echo command. When you do echo function(), the echo command will echo whatever the function returns. Same concept for $var = function(), the value of $var will be set to whatever function() returns.
Perfect.. 100% understand it now. Thanks alot. I shall paste these last two paragraphs as a comment in my code incase I forget.
1

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.

3 Comments

Yep, I understand what those functions do e.g. strtolower, ucwords etc but I wanted to know if those names are being passed into the function through the arguments and processed? or if the variables are being passed out of the function and effect what ever is in the arguments of the echoed function. If that even makes sense,.
The variables $name1, $name2, ... are only defined within the function. In the first line, you pass "WILLIAM", "henry", and "gAtEs" as arguments to the function. Then, inside the function $name1 gets the value "WILLIAM", $name2 gets the value "henry", .etc
What I don't get is how the correct processed values are echoed back to the browser using this echo name_fixer("WILLIAM", "henry", "gAtEs"). The old values are still there so how does this work?
1

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)

10 Comments

@markus: I've read a lot of beginner books and most of them will turn a blind eye to best practices to teach the basic concepts. This is in fact a common practice, because best practices are a lot harder to teach noobies, because they consider bigger pictures, and noobies do not yet understand those bigger pictures.
I only partly agree. I think there are just too many bad books out there. It is not hard (and should be a MUST) for writers of beginners books to adher to basic best practices. The beginners books are the first and crucial impression and therefore should be serve as good models. No wonder that we see so much badly written code, if authors of beginners books are such bad code writers themselves.
Right, but this isn't really an applicable "basic" best practice, when you are talking about how functions work in general. It looks like he's at the part where he's learning how functions work in general. Maybe chapter 2 will be "okay, now that you understand how they work, let's talk about how you should use them - best practices." That is where your thoughts will come in.
This answer doesn't even remotely answer the question, so why is it upvoted?
I changed the function name to name_fixer, in the book it was fix_names and the variables were $n1, $n2, $n3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.