To Echo Or Not To Echo Functions Study

function doubleMe($x){
  echo $x * 2;
}
doubleMe(4);

If we did this function with a return, we would have to write it like this;

function doubleMe($x){
  return $x * 2;
}
echo doubleMe(4);

More Interesting:

function doubleMe($x){
  return $x * 2;
}
$magicalNumber = doubleMe(8);
echo $magicalNumber;

And In An IF Statement

function doubleMe($x){
  return $x * 2;
}
if (doubleMe(12) == 24){
  echo "The function is performing the math correctly.";
}

And More Functions Inside Functions:

function doubleMe($x){
  return $x * 2;
}
function tripleMe($x){
  return $x * 3;
}
echo tripleMe(doubleMe(5));

Rule Of Thumb In WP

the_title(); will output echo
get_the_title(); will return data

the_ID(); will output echo
get_the_id(); will return data
Scroll to Top