0

Can some one tell me how to "include" a variable from another .php file without all its other content.

index.php

<?php
$info=file('somedir/somefile.php');
$v1=trim($info[2]);
$v2=trim($info[3]);
$v3=trim($info[4]);
?>

the somedir/somefile.php

<?php
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
All the other content there may not be runned or showed.
?>

Can anybody please help me??

Edit:

Its for my dynamic page.

<html>
    <?php
    include_once 'config.php';
    include_once 'includes/mysqlconnect.php';
    $url_slash=$_SERVER['REQUEST_URI'];
    $url= rtrim($url_slash, '/');
    //$url = basename($url);
    $info=file('sites/'.$url.'.php');
    $title=trim($info[2]);
    ?>
    <head>
        <meta charset="UTF-8">
        <title>$title</title>
        <link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/reset.css">
        <link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/<?php echo $theme;?>.css">
    </head>
    <body class="body">
        <div class="container-all">
            <?php include_once 'includes/header.php';?>
            <div class="container">
                <?php include_once 'includes/navigationbar.php';?>
                <?php include_once 'includes/rightsidebar.php';?>
                <div class="content"><?php

                if ($url==''){

                    include_once "sites/home.php";
                }
                elseif (file_exists("sites/$url.php") && is_readable('/var/www/html/sites/'.$url.'.php')){
                    include_once '/var/www/html/sites/'.$url.'.php';

                }

                else {
                    include_once 'sites/404.php';

                }


                ?></div>
                <?php include_once 'includes/footer.php';?>
            </div>
        </div>
    </body>
</html>

Hope you understand my question now.

4
  • save in session and use it on other page. Commented Mar 21, 2014 at 10:11
  • does somefile.php have to be PHP, or is it just a file containing data? Commented Mar 21, 2014 at 10:13
  • Depends on your needs, you can use a superglobals ($_GET, $_POST or $_SESSION). Commented Mar 21, 2014 at 10:15
  • What I need is to get some content from ONLY one line of antoher file and turn it into a variable Commented Mar 21, 2014 at 10:23

3 Answers 3

3

Programming is just driving your thoughts :)

So what i want to say that your question is how you can include just some part of an included file and my answer is that you can achieve that by doing a test each time the main file is included from withing this file to see if the file is included internally or not and you can be more precise in a way that you split your main file into block which are loaded due suitable variable

Take a look for this workaround and hope you will understand what i mean

Supposing we have the main file named main.php contains that contents

<?php
     echo 'I am a java programmer';
     echo 'I know also PHP very well';
     echo 'When the jquery is my preferred toast !';
?>

now i have three external files that will include that file each file is specific for one of this 3 programming language

So i will create my 3 files in this way :

File : java.php

<?php
    $iamjavadevelopper = 1;
    include_once("main.php");
?>

File : phpfav.php

<?php
    $iamphpdevelopper = 1;
    include_once("main.php");
?>

File : jquery.php

<?php
    $iamjquerydevelopper = 1;
    include_once("main.php");
?>

and my main.php will be coded in this way

<?php
    if(isset($iamjavadevelopper))
        echo 'I am a java programmer';
    if(isset($iamphpdevelopper))
        echo 'I know also PHP very well';
    if(isset($iamjquerydevelopper))
     echo 'When the jquery is my preferred toast !';
?>

By this way each one of our three external files will show just a part of the included file :)

Sign up to request clarification or add additional context in comments.

Comments

1

The only way I can think of without cookies or session's is to make an if condition in the page.

like that: index.php

<?php include('somedir/somefile.php');?>

the somedir/somefile.php

<?php
if ($pageName != 'somefile.php')    {
    $variable=something;
    $variable2=someotherting;
    $variable3=thirdone!;
}   else    {
    // All the other content
}
?>

Comments

0

Save the variables in a separate file that can be included separately. Do it the sane way. Structure your code properly, don't try to invent solutions for problems you have because your structure is messy.

Comments

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.