Code: Select all
<?php
function sayHello($name)
{
return "Hello, {$name}";
}
echo sayHello('Simon');Code: Select all
foreach ($users as $user) {
echo sayHello($user->first_name);
}Moderator: General Moderators
Code: Select all
<?php
function sayHello($name)
{
return "Hello, {$name}";
}
echo sayHello('Simon');Code: Select all
foreach ($users as $user) {
echo sayHello($user->first_name);
}They are key. Essentially, in your function signature, you've said you're going to have a variable called $save_path, but inside the function you're referencing $type, which is what it was called elsewhere. The crucial thing is that those names match.simonmlewis wrote:So that is the key to this. Where is it wrong?? I'm guessing $type and $save_path are the key. What have I messed up?
Yes I think so. As I say, I think one of these functions is using one variable at the top, and then the wrong one at the bottom (based on a variable way up the page).Celauran wrote:Very simple example:The string 'Simon' gets passed to the sayHello function and is referred to internally as $name. Let's assume you wanted to run this function for a collection of users. You retrieve the users from your database and want to pass each of their names to this function. You're just going to call something likeCode: Select all
<?php function sayHello($name) { return "Hello, {$name}"; } echo sayHello('Simon');or something along those lines. Make sense?Code: Select all
foreach ($users as $user) { echo sayHello($user->first_name); }
Yes! The order of the parameters absolutely matters. Look at the mail() function, which I know you're familiar with. The recipient must be the first argument. If you try to pass the subject first, you're going to have a bad time. Functions spell out which parameters they're expecting and the order in which they're expecting them so they can make some assumptions about the types of data they receive and how those can be used.simonmlewis wrote:I see it takes the variables "in order".
Again, yes! This is key. We're concerned with the values, after all, not what variable those values are attached to. I try to name my variables and my function parameters descriptively so that it's easy to understand at a glance what they're doing and/or what they represent. I could have called $type $resize_type instead, but it's not terribly important.simonmlewis wrote:You have named them differently for whatever, reason, but the values are essentially the same.
Right. You've got $save_path in the signature (the bit between the parentheses) and $type in the function body. They need to match.simonmlewis wrote:So I am guessing the usage of these variables within the functions, are mismatched somewhere........
Great!simonmlewis wrote:That's it. That's done it. It's working locally and live (demo) in generating the thumbnails on the fly.
That's partly why descriptive naming is important. For functions you have written, you're going to know because you wrote it. For most functions, you'll have documentation explaining what each parameter is, which are required vs optional, and so on. It's also a good habit to get into to document your own functions so that when you come back to them in six months, or when someone else on the team needs to work with them, it's pretty clear what does what.simonmlewis wrote:So in that function where it has the variables one after the other, how do I know which one is which? As you say, they could be named so differently.
What's they key to that?
The function signature is the name of the function and the list of parameters it accepts. Everything up to the opening brace, essentially. The function body is everything between the braces.simonmlewis wrote:Also, you keep using the term "function signature". What do you mean?
Once they're generated, you needn't worry about it. Maybe write a crawler or something that will hit all your product pages, category pages, etc and generate the images for you. Run it overnight or something, when you know traffic is going to be low. Alternately, run it locally and upload the generated images.simonmlewis wrote:It now works on all the page we have worked hard on lately.
The category pages takes some churning, as it is generating the images. Whether that's ok SEO wise for now, or not, I'm not sure. Maybe we need to run one BIG page, maybe split into a few pages, and initiate that before we go live. Just to generate all the images.