Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Stein Christensen
Stein Christensen
3,592 Points

Storing A function return as a variable

I am trying to create a if then statement with a function: '''php <?php $thetitle = the_title();

if ($thetitle == "Our Clients") { echo "success!!" } ?> ''' However, when ever I write the function the_title() it just prints the return value of "Our Clients" to the screen.

How can I set the return value of a function as a variable?

2 Answers

Hello,

It all comes down to how the function the_title() uses the value in its function. If I am correct, the_title() doesn't return the title of the page but echoes it out. You can only store a function's returned value if that value gets returned by the function. I am guessing this is a WordPress function and you can simply Google that function and it should allow you to view the function's code either on WordPress or on another site that displays the open-source code for WordPress. Again, you can only store a function's returned value if the function actually returns it. I believe the function the_title() echoes out the title not return it. Hope this helps.

Cheers!

Michael Collins
Michael Collins
433 Points

How about this ...

<?php

function the_title($title) {

  if ($title == "Our Clients") {
    return TRUE;
   } else {
    return FALSE;
   }

}

if (the_title("Our Clients")) {

 print "success!!";

} else {

 print "the title is not 'Our Clients'";

}

if (the_title("Our Cats")) {

 print "success!!";

} else {

 print "the title is not 'Our Clients'";

}



?>