John
John

Reputation: 49

can I achieve this simple thing in PHP?

Basically, I want whatever is inside the h1 tag to duplicate to the h2 tag. So look at the example below.

<!DOCTYPE html>
<html>
<body>

<h1>whatever is inside here, needs to also be inside the H2 tag.</h1>

<h2>whatever is inside h1 tag, it should also be here</h2>

</body>
</html>

In my website, I have around five different heading tags that I need to copy-paste manually to each one so I want to do this just once and have them all be the same.

Upvotes: 1

Views: 131

Answers (4)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

"Thank you Fred. Your answer worked. – John"

As per a comment I left in comments and with an added explanation:

Echo an assigned variable that you can pass/echo inside the <h2> tags:

echo $var="<h1>whatever is inside here, needs to also be inside the H2 tag.</h1>";

<h2>Text<?php echo $var; ?> more text</h2>

Upvotes: 1

kazinix
kazinix

Reputation: 30113

Just create a variable and assign the value you want to use on your headings:

<?php $title = "Stackoverflow!"; ?>
<!DOCTYPE html>
<html>
<body>

<h1><?php echo $title; ?></h1>

<h2><?php echo $title; ?></h2>

</body>
</html>

or if you want to use it to many pages, create a separate file that contains your configurations, include it where you need it.

config.php

<?php $title = "Stackoverflow!"; ?>

page1.php

<?php include("config.php") ?>
<!DOCTYPE html>
<html>
<body>

<h1><?php echo $title; ?></h1>

<h2><?php echo $title; ?></h2>
<p>This is page 1!</p>
</body>
</html>

page2.php

<?php include("config.php") ?>
<!DOCTYPE html>
<html>
<body>

<h1><?php echo $title; ?></h1>

<h2><?php echo $title; ?></h2>
<p>This is page 2!</p>
</body>
</html>

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38609

What about this

<?php

    $h1 = "i'm Heading 01";
    $h2 = "i'm Heading 02";

?>

In HTML

<!DOCTYPE html>
<html>
    <body>

        <!-- <h1>whatever is inside here, needs to also be inside the H2 tag.</h1> -->
        <h1><?php echo $h1 ?></h1>

        <!-- <h2>whatever is inside h1 tag, it should also be here</h2> -->
        <h2><?php echo $h2 ?>, <?php echo $h1 ?></h2>

    </body>
</html>

Upvotes: 0

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

There are 2 methods you achieve with. One is as suggested by @Fred ii one more is,

you have to bind your h2 tags with h1 with some class.

like this,

$("h2.def").html($("h1.abc").html());
$("h2.xyz").html($("h1.second").html());

Working demo

Make sure you try what is suggested by @Fred ii.

Upvotes: 0

Related Questions