Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:06:32+00:00 2026-06-09T04:06:32+00:00

So I’ve seen some controversy on passing variables from file to file with PHP.

  • 0

So I’ve seen some controversy on passing variables from file to file with PHP. What is the “best way” to do it?

Currently, I’ve got a setup like this

FILE 1:

<? $foo = 'somethingCoolHere';
$bar = 'nothingLameThere'; ?>

FILE A:

<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff'); ?>

FILE B:

<? include('/link/to/file1.php');
printf('stuff' .$foo. 'stuff');
printf('stuff' .$bar. 'stuff'); ?>

This works, ‘alright’. But if someone wants to expand on what I’ve got, they may need to remember to include the “include” on the top line to get the variables to work. Is there a “better practice” or “best practice” way vs what I’m doing now?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:06:34+00:00Added an answer on June 9, 2026 at 4:06 am

    Personally (not sure if others will agree), I would use absolute paths when including files. And I would have those paths sit in constants. Why? Because if I ever needed to change a path (say I moved a file or something) I would not have to go through every file to change the path, but instead, just the file where the constants were defined.

    An example:

    constant.php

    define('file1' , 'abolute/path/to/file1');
    define('fileA' , 'abolute/path/to/fileA');
    define('fileB' , 'abolute/path/to/fileB');
    

    Then include constant.php in any file you need to use the constants:

    something.php:

    include_once($_SERVER['DOCUMENT_ROOT'].'/constant.php');//we now have access to our constants
    include_once(fileA);
    include_once(fileB);
    include_once(file1);
    

    I’m not saying that method is the best practice, but it would just be something I would do in just about all my projects. And I can’t see anything wrong with it either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.