Skip to main content
Inspiring
October 18, 2017
Question

Using PHP strings in PHP includes (novice level)

  • October 18, 2017
  • 1 reply
  • 424 views

As a designer, my PHP knowledge is limited to what I picked up over the years that could help facilitate my process. For me, this means pretty much just using includes and strings.

The index files would therefore look something like this (simplified) :

<?php include ('header.php'); ?>

<?php include ('custom.php'); ?>

<?php include ('footer.php'); ?>

It's not ideal — a more common solution would probably have the url insert the custom content into a single-file wrapper (ex: domain.com/article.php?file=93840923) — but I have no idea how to do that; while this is within my limited realm of understanding.

Now correct me if I'm wrong, but if I want to include strings to define header info that is specific to one page (ex: <title><?php echo $title; ?></title>) then I shouldn't define $title; in the custom.php page, because the server reads the includes in order... correct? The head never gets to read the definitions before it has to render them if they're in the body.

So the only way for me to use PHP includes the way I've been using them, as well as strings to define custom header info, would be for me to take the string definitions out of the custom.php file and put them in a standalone file that would be processed before the header. As so...

<?php include ('custom_strings.php'); ?>

<?php include ('header.php'); ?>

<?php include ('custom.php'); ?>

<?php include ('footer.php'); ?>

...right? The reason I had the string definitions in the custom content file in the first place is that I liked the idea of having all the unique non-image data in a single file. But I'm not against separate custom.php + custom_strings.php files.

Could an actual PHP connoisseur confirm the many assumptions I've made in this post, or correct me where I err?

Thanks!

This topic has been closed for replies.

1 reply

Legend
October 18, 2017

You can declare the variables in a custom_strings.php include or just declare the $title variable for instance in the most relevant php include - in the case above that would be header.php include?

It all comes down to what workflow you find most suitable for yourself. There are no strict rules for this kind of decision making.

You could even put all your page titles in a php array:

$page_title = array('page 1' , 'page 2' , 'page 3'); ?>

Then echo them out for the specific page:

<title><?php echo $page_title[0]; ?></title>

<title><?php echo $page_title[1]; ?></title>

<title><?php echo $page_title[2; ?></title>

or you might want to pass the page title via a url variable:

<a href="page_2.php?page_title=Page 2">Page 2</a>

<a href="page_3.php?page_title=Page 3">Page 3</a>

Then at the top of each page add:

<?php

$page_title = $_GET['page_title'];

?>

Then echo the title out:

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

There are literally dozens of ways to approach this.

Under S.Author
Inspiring
October 18, 2017

osgood_  wrote

(...) or just declare the $title variable for instance in the most relevant php include - in the case above that would be header.php include?

Well, the point of even having a header.php file is so I can put the first half of the wrapper in there. In other words, code that isn't subject to change from page to page -- it's common to all pages, EXCEPT for the page title, desc and keys (to help w/ SEO).

Here is what my tentative "have my cake and eat it too" (includes AND strings) header.php file kinda looks like :

<!doctype html>

<html>

<head>

<meta charset="utf-8">

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

<meta name="description" content="<?php echo $description; ?>" />

<meta name="keywords" content="graphic, portoflio, <?php echo $keywords; ?>" />

<link href="/scripts/styles.css" type="text/css" rel="stylesheet" />

<link href="https://fonts.googleapis.com/css?family=Pathway Gothic One|Oswald|Palanquin" type="text/css" rel="stylesheet" />

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<script src="/scripts/scripts.js" type="text/javascript"></script>

</head>

<body>

And the footer.php file just closes that :

</body>

</html>

Since all the data custom to this particular page will be in custom.php, and that file is sandwiched between the header.php and footer.php files, the only way for those variables to read their definitions before they have to be rendered, is by adding a 4th file called custom_strings.php and including it BEFORE the header in the include order.

You could even put all your page titles in a php array:

$page_title = array('page 1' , 'page 2' , 'page 3'); ?>

Then echo them out for the specific page:

<title><?php echo $page_title[0]; ?></title>

<title><?php echo $page_title[1]; ?></title>

<title><?php echo $page_title[2; ?></title>

Thanks, but how does this help me? I still have to create a definitions file that is separate from the custom.php file, and insert it before the header is read, right? What would be the tactical advantage of using arrays here? Are you suggesting I could get away with a single custom_string.php file (the one inserted before the header) for the entire website, instead of one per page? Or is there more to your suggestion than what my untrained eye is seeing?

or you might want to pass the page title via a url variable:

<a href="page_2.php?page_title=Page 2">Page 2</a>

<a href="page_3.php?page_title=Page 3">Page 3</a>

 

Then at the top of each page add:

<?php

$page_title = $_GET['page_title'];

?>

Then echo the title out:

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

So we can define strings via url in PHP? Very nice to know, but I can't imagine putting something as important as the page title in the url; this would make it too easy for users to modify said url to make the page title anything they want (and as vulgar as they want). But again, nice to know this is doable for future reference.

There are literally dozens of ways to approach this.

Good to know! Do my responses here help narrow what you feel the best solution would be?

Final question... since all the parsing is being done on the server side, does this mean I can use PHP includes up the wazoo with no fear of slowing down the rendering process? Could I use 50 includes per page without affecting the user experience? (I won't, just asking for argument's sake.)

Nancy OShea
Community Expert
Community Expert
October 18, 2017

Every request & action on the server takes resources.  That's a given.  But for what you're doing, the hit in performance would be almost negligible.  That's assuming you're on a fast server that isn't shared by 200+ other websites that get tons of traffic.  

Nancy

Nancy O'Shea— Product User & Community Expert