1

My PhP files contain some long string constants and I'm trying to factor them out. So I created "my_string_constants.php" and used include in header.php. So far that works fine.

Now another file, page.php also requires the string constants and header.php. The scheme below tries to clarify these dependendies.

enter image description here

The string constants now seem available in my header only, not the rest of my page. I tried to resolve this by adding global ... to each string constant in string_constants.php. This resolved the error of "Unknown variable" but my string constants still seem unavailable to most of the page content.

What's the right way to get this working?

UPDATE

The issue's been solved. I should have used define(myString instead of $myString = .... By doing so, I need just one include in header.php and the constants will be available to page.php as well.

Thanks a million, you guys are great.

16
  • 2
    if page.php needs header and constants, but header already has constants... then page.php only needs header Commented Dec 28, 2016 at 19:03
  • 1
    So they are string variables, not string constants. Constants can't be changed and don't need to be declared global. Commented Dec 28, 2016 at 19:12
  • 1
    @RubenGeert correct, that is not a true constant. Using true constants should resolve the issue. Commented Dec 28, 2016 at 19:28
  • 1
    @RubenGeert, if you can do $someString = 'blah...'; $someString = 'something else'; then it isn't a constant in any language. Commented Dec 28, 2016 at 19:29
  • 1
    You are doing this the wrong way if this is WordPress. Is page.php part of your theme? If so, define your constants in your theme's functions.php file, and you are DONE. You do not have to include header, bootstrap, etc - that's all handled automatically by WordPress (I've added the WP tag to your question, as that's a VERY important detail). Commented Dec 28, 2016 at 19:38

3 Answers 3

3

One thing you would want to do is distinguish a constant from a variable. Especially if other developers end up working on this, they will be confused by your terminology.

For constants, you do not need to declare them as global and they are defined like so:

define('MY_CONSTANT', 'Value');

It seems to me that the constants file is acting as your site wide configuration file, so to me, it makes sense to have that on every page, regardless of whether header is used or not. I would normally create a bootstrap file for this purpose.

bootstrap.php:

<?php
require_once(__DIR__ . '/constants.php');
require_once(__DIR__ . '/database.php');
require_once(__DIR__ . '/session.php');

You get the point, then every accessible page needs to include this bootstrap file and then possibly the header.

page.php:

<?php
require_once(__DIR__ . '/bootstrap/bootstrap.php');
require_once(__DIR__ . '/header.php');
?>
<h1>Page Title</h1>

In header.php, since it requires these constants, you can handle this in two ways, either check that the constants are defined (meaning, bootstrap was included first) or just use another require_once to make sure that file was loaded.

if (!defined('MY_CONSTANT')) exit('Bootstrap failure');

or

require_once(__DIR__ . '/bootstrap/constants.php');
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I need to consult the manual on this for a couple of minutes.
it appears from a comment that he's also using wordpress... if that makes any difference to how things are already organized.
@mister martin: Yes, it's Wordpress indeed.
IF this in fact a WordPress question, then all of this is irrelevant. OP needs to define his constants in his theme's functions.php file, and does NOT need to do the include(s), etc per this (and other) answers.
1

Going to many directories or "files" deep regarding includes can really cause issues later when you are trying to debug. As a rule I try to only go one level deep in regards to including files. I.e. Create a folder called includes and place everything in there. If there is a file that needs multiple variables, functions etc, then include them in the needed pages at that point doing several includes like so:

<?php 
include("includes/header.php"); 
includes("includes/functions.php");
?>

There are also other issues in regards to having multiple includes, like if you have sessions or cookies some LAMP stacks will require you to declare

session_start();

at the top of every page including all included php files that may need access to that session or cookie.

So to answer your question I believe the simplest solution would be to re-organize your site or script.

Comments

0

in the header page ontop u write
include 'header.php';

and in header.php you write include 'my_string_constants.php';

so in this case the page.php calls the header and in the header the my_string_constants is being called...is this what you mean?

1 Comment

Is this an answer? It doesn't feel like one, it feels like a comment....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.