Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Declaring Functions and Using Parameters | Functions in PHP
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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookDeclaring Functions and Using Parameters

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2
some-alt