champer
champer

Reputation: 21

Echo PHP inside HTML tag

I want to echo this title variable inside the h1 tag, but I get an error message. Can you please tell me what I'm doing wrong?

<?php 
  $title = get_field ('page_title');
  if ($title):
  <h1> echo ($title); </h1> 
  endif;
?>

Upvotes: 0

Views: 95

Answers (3)

champer
champer

Reputation: 21

I changed the code as per the suggestion, and it is working. I hope it is correct.

<?php
$title = get_field('page_title');
if ($title) :
  echo "<h1> $title </h1>";
endif;
?>  

<?php
$input = get_field('input');
if ($input) :
  echo " <a href= 'mailto: ($input)' > $input </a> ";
endif;
?>
    

Upvotes: 1

il4mb
il4mb

Reputation: 125

This you code with correcting :

<?php 
    $title = get_field ('page_title');
    if (isset($title)){
    echo'<h1>'.$title.'</h1>'; 
    }
    end_if;
   

Upvotes: 2

This line is wrong:

   <h1> echo ($title); </h1> 

PHP won't understand the HTML tags so it complains about them.

There are a couple of ways you might do this, but I'd suggest replacing the entire line with

        echo "<h1>$title</h1>";

PHP will insert the current value of $title in the string and echo the result.

Reference: PHP double-quoted Strings

Upvotes: 2

Related Questions