How to pass arguments to a function in PHP?

Member

by mac , in category: PHP General , 6 months ago

How to pass arguments to a function in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , 6 months ago

@mac 

In PHP, you can pass arguments to a function by specifying them inside the parentheses when you define the function. Here's an example:

1
2
3
function myFunction($arg1, $arg2) {
  // Code that uses $arg1 and $arg2
}


In this example, the myFunction function takes two arguments: $arg1 and $arg2. You can name the arguments anything you want, as long as they are valid variable names in PHP.


To call the function and pass arguments to it, you simply include the values of the arguments inside the parentheses when you call the function. For example:

1
myFunction("hello", 42);


In this example, we are calling the myFunction function and passing two arguments: the string "hello" and the integer 42.


When you call a function, the arguments you pass are assigned to the corresponding parameters in the function definition. In this example, $arg1 will be assigned the value "hello", and $arg2 will be assigned the value 42.