-1

I am attempting to dynamically grab a page's title and URL and echo into HTML. Here's what I have:

<ul>
    <li><a href="mailto:?Subject=<?php echo $page_title?>&body=<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>" class="email" title="Email">Email</a>
    </li>
</ul>

The URL successfully echos, but I'm not sure if this is the correct way? And I can not figure out how to print the page's title via <?php echo $page_title?>?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Yahreen
  • 1,639
  • 7
  • 24
  • 38

2 Answers2

1

There is no global PHP variable which corresponds to the page <title>. Unless you earlier used the $page_title variable to specify the <title>...

<?php $page_title = "My Page Title"; ?>
<title><?php echo $page_title ?></title>

...<?php echo $page_title?> wont' do anything special.

If you did use the $page_title variable in this way, you might need to encode the data. It should be both URI encoded and HTML encoded - in that order.

<a href="mailto:?Subject=<?php echo htmlentities(urlencode($page_title)); ?>" class="email" title="Email">Email</a>
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
0

$page_title is just a variable. If you define it, then it will echo. You might want to have <title><?php echo $page_title?></title> in your <head>, again assuming you actually define that variable.

PHP isn't some magical force, it is a programming language and as such it does what you tell it to do.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592