Tags
Asked 2 years ago
17 Jun 2021
Views 216
Lesly

Lesly posted

how to return string by Function in PHP

how to return string by Function in PHP
Mitul Dabhi

Mitul Dabhi
answered Apr 27 '23 00:00

To return a string from a function in PHP, you can use the return statement followed by the string you want to return. Here's an example:


f

unction getGreeting() {
    $name = "John";
    return "Hello, " . $name . "!";
}

$greeting = getGreeting();
echo $greeting; // Output: Hello, John!

In this example, we define a function called getGreeting () that returns a string. Inside the function, we define a variable $ name and set it to "John". We then use the return statement to return a string that includes the variable $name.

We call the getGreeting () function and store the returned string in a variable $greeting . Finally, we use echo to output the string "Hello, John!" to the screen.

Note that you can also pass parameters to a function to customize the string that is returned, and you can concatenate multiple strings using the . operator.
Post Answer