Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Declaring Functions and Using Parameters | Functions in PHP
Quizzes & Challenges
Quizzes
Challenges
/
PHP Core Concepts

bookDeclaring Functions and Using Parameters

To understand how functions in PHP handle parameters and return values, you can start with their basic structure. You define a function using the function keyword, followed by its name and a set of parentheses. Inside these parentheses, you can include parameters, which serve as placeholders for the values you pass into the function when it is called.

add_numbers.php

add_numbers.php

copy
1234567891011
<?php // This function takes two numbers as parameters and returns their sum. function addNumbers($num1, $num2) { $sum = $num1 + $num2; return $sum; } // Call the function with arguments and display the result. $result = addNumbers(5, 10); echo "The sum is: " . $result; ?>

The function is named addNumbers and it takes two parameters: '$num1' and '$num2'. These parameters allow you to provide input values when calling the function. Inside the function body, the two parameters are added together and stored in a new variable, '$sum'. The return statement then sends this value back to the part of the script that called the function.

Note
Note

When you call addNumbers(5, 10), the values 5 and 10 are passed into the function as '$num1' and '$num2'. The function calculates their sum and returns it. The result is then stored in the variable '$result', which is displayed using echo.

Using parameters makes your functions flexible and reusable, since you can pass in different values each time you call the function. The return statement is essential when you want your function to produce a result that can be used elsewhere in your code.

question mark

What is the role of parameters in a PHP function?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you show me an example of how to call the `addNumbers` function?

What happens if I call the function without providing both parameters?

Can you explain the difference between parameters and arguments in this context?

bookDeclaring Functions and Using Parameters

Pyyhkäise näyttääksesi valikon

To understand how functions in PHP handle parameters and return values, you can start with their basic structure. You define a function using the function keyword, followed by its name and a set of parentheses. Inside these parentheses, you can include parameters, which serve as placeholders for the values you pass into the function when it is called.

add_numbers.php

add_numbers.php

copy
1234567891011
<?php // This function takes two numbers as parameters and returns their sum. function addNumbers($num1, $num2) { $sum = $num1 + $num2; return $sum; } // Call the function with arguments and display the result. $result = addNumbers(5, 10); echo "The sum is: " . $result; ?>

The function is named addNumbers and it takes two parameters: '$num1' and '$num2'. These parameters allow you to provide input values when calling the function. Inside the function body, the two parameters are added together and stored in a new variable, '$sum'. The return statement then sends this value back to the part of the script that called the function.

Note
Note

When you call addNumbers(5, 10), the values 5 and 10 are passed into the function as '$num1' and '$num2'. The function calculates their sum and returns it. The result is then stored in the variable '$result', which is displayed using echo.

Using parameters makes your functions flexible and reusable, since you can pass in different values each time you call the function. The return statement is essential when you want your function to produce a result that can be used elsewhere in your code.

question mark

What is the role of parameters in a PHP function?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2
some-alt