Reputation: 43
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.
Upvotes: 0
Views: 3649
Reputation: 9875
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";
?>
Upvotes: 1
Reputation: 530
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 :)
Upvotes: 0
Reputation: 5524
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>
Upvotes: 0