Function confusion

i m learning php any best site, tutorials for learning php…???. it sound silly / stupid but i got little confusion with functions parameters below is what i mean:

<?php
function show($now){
  echo $now;
}

show('hello');//this will call function and execute the input parameter

//but when i create new parameter as below and call the function how is new parameter $top is being display as i haven't echo $top
$top = 'im here for you';

show($top);

When i pass new parameter $top does function become like below:
function show($top){
  echo $top;
}

show($top);

//does $top automatically replace $now everywhere in function, how it is working..?
 ?>

Hi there @esu, welcome to the forums :wave:

Before I answer your question I want to first go through some terminology. When programming it is important that everyone uses the same words to mean the same things, otherwise it just becomes confusing for everybody.

So first, as you stated this is a function:

function show($now){
  echo $now;
}

A function has a name (show in this case) and zero or more parameters, which are input for the function to work with.

In the following line, $top is a variable, not a parameter:

$top = 'im here for you';

The following is a function call, I’m calling a function with the argument hello:

show('hello');

When I do that, within the show function, the variable $now is set to hello. Outside of the show method there is no $now variable. We say that the variable is scoped to the function.

To illustrate, consider the following:

function show($now) {
   echo $now;
}

show('hello');
echo $now; // results in "PHP Notice:  Undefined variable: now"

As you can see, PHP tells me that the variable $now doesn’t exist. It exists as long as we’re inside the show function, then it’s gone again.

The other way around, the function has no access to anything that is defined outside itself:

$now = 'hello';
function show() {
  echo $now; // results in "PHP Notice:  Undefined variable: now"
}

$now is defined outside the show function, so show has no access to it, PHP will tell you it doesn’t exist.

So in order to give it access we use parameters to pass a variable from outside the function to inside the function:

function show($now) {
   echo $now;
}

$top = 'im here for you';
show($top);

so the value of the variable that is $top outside the show function is available as the $now variable inside the show function. You could say that the value of $top is copied to the $now variable within the show function.

Does that help, or just confuse you more? :smiley:

8 Likes

i may not be sound enough to explain in proper way

function show($now) {
   echo $now;
}

$top = 'im here for you';
show($top);

So Variable $now and $top are just way to communicate with function show it’s really doesnt matter at all, what matter is what inside in small brackets (),means argument matter while calling function not variable…

function show($now) {
   echo $now;
}

$top = 'im here for you';
show($top);

so when i call show function with $top variable,whats doing is:

function show('im here for you') {
   echo 'im here for you';
}

$top = 'im here for you';
show($top);

is that right, this is what i have understood from your explanation…

Yes - whatever you put in the brackets when you call your function from your main code, arrives at the function in $now, because that’s what you call it when you define the function.

It is :slight_smile:

The term scope is important here. PHP: Variable scope is the relevant PHP manual page. Also see PHP Variables. You can search for more descriptions of variable scope, it is a very common term. The details vary depending on language but the concept is relevant in nearly every computer language.

On the subject of terminology, if you are calling parentheses brackets then that is very uncommon.

Also the functions parameters can have default values which prevent errors and warnings from being thrown. It is also always good practise to set maximum error reporting and display the errors to the screen. Once the script has no errors then the display_errors can be removed to prevent any sensitive information being shown to other users.

<?php declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors','true');

function show1($now) {
   echo $now;
}


function show2($now='default value goes here') {
   echo $now;
}

function show3($now='default value') {
  // echo $now; not good practise

$now = __FUNCTION_NAME__;

return $now; // better to return a result
}

$test_variable = 'test_variable'';

if(FALSE {
  show1();
  show1('test parameter');
  show1($test_variable);
}


if(FALSE) {
  show2();
  show2('test parameter');
  show2($test_variable);
}

if(FALSE) {
  show3();
  show3('test parameter');
  show3($test_variable);
  echo show3();
  echo show3('test parameter');
  echo show3($test _variable);
}


Try toggling the FALSE value to TRUE one at a time.
have fun :slight_smile:

Default values are not for error control. They are for just as titled, to provide a default value. If your using them for error control you are probably doing something wrong.

It should also be noted that default values must come last (After any other parameters.)

2 Likes

True “default values are not for error control”.

I was trying to show an example and could not think of anything better :frowning:

Any suggestions for a better example?

Thanks to everybody for taking their time for helping me…i really appreciate your effort for this small confusion for you guys which was obviously big for me…thank you once again…

3 Likes

If we have to explain default parameters at all (and I’m not sure that we do) I’d stick to something really simple like

function greet($who = 'there') {
    echo 'Hi ' + $who;
}

This method can now be called with and without parameter:

greet('rpkamp'); // Hi rpkamp
greet(); // Hi there

This can be useful in situations where sometimes the input isn’t available and you want to fall back to some default value. That being said default values are overrated and abused way too much (feel free to fork a new thread from here if you want to discuss that, let’s not dilute this thread which such discussions).

3 Likes

I had to look it up, as I’ve always been taught that the ( and ) are known as brackets. Turns out that (well, according to Wiki and a few other sites) that it varies depending on where you come from, and as I’m from the UK, it’s common terminology here. I accept that in the programming context, it probably requires some clarification to distinguish between (, [ and { types.

2 Likes

Yeah I know that you British say things (such as in-built instead of built-in) that seem very wrong to me. I apologize for any difficulty I have with that but sometimes I do. To the extent that it is common for at least the British to call parentheses brackets, I must accept that.

Ah, now I’d definitely say “built-in” rather than “in-built”. But I’d never put a “z” in “apologise”. :slight_smile:

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.