-3

I'm not stuck on anything but I have the following code and want to know froma theoretical perspective why echo is required with the_permalink(); and the_title(); in the PHP script below

<div class="col-md-9">

    <a href="<?php echo the_permalink(); ?>"

        <h2><?php echo the_title(); ?></h2>

    <a/>

I'm still a bit new to PHP and building in Wordpress so that could have something to do with my confusion here but just for general knowledge, I would like to know.

logos_164
  • 736
  • 1
  • 13
  • 31
  • `echo` isn't actually required for these (see the code samples in https://codex.wordpress.org/Function_Reference/the_permalink, for example). It's required for `get_permalink()` and `get_the_title()`, but `the_permalink()` and `the_title()` actually do their own `echo` calls internally. WordPress is weird - some functions output, others don't, some are `get_the`, others are `get_`, etc. It's a product of long, messy evolution and the developers like to keep everything backwards-compatible, so a lot of very ancient design mistakes persist in the modern version. – ceejayoz Dec 13 '17 at 00:34

1 Answers1

0

As stated above, the echo command prints the text to the screen. There are some functions in WordPress, and PHP, where you want to get some data but not print it to the screen just yet.

If you were using a statement like:

$title = the_title();

<h1><?php echo the_title();?></h1>

You would have the title displayed twice, as the_title() echoes by default.

If you wanted to get the title and use it somewhere else. then the_title() has a parameter you can set to false.

$title = the_title('<h1>','</h1>', false);

<a href="#"><?php echo $title; ?></a>
  • 1
    @ceejayoz actually it will display the title twice as demonstrated here using `the_title()` function from WordPress: https://3v4l.org/PS3Es – naththedeveloper Dec 13 '17 at 11:51