I've got a php function that is something like this
function get_some_info()
{
$location = other_function_that_returns_a_string();
return <<<HTML
<script>
$("<h1> <?php echo $location ?> </h1>").insertAfter("header");
</script>
HTML;
}
but I can't get my variable to dynamically display.
I've also tried something like this
$("<h1> <?php $location = other_function_that_returns_a_string() echo $location?> </h1>").insertAfter("header");
and it never displays anything dynamically. How can I get my variable to show up?
The issue is that you use the HEREDOC syntax.
You don't have to use <?php and ?> inside that.
This will work fine:
$("<h1> $location </h1>").insertAfter("header");
Here is the code I tested in PhpFiddle:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
Header
</header>
<?php
function other_function_that_returns_a_string(){
return "A string";
}
function get_some_info(){
$location = other_function_that_returns_a_string(); // Will be "A string"
return <<<HTML
<script>
$("<h1> $location </h1>").insertAfter("header");
</script>
HTML;
}
echo get_some_info();
?>
Related
I'm trying to fetch a variable which is defined in a different function inside the same file into a different shortcode function, but it's not reflecting it, can anybody please let me know what I'm doing wrong
if ($hits[0]<= $varlinkhits)
{
$buttonlink="$varlinkone";
}
else
{
$buttonlink="$varlinktwo";
}
?>
<?php
//creating short code
?>
<button onclick="onclickRedirect()">redirect</button>
<script>
function onclickRedirect(){
window.location.href = "<?php echo $buttonlink ?>";
}
</script>
<?php
}
session_start();
function link_button_function() {
global $buttonlink;
$_SESSION["sessionlink"] = $buttonlink;
return 'Join Whatsapp Group';
}
add_shortcode('button_link', 'link_button_function');
First of all I should say this code is a sample and i remake for you to show what i want my original code too huge and can't post here, but the logic and functions similar to this example.
I made a dynamic title in a function called getTitle() and it show on #side-bar now i want to use this title in h1 tag too, but as you see h1 tag rendered before this function, and i'm not able to move h1 tag after #side-bar. Now i want to know how can i echo a variable that generated from a function, before call this function.
PHP:
<?php
function getTitle(){
global $title;
// some code to generate dynamic title
$title = "example title";
echo $sample = "123";
return $title;
// other code
}
?>
HTML:
echo <h1><?=$title;?></h1>;
<div id="side-bar"><?php getTitle(); ?></div> <!-- call -->
I know if i move $title after calling the function getTitle(); it works fine but i need to echo $title before calling this function. Is it possible? or any idea, or logic to do this?
Also i know i can clone the title from side-bar to h1 with javascript or etc.. but this is a h1 tag and can't fill it after page load in client side.
PHP:
<?php
function getTitle(){
// some code to generate dynamic title
return ['title' => 'Example title', 'sample' => '123'];
}
?>
HTML:
$response=getTitle();
echo <h1><?=$response["sample"];?></h1>;
<div id="side-bar"><?php $response["title"]; ?></div> <!-- call -->
Your approach is wrong. Instructions are not executed after return statement.
PHP function
<?php
function getTitle(){
$title = /*generate dynamic title*/;
return $title;
}
?>
HTML
<?php $generatedTitle = getTitle(); ?>
<h1><?php echo $generatedTitle; ?></h1>
<div id="side-bar"><?php echo $generatedTitle; ?></div>
I want to use a variable inside an HTML-String of another PHP-File template.php in my PHP-File constructor.php.
I´m searched on Stackoverflow for a workaround to include the content of the other PHP-File. I included the following code into constructor.php because its known to be more safe instead of using file_get_contents(); Source:
function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
The rest of constructor.php looks like this:
...
$sqli = mysqli_query($mysqli, "SELECT ...");
if(mysqli_num_rows($sqli) > 0){
$ii = 0;
while ($row = $sqli->fetch_assoc()) {
$ii++;
if($row['dummy']=="Example"){
$content.=requireToVar('template.php');
...
The template.php looks like this:
<?php echo "
<div class='image-wrapper' id='dt-".$row['id']."' style='display: none;'>
...
</div>
"; ?>
The constructor.php doesn´t recognize the var $row['id'] inside the string of template.php as its own variable and also doesn´t execute it. The variable definitely works for the other code in constructor.php.
If I´m copy&paste the code of template.php into constructor.php after $content.= its working like a charm. But I want to restructure my constructor.php because its getting to big and this way its easier to customize.
I don´t know how to describe this problem more exactly, but I´m hoping this title fits to my problem.
Update your function
function requireToVar($file,$row_id){
ob_start();
require($file);
return ob_get_clean();
}
so you can call it like that
while ($row = $sqli->fetch_assoc()) {
$ii++;
if($row['dummy']=="Example"){
$content.=requireToVar('template.php',$row['id']);
and display it in template.php like that
<div class="image-wrapper" id="dt-<?php echo $row_id; ?>" style="display: none;"></div>
Use MVC model and renderer functions. For example: index.php
<!DOCTYPE HTML>
<html>
<head>
<title><?=$title; ?></title>
</head>
<body>
<h3><?=$text; ?></h3>
</body>
</html>
Than I'll have PHP array that contains those variables:
$array = array("title"=>"Mypage", "text"=>"Mytext");
Now we will use both in renderer function
function renderer($path, $array)
{
extract($array); // extract function turn keys into variables
include_once("$path.php");
}
renderer("index", $array);
Your approach is rather strange, but anyway; you can access $row from within $GLOBALS array.
Write your template as:
<?php echo "
<div class='image-wrapper' id='dt-".$GLOBALS['row']['id']."' style='display: none;'>
...
</div>
";
?>
I have a PHP function that I'm using to output a standard block of HTML
<?php function test () { ?>
echo(" <html>
<body><h1> HELLO </h1> </body>
</html>
<?php } ?>
but i see the string of HTML and not the page
I have see this
Is there any way to return HTML in a PHP function? (without building the return value as a string)
if you use SLIM framework change the content type:
$app->contentType('application/json');
to:
$app->contentType('text/html');
then use render function of slim instance with specific template or simply echo html string
you have to do
<?php
function text() {
echo '<html><body><h1>Hello</h1></body></html>';
}
?>
But in addition this is not the base valid structure of a page. You're missing the <head /> tag.
Try this:
<?php
function test ()
{
echo '<html><body><h1> HELLO </h1> </body></html>' ;
}
?>
Try this:
<?php
function text() {
return '<html><body><h1>Hello</h1></body></html>';
}
?>
and where you need it just:
<?php echo text(); ?>
Try out this:
<?php
function test()
{
$html = '<html>';
$html .= '<body>';
$html .= '<h1>Hello</h1>';
$html .= '</body>';
$html .= '</html>';
echo $html;
//or
return $html;
}
?>
In PHP you have something called heredoc which lets you write large amounts of text from within PHP, but without the need to constantly escape things. The syntax is <<<EOT [text here] EOT;
So in your case you can have the function return the text like this
function test() {
return <<<EOT
<html>
<body><h1>HELLO</h1></body>
</html>
EOT;
}
Then just call function test to get the contents
echo test();
Is it possible to use functions within the heredoc template without breaking it?
Somethig like this:
<<<HTML
<div> showcaptcha(); </div>
HTML;
Specifically i wanna require another template in this one without using variables.
Or is there another and more simple solution that is not using heredoc?
Thx in advanced.
For example im using class named requires.
class requires {
public function __construct(){
$this->MYSQL();
$this->FUNC();
}
public function MAIN_MENU() {
require_once ('main-menu.tpl');
}
}
Then what i do in index.php
require_once ('requires.php');
$req = new requires();
echo <<<HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
//bla bla
</head>
<nav><ul class="fancynav">
{$req->HEAD_SCRIPTS()}
</ul></nav>
HTML;
main-menu.tpl
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
And in result i have an empty field without php error
<nav><ul class="fancynav">
</ul></nav>
WTF?
Yes, you can use the "encapsed var" trick:
function hello() {
global $result;
$result = 'hi there!';
return 'result';
}
echo <<<EOF
text ${hello()} text
EOF;
Technically, this works, but it's better to avoid hacks like this in production. A temporary variable will be much cleaner.
Since you cannot directly call a function inside HEREDOC, you may put the function name in a variable, and use that variable inside the HEREDOC string:
$showcaptcha= 'showcaptcha';
echo
<<<HTML
<br/> dawg {$showcaptcha()} dawg
HTML;
So you can leave the current already coded function:
function showcaptcha()
{
return "Gotta captcha mall";
}
You could also use:
$showcaptcha = function()
{
return "Gotta captcha mall";
}
echo
<<<HTML
<br/> dawg {$showcaptcha()} dawg
HTML;
If the function to call is defined at the same level as the echo (else, you'll have a global variable $showcaptcha).
If you have several functions, you can make a loop before the heredoc:
function dawg()
{
return "I'm loyal";
}
function cat()
{
return "I'm cute";
}
function fish()
{
return "I'm small";
}
function elephant()
{
return "I'm big";
}
$functionsToCall = array('elephant', 'fish', 'cat', 'dawg');
foreach ($functionsToCall as $functionToCall)
$$functionToCall = $functionToCall;
echo
<<<HTML
<br/> Dawg: {$dawg()}
<br/> Cat: {$cat()}
<br/> Fish: {$fish()}
<br/> Elephant: {$elephant()}
HTML;
That's way less ugly than using a global variable
No, it is not possible to use function inside heredoc strings.