Difference between return, echo and print keyword in PHP

What is the difference between the three?

return, echo and print keyword in PHP

function theBand($abc,$bac) {

return $abc;
echo $abc;

}

Both do the same thing, it shows or returns to me the value contained in the variable abc. Now the function returns and the echo continues. In addition, there is something specific with the return keyword.

+6
source share
3 answers

return is a language construct used to exit a function and provide the value of the calling function.

echoand printboth language constructs that print strings. The main difference is that an echo can take multiple arguments, separated by commas, but print accepts only one argument.

+17
source

return , .

.

echo print , echo , .

  • . , , . echo , , nitty gritty.

  • . print() , :

    $ret = print "Hello World";
    

    $ret 1. , print , echo . PHP:

    $b ? print "true" : print "false";
    

    print , , . . ",", AND, OR XOR .

  • (). : echo expression [, expression[, expression] ... ]. echo ( expression, expression ) . : echo ("howdy"),("partner"); , : echo "howdy","partner"; ( , .)

, echo , :

echo  "and a ", 1, 2, 3;   // comma-separated without parentheses
echo ("and a 123");        // just one parameter with parentheses

print() :

print ("and a 123");
print  "and a 123";
+4

print 1, . , .

return (in the context of a function) exits the function (Returning a value, if one is provided). In a global context, returning will stop execution of any file in which it is located. This way you can get rid of the included file or stop the main script from doing this.

0
source

All Articles