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.