Printing "PHP and HTML"

Hi I am on lesson “PHP and HTML” exercise 5 “Beyond String”. I am trying to print age by using a provided function called calculateAge()

function calculateAge ($person_arr){
  $current_year = date("Y");
  $age = $current_year - $person_arr["birth_year"];
  return $age;

and by passing in an array as the argument,also provided in the exercise

$about_me = [
  "name" => "Aisle Nevertell",
  "birth_year" => 1902,
  "favorite_food" => "pizza"

I’m trying to go from PHP to HTML so I tried doing it in one line like this:

echo "<p>You are calculateAge($about_me)  years old </p>";

This doesn’t work for me and I get errors. Am I not suppose to do it this way? This is what I get:

You are calculateAge(Array) years old

I’ve tried other ways like containing that function call with more parentheses, or trying to concatenate with .'s but that still did not work.
What ended up working for me is taking the result of calculateAge($about_me) and setting it to a variable($result_age)
Then using that variable in my echo, so…

$result_age= calculateAge($about_me);
echo "<p>You are $result_age  years old </p>";

I just wanted to know if there was anyway to do this in one line like I was trying to do, do I just have the syntax wrong?


there is, you just need to concat the string with the function call:

echo "<p>You are " . calculateAge($about_me) . " years old </p>";

i would prefer string literal, but PHP seems only to allow simple variables when using string literals, not function calls


Thank you so much! This worked for me