0

I am finding different functions in which the arguments seem to work or behave differently

I've tried a basic function with two parameters that are defined as variables inside the function, but the function i want to understand does not define the argument inside the function

For example:

 function myFunction($name, $age)
  {
    $name = ('maj');
    $age = ('31');
  }

Should, in theory(when I call the function), print to screen:

maj 31

But what about this example?

 function createTable($name, $query)
  {
    queryMysql("CREATE TABLE IF NOT EXISTS $name($query)");
    echo "Table '$name' created or already exists.<br>";
  }

 function queryMysql($query)
  {
    global $connection;
    $result = $connection->query($query);
    if (!$result) die($connection->error);
    return $result;
  }

So, here's where I am confused. Where in these functions are the arguments $name and $query defined?

1
  • Those functions already defined, you need to call these in the place where you want to execute to see the output ex: createTable('name', 'query') Commented Sep 1, 2019 at 1:09

2 Answers 2

1

Parameters are primarily (not always) for passing things INTO the function

So if you want your function to print those values you give to it, it would look like this.

function myFunction($name, $age) {
    echo $name;
    echo ' ';
    echo $age;
    echo PHP_EOL;
}

// call the function and pass the paramters into it

myFunction('maj', 31);
myFunction('John', 32);

RESULT:

maj 31
John 32

You dont define those 2 variables inside the function, they are defines and exist insode the function only by virtual of being added to the parameter list.

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

4 Comments

Thanks for all the comments and answers. From what I think you are saying, arguments are passed through to the function and then parameters are given later when the function is called to give a value to the arguments. Is that correct?
@MajdiKanaan - I think you are confusing the concept, parameters and arguments are nearly the same thing. The only difference is the context in which you are using/defining them. Maybe this question will help you understand. Basically, in function myFunction($name, $age) {}, $name & $age are the parameters. myFunction('maj', 31); "maj" and "31" are the arguments.
OK, that helps a bit, however, how does that apply to the $name and $query parameters above(in my initial question)? I don't understand how they are summoned by or to the function.
They are defined when you call the function. RiggsFolly's answer explains it.
1

A function is a block of statements that can be used repeatedly in a program. You have already defined the function now you need to call them where you want to execute. Information can be passed to function through argument which is comma delimited list of expressions. Arguments are evaluated from left to right. Following function has two arguments $name, $query Therefore when createTable function call you also need to pass those two arguments and those arguments are used in inside the function.

//define function
function createTable($name, $query) {
    queryMysql("CREATE TABLE IF NOT EXISTS $name($query)");
    echo "Table '$name' created or already exists.<br>";
}

//call the function
createTable('user', 'id INT NOT NULL AUTO_INCREMENT,
        PRIMARY KEY(id),
        NAME VARCHAR(30) NOT NULL');

For more info, you can refer PHP documentation.

Comments

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.