PHP Defining Function Logical -
i starting out learning php, , wondering can me out logical of defining functions. can't seem wrap head around it.
simple example:
<?php function hello($word) { echo "hello $word"; } $name = "john"; strtolower(hello($name)) ?> i know if use return instead of echo in function , echo outside defining function, "strtolower" applies, in example doesn't. don't how php interpreting it. in advance.
imagine function box of unknown content. put in , comes out - meaning have parameters , returned.
in code example don't return anything, instead echo string.
function hello($word) { // ^ parameter return "hello $word"; } $name = "john"; strtolower(hello($name)); to explain bit further, can @ original code way:
echo strtolower(hello("john")); ^ ^--- call hello("john") | happens (your echo) | hello() ended without return, return null default |--- call strtolower( null ) happens strtolower() returned "" but want this:
echo strtolower(hello("john")); ^ ^--- call hello("john") | happens | hello() return "hello john" |--- call strtolower("hello john") happens strtolower() returned "hello john"
Comments
Post a Comment