0

say i have a main file :

        <form id="form1" name="form1" action="" method="post">
        <input title="Masukkan kalimat disini" type="text"  />
            <?php   
           //main.php 
            $a = ...;
            $b = ....;
    ?> 

and included file

  <?php
//included.php
    include 'main.php';
    ?>

certainly included.php file will load all parts of main.php, so how to include some variable, such as include $a only, and not showing the textfield

Thanks.

3 Answers 3

1

If you want to include just a variable (or set of variables), they need to be in their own file. Ideally, you would also put them in a different scope (such as a class), so they're not cluttering up the global namespace.

E.g.,

config.php:

<?php
    class Foo {
        public $a = "I am a variable";
        public $b = "I am also a variable";
        const $c = "I am an immutable variable!";
    }
?>

main.php:

<form id="form1" name="form1" action="" method="post">
<input title="Masukkan kalimat disini" type="text" />
<?php
    @require_once "config.php";
?>

included.php:

<?php
    @require_once "config.php";
?>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Hope that was clear and helpfull :) Any help needed i am just ready :)

1 Comment

@user2192076 : For nothing ! But please if you find my answer is the helpfull answer mark it as the best answer and mark your question as answered :) And don't hesitate to ask for any help i am just here :)
0

Providing you are including the page, you will beable to use your set variables which are set on a different page (if that makes sense)

Global.inc.php

<?php
   $Var_1 = "This will work"; 
   $Var_2 = "Another set variable.";
?>

index.php

<?php
   include "Global.inc.php";
   echo $var_1; // This will output 'This will work'
?>

As you have included a file, all the contents will be usable; You just have to work with the variables that you want. There will be no performance hits for including a 300 line page, to use less than 50 lines of code for example.

Update:

Show within HTML Structures:

Inputs:

<input type='text' name='Name' value='<?php echo $Var_1; ?>'>

Text Box:

<textarea><?php echo $Var_1; ?></textarea>

1 Comment

I have updated my answer to show an example on how to use these within HTML Structures, if this is not what your looking for then let me know

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.