I am essentially trying to combine to PHP statements.
<?php echo ROOT_PATH; ?>
<?php echo file_get_contents( "../css/themes/subtitle.php"); ?>
I want to achieve something like this:
<?php echo file_get_contents( "ROOT_PATH/css/themes/subtitle.php"); ?>
If you call constants inside single or double quotes then it will be always picked as a string.
You need to add like below:
<?php echo file_get_contents( ROOT_PATH."/css/themes/subtitle.php"); ?>
Its pretty simple you can change it to following
//full path of the file
$file_name = ROOT_PATH."/css/themes/subtitle.php";
echo file_get_contents($file_name);
it should work
. operator is used to concatenating.
Like:
$str = "Hello";
echo $str;
echo "World";
can be written as
$str = "Hello";
echo $str."World";
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
I'm try to run php tag inside a string like so:
$str = '<h1><?php echo Hello World ?></h1>';
echo $str;
But it returns
<h1><!-- ?php echo Hello World ? --></h1>
I want an output similar to this code:
ob_start();
require ("../view/file_name.inc");
$html = ob_get_contents();
ob_end_clean();
Without Using:
require
or
include
Using:
file_get_contents('../view/file_name.inc');
without templating as well
let say :
file_name.inc has this:
<h1><?php echo $title ?></h1>
<?php foreach($data as $d){ ?>
<p><?php echo $d->desc ?><p>
<?php } ?>
The output something like:
<h1>Hello</h1>
<p>desc1</p>
<p>desc2</p>
Use htmlentities function of PHP to achieve your goal.
$str = '<h1><?php echo Hello World ?></h1>';
echo htmlentities($str);
It will output
<h1><?php echo Hello World ?></h1>
EDIT
Use eval to execute php script in string. try below code
$str = '<h1><?php echo "Hello World" ?></h1>';
eval('?>'.$str.'<?php;');
echo $str;
this should be help you
$str = '<h1>Hello World</h1>';
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 have some problems to put a URL-function within an if-statement properly. The following method works fine outside of my if-statement and the newly created link refers to this URL: "h**p://www.blog.com/?location=Bern&date=1"
<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a>
If-Statement works also just fine when I put simply an URL (e.g. https://www.google.com/) in between the quotation marks, then if I put the URL-function above in there, the newly created link refers to this: "h**p://www.blog.com/%3C?php%20echo%20preg_replace%28". Actually it should refer to the URL above "h**p://www.blog.com/?location=Bern&date=1"
URL-Function within if-statement:
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) { echo
'<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a> ' ;
} else {
echo 'No cars.';
} ?>
Any ideas?
Solution thanks to IMSop:
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
}
else {
echo 'No cars.';
}
This sequence makes no sense:
echo '<a href="<?php echo ...
A quoted string and direct output outside the <?php ... ?> markers are completely different things. The ' starts a string, which will continue until you put another '; the <?php would start a block of PHP code if you weren't in one, but you already are - if you weren't the echo wouldn't mean anything.
To join multiple strings together, you can use the . ("concatenation") operator:
echo '>Heutel';
Alternatively, drop out of PHP mode like you were before, but with the if in place:
<?php
// In PHP mode...
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
// Now leaving PHP mode, but still inside the if condition...
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
// Re-enter PHP mode to close off the if statement
}
There's even an alternative syntax for control syntax which some people prefer to use in cases like this:
<?php
if ($some_condition) :
?>
your output here
<?php
endif;
?>
which is exactly the same as
<?php
if ($some_condition) {
?>
your output here
<?php
}
?>