Highlighted

Using PHP strings in PHP includes (novice level)

Contributor ,
Oct 17, 2017

Copy link to clipboard

Copied

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!

Views

237

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Using PHP strings in PHP includes (novice level)

Contributor ,
Oct 17, 2017

Copy link to clipboard

Copied

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!

Views

238

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 17, 2017 0
LEGEND ,
Oct 18, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 18, 2017 0
Contributor ,
Oct 18, 2017

Copy link to clipboard

Copied

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.)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 18, 2017 0
Adobe Community Professional ,
Oct 18, 2017

Copy link to clipboard

Copied

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, ACP
Alt-Web.com

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 18, 2017 0
osgood_ LATEST
LEGEND ,
Oct 18, 2017

Copy link to clipboard

Copied

https://forums.adobe.com/people/Under+S.  wrote

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>

Reading through your reply this seems inefficient if you are intending to use one custom_strings.php per page to serve the unique title, description and keywords contents. I dont even see how that works effectively because you are changing every file which is not what you want to do, presumably.

You'll just end up with this kind of stiuation below, creating a seperate page for each page to just include the custom_strings.php or custom_strings_1.php, there's no advantage

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

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

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

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

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

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

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

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

A more efficient way would be to store the title, description and keywords information for each page in a database and pass a unqiue page identifier, maybe a page id via a link url so you could query the database.

<a href="products.php?page_id=2">Products</a>

<?php

// get page_id from url link

$page_id = $conn->real_escape_string($_GET['page_id']);

// get page details to display on page

$pageDetails = $conn->query("SELECT * FROM page_details WHERE page_id = '$page_id'") or die($conn->error);

$pageDetails = $pageDetails->fetch_assoc();

?>

<title><?php echo $pageDetails['title']; ?></title>

<meta name="description" content="<?php echo $pageDetails['description']; ?>" />

<meta name="keywords" content="<?php echo $pageDetails['keywords']; ?>" />

or if that is too advanced then at least test to see what page is being viewed and use a php switch statement to select the correct custom_string.php include:

<a href="index.php">Index</a>

<a href="products.php?page_id=1">Products</a>

<a href="outlets.php?page_id=2">Outlets</a>

<?php

$page_id = $_GET['page_id'];

switch ($page_id) {

case 1:

include('custom_string_1.php');

break;

case 2:

include('custom_string_2.php');

break;

case 3:

include('custom_string_3.php');

break;

default:

include('index_page_string.php');

}

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

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

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

?>

https://forums.adobe.com/people/Under+S.  wrote

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.

It's not relevant - why would anyone, but a fool, want to change the name of your page. php url variable parameters are used frequently to determne which bits of information are served to which pages, they are not permanent.

Very often I will only have 1 or 2 template php pages and serve all the information required from a database based on either POST or GET values.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 18, 2017 0