Welcome to WebmasterWorld Guest from 54.227.48.147

Forum Moderators: coopster & jatar k

print code repository script

     
7:30 am on Dec 3, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2215
votes: 7


Hi,

I hope I called it right in the subject...

Sometimes I use PHP to store HTML code. For example, to echo the code for some social buttons. I do it in this way:
<?php
print '<!-- AddThis Buttons BEGIN -->
<div class="addthis_inline_share_toolbox"></div>
<!-- AddThis Buttons END -->';
?>

Then I include this into HTML pages. Yeah, a template does the same, but I may have multiple templates or whatever, so this ensures I do the change in a single place when needed, and no need to upload HTML files, but the tiny PHP code only.

Anyway, in the same fashion I would like to maintain several, call them blurbs, in a single PHP file, and call a certain blurb by using PHP include command. For example:
<?php
$blurb1 = print 'blurb1 text';
$blurb2 = print 'blurb2 text';
$blurb3 = print 'blurb3 text';
... and so on...
?>

Then, in my HTML, I do <?php echo $blurb1 ?>

Now, what would be the simplest way of doing this?

Is it that I include the whole script in the beginning of the HTML, and then call echo on 1, 2, or 3, or I just call each blurb directly however and whenever I want?

For example, how do I call "echo $blurb2" directly in one line?

Thank you
11:31 am on Dec 3, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member topr8 is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 19, 2002
posts:3356
votes: 39


>>For example, how do I call "echo $blurb2" directly in one line?

you don't need to use
print
just set the text string to a variable:


$blurb1 = 'blurb1 text';
$blurb2 = 'blurb2 text';
$blurb3 = 'blurb3 text';


and then echo the variable to the page.

I don't know what is best practice but i do something similar to you, i have a file called something like website-text-snippets.php
in that file i have a bunch of text strings set to variables, i include this file at the very top of every page (kind of anyway, i don't actually have individual pages as such) and then i can insert the text snippet wherever i want by echo-ing the appropriate variable