I want to echo block of code which is dynamically generated. For example:
<?php
$cid = $camp_id;
$hostname = "$host";
$db_user = "$dbuser";
$db_pass = "$dbpass";
$db_name = "$dbname";
$mysqli = new mysqli();
$mysqli->connect($hostname, $db_user, $db_pass, $db_name);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
etc....
?>
I got access to $camp_id and other variables because they are in the file which is included.
I tried to store this code in variable with < pre> and < code> tag and echo after that but couldn't make it work.
Also how can I insert $camp_id to this. Below is example what I think (I know it's not correct just for understanding.
$generated_code = "<.code><?php $cid = <?php echo $camp_id;?> $hostname = $host; etc... </code > ?>";
I used space and dot before code and pre because if not it doesn't show as tag..
Thanks
You could also try like this:
<?php
ob_start();
?>
<code>$cid = <?php echo $camp_id; ?> , $hostname = <?php echo $host; ?></code>
<?php
echo ob_get_clean();
?>
Depending on the circumstances and what your code is like, using ob_start() and ob_get_clean() functions allow your code to be more legible in color coded IDEs, since your output wont look like one solid block of color, instead it will be styled like it should in html for better readability.
You need to follow the rules for strings in PHP, and next to that you need to follow the rules for HTML, or better, output plain text:
<?php
header('Content-Type: text/plain;');
echo '<?php
$cid = ' . $camp_id .';
etc....
?>';
Using EOF inside single quote have better result. But No space after 'EOF'
<?php
$head= <<<'EOF'
<?php $var=2; ?>
EOF;
?>
Related
sorry for my last question where i try put some live code with ob_start buffer content is not helping me to solve my problem because buffer content just collects output text, it doesn't execute any code. thanks #akrys for your advices
what i want is to put code into while looping like this
$sql = $conn->query("SELECT * FROM `users`");
$var = $row['full_name'];
include('test.php');
after i call test.php contain while code like:
while($row = $sql->fetch_array()) {
echo $var;
}
everything is work if i replace $var with $row['full_name'];
but i get the name of row field from some script on index.php so i should access that file first then i call portable file contain query to fetch_array on test.php
how to make it work when i put it back with $var contain variable field name
thank you very much for your attention guys
you should to include before your code
page
test.php
<?php
$someVariable = 'hello'; // the variable only can access in here
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable $someVariable";
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
of course you can use define too
page test.php
<?php
define( "SOMEVARIABLE", hello );
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable ".SOMEVARIABLE;
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
you can use:
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
for more help, use the link below:
enter link description here
first of all this is my php code. i am trying to get this code as text on output. not sure how i do that. with html we can do \" to insert it inside php but how do i get this done like that on my code ?
<?php
$stringData = "
// Start here
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo 'Hello';
?>
// End here
";
?>
i have marked that part that i want to get it on output as text but when i put it like that on page i get syntax error not sure why.
EDIT #2
here below is my full code and i explain how my code works and for what.
my code is to create page and put something inside that page created
<form method="post">
<label>Page Name:</label><br>
<input type='text' name='filename' placeholder='page name'>
<label>Folders</label>
<select name="thisfolder">
<option value="">Default</option>
<option value="Folder1">Folder1</option>
<option value="Folder2">Folder2</option>
<option value="Folder3">Folder3</option>
</select><br><br>
<label>content</label><br>
<input type='text' name='strin' placeholder='content of created page'>
<input type='submit' value='Add Feed'>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// the name of the file to create
$filename=$_POST['filename'];
// the name of the file to be in page created
$strin=$_POST['strin'];
// the name of the folder to put $filename in
$thisFolder = $_POST['thisfolder'];
// make sure #thisFolder of actually a folder
if (!is_dir(__DIR__.'/'.$thisFolder)) {
// if not, we need to make a new folder
mkdir(__DIR__.'/'.$thisFolder);
}
// . . . /[folder name]/page[file name].php
$myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php";
$fh = fopen($myFile, 'w');
$stringData = "
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo '';
?>
";
fwrite($fh, $stringData);
fclose($fh);
}
?>
what i am trying to do is, passing that php code that is inside $stringData to that page that will be created
You'll want to escape the $'s in your text.
Set $stringData like this:
$stringData = "
// Start here
<?php
\$width = \$_GET['width'];
\$heigh = \$_GET['height'];
echo 'Hello';
?>
// End here
";
using highlight_string internal function
echo highlight_string($stringData);
or using htmlspecialchars
echo htmlspecialchars($stringData);
EDIT , as long as you don't want to print the php code literally to the output [as you've mentioned in your comment]
the problem here is that you are using (double quotes) to store values, which has special meaning in php
the solution is to store your text in single quotes ,
<?php
$stringData = '
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
';
?>
You're using double quotes (") which lets you use $variables inside the string where single quotes (') will not.
like so:
$color = 'red';
$string_one = "My car is $color."
echo $string_one; // My car is red.
$string_two = 'My car is $color.'
echo $string_two; // My car is $color.
So to fix your code you simply need to change the double quotes to single quotes (and escape [put a backslash before] the other single quotes).
Like so:
<?php
$stringData = '
// Start here
<?php
$width = \$_GET[\'width\'];
$heigh = \$_GET[\'height\'];
echo \'Hello\';
?>
// End here
';
?>
In your code I added:
<?php
$stringData = '
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
';
echo $stringData;
?>
When I opened this phap page, I had in browser:
// Start here // End here
In Page Source View I had:
// Start here
<?php
$width = $_GET["width"];
$heigh = $_GET["height"];
echo "Hello";
?>
// End here
There is no error! I see now what you wont.
This code with "(string) $price" working:
<?php
$price = 10;
$stringData = "start here (string) $price end here";
echo $stringData;
echo "(string) $price";
?>
Your code
?>
// End here must be in out put
";
?>
There is not start php delimiter <?php
Correct is:
?>
// End here must be in out put
<?php
";
?>
You missed one more php delimiter, total 2:
<?php
$stringData = "
**?>**
// Start here must be in out put
<?php
$width = $_GET['width'];
$heigh = $_GET['height'];
echo '';
?>
// End here must be in out put
**<?php**
";
?>
I am working on a script with templates. So I have this PHP code:
<?php
$string = "TEST";
echo(file_get_contents('themes/default/test.html'));
?>
And I have this HTML (the test.html file):
<html>
<p>{$string}</p>
</html>
How can I make PHP actually display the variable inside the curly brackets? At the moment it displays {$string}.
P.S:
The string might also be an object with many many variables, and I will display them like that: {$object->variable}.
P.S 2: The HTML must stay as it is. This works:
$string = "I'm working!"
echo("The string is {$string}");
I need to use the same principle to display the value.
You can use the following code to achieve the desired result:
<?php
$string = "TEST";
$doc = file_get_contents('themes/default/test.html'));
echo preg_replace('/\{([A-Z]+)\}/', "$$1", $doc);
?>
P.S. Please note that it will assume that every string wrapped in { }
has a variable defined. So No error checking is implemented in the code above. furthermore it assumes that all variables have only alpha characters.
If it is possible to save your replacees in an array instead of normal variables you could use code below. I'm using it with a similar use case.
function loadFile($path) {
$vars = array();
$vars['string'] = "value";
$patterns = array_map("maskPattern", array_keys($vars));
$result = str_replace($patterns, $vars, file_get_contents($path));
return $result;
}
function maskPattern($value) {
return "{$" . $value . "}";
}
All you PHP must be in a <?php ?> block like this:
<html>
<p><?php echo "{" . $string . "}";?></p>
</html>
If you know the variable to replace in the html you can use the PHP function 'str_replace'. For your script,
$string = "TEST";
$content = file_get_contents('test.html');
$content = str_replace('{$string}', $string, $content);
echo($content);
It's simple to use echo.
<html>
<p>{<?php echo $string;?>}</p>
</html>
UPDATE 1:
After reading so many comments, found a solution, try this:
$string = "TEST";
$template = file_get_contents('themes/default/test.html', FILE_USE_INCLUDE_PATH);
$page = str_replace('{$string}',$string,$template);
echo $page;
Embedding php inside html </p>
<?php
$text ='Click here';
$link = 'http://www.google.com';
?>
<php? echo $text; ?>
Why is this not printing out link and text assigned in the php code inside the html tags?
If you'll always be running your code in PHP 5.4+, you could use short echo tags;
<?php
$text ='Click here';
$link = 'http://www.google.com';
?>
<?= $text ?>
Looks a little neater in my opinion, but it's a matter of preference, and short echo tags aren't on by default in earlier versions of PHP, so I wouldn't recommend it if your code is ever going to run on server with PHP versions below 5.4
Another way
Embedding php inside html </p>
<?php
$text ='Click here';
$link = 'http://www.google.com';
echo ''.$text.'';
?>
Use sprint
<?php
$text ='Click here';
$link = 'http://www.google.com';
echo sprintf(" %s", $link, $text);
?>
Use this
Embedding php inside html </p>
<?php
$text ='Click here';
$link = 'http://www.google.com';
?>
<?php echo $text; ?>
It is <?php not <php?
use the below code
<?php echo $text; ?>
For the server to interpret your php you need to close all your php code inside the <?php ?> tags and then echo that variable
Open PHP tags properly
You can embedd PHP inside tag as like :
<?php echo $text;?>
I'm trying to include a php file inside another php file, but it is not working and I don't know why.
Moreover, I'm getting no erroes. allow_url_include is enabled in php.ini file.
I'm using XAMPP server.
Below here is part of my code:
q.php
<div class="article">
<? php
include ('a.php');
?>
</div>
where a.php simply has echo statement:
echo "hello";
I'm posting bigger section of my code now.
<div class="artical">
<?php
$username = "root";
$password = "";
$database = "techinsight";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found)
{
$SQL = "SELECT * from questions";
$result = mysql_query($SQL);
while($db_field=mysql_fetch_assoc($result))
{
$x = $db_field['Qid'];
while($x==1 && $x==NULL)
{
$SQL = "SELECT * from questions";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result);
$x = $db_field['Qid'];
}
}
if($x==$x)
{
for($x; $x>0; $x--)
{
$SQL = "SELECT * from questions WHERE Qid=$x";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result);
$str_que = $db_field['question'];
echo "<div class='dabba'>
<div class='block_a'> <?php include('a.php'); ?> //here it is.
</div> <br>
<div class='block_b'>
it is 2nd section. <br>
</div><br>
<div class='block_c'>
last one.<br> </div>
</div> <br><br>";
}
}
}
?>
</div>
Try:
<?php //Before you had <? php <--
include "a.php";
?>
Make sure the files are in the same directory.
You have a space between <? and php
Remove it.
It can be a couple of things...
1. Place the included file in the correct folder
Make sure that when you use include you either provide the folder path correctly or place the file in the same folder as where you reference / call it from.
2. It is <?php and not <? php
You have a space too much. Actually, if you use <? php I believe PHP will look for a function called php as <? may also be an opening tag (assuming short tags are activated in your php.ini file: short_open_tag=On).
Bonus: make sure error reporting is turned on while debugging
Another thing that I'd recommend you to do is to setup your php.ini file to report for all errors while debugging:
error_reporting(E_ALL);
I haven't tested it, but I'm pretty sure your above code would have resulted in a notification on the missing function php.
And the real bonus (following your updated post) - what you did wrong
You have added the include inside echo - obviously it won't work. So replace this...
echo "<div class='dabba'>
<div class='block_a'> <?php include('a.php'); ?> ...
...with this...
echo "<div class='dabba'>
<div class='block_a'>"; include('a.php'); echo "...