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 6096555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:56:27+00:00 2026-05-23T12:56:27+00:00

In my code, I use a public load_snippet function of a class when I

  • 0

In my code, I use a public load_snippet function of a class when I need to include HTML or PHP snippets. (I do this instead of a direct include_once because the directory structure varies depending on certain variables).

I had some issues with variable scopes, so I’ve narrowed down the problem to this: let’s say I define a variable within my page:

$variable = 'Hello World!";

Then, I need load in a snippet:

$APP->load_snippet("slider");

The snippet renders perfectly, except that PHP gives an undefined variable error if I try to reference $variable in the slider code. If I directly include the php file, it works as expected, so I don’t understand why I’m having this problem, since this is the load_snippet function:

public function load_snippet($snippet){  
if(file_exists("app/".$this->APP_TYPE."/snippets/".$snippet.".php")){  
        include "app/".$this->APP_TYPE."/snippets/".$snippet.".php";  
    }  
    else{  
        include 'common/txt/404.txt';  
    }  
}

Any help you can give me is much appreciated.

  • 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-05-23T12:56:27+00:00Added an answer on May 23, 2026 at 12:56 pm

    The file is being included within the context of the load_snippet() function, and therefore has only those variables which exist within that function. One way to modify this is to make your function accept two variables: the filename and an array of values.

    public function load_snippet($snippet, $content) {  
        if (is_array($content)) extract($content);
        if (file_exists("app/".$this->APP_TYPE."/snippets/".$snippet.".php")) {  
            include "app/".$this->APP_TYPE."/snippets/".$snippet.".php";  
        } else {  
            include 'common/txt/404.txt';  
        }  
    }
    

    Then

    $arr = array('variable' => 'Hello world!');
    load_snippet('slider', $arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need process concurent requests at my application. I use this code: public class
I wanted to use code like this in my Zend Framework project: public function
how exactly I can use public methods (non-virtual) with NHibernate? I have this code:
When using XML serialization in C#, I use code like this: public MyObject LoadData()
I wanted to list all my Recently Used Items. I use this code: public
i use this code for html file but it not working how to solve
In c# when I use the code : public System.IAsyncResult BeginConnect(System.Net.EndPoint remoteEP, System.AsyncCallback callback,
This is the code I use to setup my TCP server internal void Initialize(int
Doctrine documentation says you can use public function construct() { ... } as a
I use this code to load data and convert them to a type that

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.