Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Defining and Calling Functions | Getting Started with PHP Functions
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
PHP Functions

bookDefining and Calling Functions

How to Define a Function

To define a function in PHP, use the function keyword, followed by the function name and a pair of parentheses. The code you want the function to run goes inside curly braces. Here’s the basic structure:

function functionName() {
    // Code to run when the function is called
}

Example:

function sayHello() {
    echo "Hello, world!";
}

This defines a function named sayHello that prints "Hello, world!" when it is called.

How to Call a Function

To use your function, write its name followed by parentheses:

sayHello();

This line runs the code inside the sayHello function, so you will see Hello, world! printed on the screen.

Key Points

  • Always start a function definition with the function keyword;
  • Function names should be descriptive and use only letters, numbers, and underscores;
  • Don't forget the parentheses, even if your function does not take any input;
  • Call a function by writing its name followed by parentheses.

By defining and calling functions, you can make your PHP programs more organized and efficient.

functions_example.php

functions_example.php

copy
123456789101112131415
<?php // Define a function named 'addNumbers' that takes two arguments function addNumbers($a, $b) { // Calculate the sum of the two arguments $sum = $a + $b; // Return the result to the caller return $sum; } // Call the 'addNumbers' function with 5 and 7 as arguments $result = addNumbers(5, 7); // Print the returned value to the screen echo "The sum of 5 and 7 is: " . $result;
question mark

Which of the following is the correct way to define a simple function named greet in PHP?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookDefining and Calling Functions

Свайпніть щоб показати меню

How to Define a Function

To define a function in PHP, use the function keyword, followed by the function name and a pair of parentheses. The code you want the function to run goes inside curly braces. Here’s the basic structure:

function functionName() {
    // Code to run when the function is called
}

Example:

function sayHello() {
    echo "Hello, world!";
}

This defines a function named sayHello that prints "Hello, world!" when it is called.

How to Call a Function

To use your function, write its name followed by parentheses:

sayHello();

This line runs the code inside the sayHello function, so you will see Hello, world! printed on the screen.

Key Points

  • Always start a function definition with the function keyword;
  • Function names should be descriptive and use only letters, numbers, and underscores;
  • Don't forget the parentheses, even if your function does not take any input;
  • Call a function by writing its name followed by parentheses.

By defining and calling functions, you can make your PHP programs more organized and efficient.

functions_example.php

functions_example.php

copy
123456789101112131415
<?php // Define a function named 'addNumbers' that takes two arguments function addNumbers($a, $b) { // Calculate the sum of the two arguments $sum = $a + $b; // Return the result to the caller return $sum; } // Call the 'addNumbers' function with 5 and 7 as arguments $result = addNumbers(5, 7); // Print the returned value to the screen echo "The sum of 5 and 7 is: " . $result;
question mark

Which of the following is the correct way to define a simple function named greet in PHP?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
some-alt