conditional statements inside php heredocs syntax?
Asked Answered
P

4

9

i was wondering if you can have conditional statments inside a heredocs, this is my script but it deosnt parse out the $username properly?

php code:

function doSomething($username) {

if (isset($_SESSION['u_name'])){
$reply ='<a class ="reply" href="viewtopic.php?replyto=@$username.&status_id=$id&reply_name=$username"> reply </a>';

return <<<ENDOFRETURN

$reply

ENDOFRETURN;

the problem with this is the $username variable deosnt get rendered on the html. it remains $username :)) thanks

Pavior answered 25/10, 2010 at 5:58 Comment(0)
K
8

Do you want to have conditional statements in heredoc or do you wonder why your code does not work? Because currently you have no conditional statement inside heredoc, but it is not possible anyway.

If you wonder why your code does not work:
This is not related to heredoc, it is because the entire $reply string is enclosed in single quotes, where variable parsing is not supported. Use string concatenation:

$reply ='<a class ="reply" href="viewtopic.php?replyto=@' . $username . '.&status_id=$id&reply_name=' . $username . '"> reply </a>'

I hope you are doing more with heredoc in your real code, otherwise return $reply would be easier ;) (and you are missing brackets).

Karlykarlyn answered 25/10, 2010 at 6:2 Comment(2)
+1 for "I hope you are doing more with heredoc in your real code"Phono
cheers it works, no im returning a whole paragraph, this example was just for illustration only, i didint want to hurt your eyes :)) cheers!!Pavior
Q
14

Easy. Wrap everything in curly braces (obviously supported in Heredocs) and then use an anonymous function and return what is necessary for the logic :] you can even get advance with it and use expressions inside the the anonymous function variable within the heredocs.

Example:

// - ############# If statement + function to set #################


$result = function ($arg1 = false, $arg2 = false)
{
    return 'function works';
};

$if = function ($condition, $true, $false) { return $condition ? $true : $false; };


// - ############# Setting Variables (also using heredoc) #########


$set = <<<HTML
bal blah dasdas<br/>
sdadssa
HTML;

$empty = <<<HTML
data is empty
HTML;

$var = 'setting the variable';


// - ############## The Heredoc ###################################


echo <<<HTML
<div style="padding-left: 34px; padding-bottom: 18px;font-size: 52px; color: #B0C218;">
    {$if(isset($var), $set, $empty)}
    <br/><br/>
    {$result()}
</div>
HTML;
Quintile answered 11/8, 2015 at 15:52 Comment(1)
maybe not easy? but certainly cunning. I found myself using a heredoc, then having to come back later and make some parts of it conditional. I think I would not have used a heredoc in the first place if I knew where it was going, but having done so, I did not want to rewrite it. This solution worked, but I am a little uncomfortable that the advantages of heredocs quickly erode as you need to use clever coding to get around its shortcomings. In the future I will probably use obstart() functions to collect blocks of HTML into a variable, as I have in the past.Vendue
K
8

Do you want to have conditional statements in heredoc or do you wonder why your code does not work? Because currently you have no conditional statement inside heredoc, but it is not possible anyway.

If you wonder why your code does not work:
This is not related to heredoc, it is because the entire $reply string is enclosed in single quotes, where variable parsing is not supported. Use string concatenation:

$reply ='<a class ="reply" href="viewtopic.php?replyto=@' . $username . '.&status_id=$id&reply_name=' . $username . '"> reply </a>'

I hope you are doing more with heredoc in your real code, otherwise return $reply would be easier ;) (and you are missing brackets).

Karlykarlyn answered 25/10, 2010 at 6:2 Comment(2)
+1 for "I hope you are doing more with heredoc in your real code"Phono
cheers it works, no im returning a whole paragraph, this example was just for illustration only, i didint want to hurt your eyes :)) cheers!!Pavior
I
1

This question has aged a bit but to provide a more complete answer with a few possible solutions. Conditionals are not allowed "inside" a heredoc but you can use conditionals with heredocs.

These examples should give you an idea of heredoc usage. Take note that the first line of a heredoc starts with 3 less than symbols and some arbitrary text with no space at the end of the first line. Press enter. The heredoc must be closed similarly. As you probably know, the text used to open the heredoc must be used to close it which is typically but not required to be followed by a semicolon. Be sure no trailing whitespace or characters follow the semicolon or last text character and then press enter.

function doSomething($username = '', $status_id = '') {

  if ('' != $username && '' != $status_id) {

    $reply = <<<EOT
<a class ="reply" href="viewtopic.php?replyto=@{$username}&status_id={$status_id}&reply_name={$username}"> reply </a>

EOT;

  } else {

    $reply = <<<EOT
<h2>The username was not set!</h2>

EOT;

  }

  return $reply;

}

echo doSomething('Bob Tester', 12);
echo doSomething('Bob Tester');
echo doSomething('', 12);

Depending on your specific situation you may find it helpful to use a class to do your comparisons that you wanted to use in your heredoc. Here is an example of how you may do that.

class Test {

  function Compare($a = '', $b = '') {

    if ($a == $b) 
      return $a;

    else
      return 'Guest';

  }

};

function doSomething($username = '') {

  $Test = new Test;

  $unme = 'Bob Tester';

  $reply = <<<EOT
  <h2>Example usage:</h2>
  Welcome {$Test->Compare($username, '')}<br />
  <br />
  Or<br />
  <br />
  Welcome {$Test->Compare($username, $unme)}

EOT;

  return $reply;

}

echo doSomething('Bob Tester');
Illiquid answered 7/1, 2012 at 23:31 Comment(0)
I
0

I think this could be a simple solution

$canDisplay = validateIfCanDisplay();

$getRenderAvailabilityTime = renderAvailabilityTime(myArgs);

if($canDisplay && $getRenderAvailabilityTime){ 
    $templateResult= <<<HTML
    <div class="d-flex justify-content-center align-items-center mt-5">
        <h5 class="mb-0 lead">
            $getRenderAvailabilityTime
        </h5>
    </div>
    HTML;
}

and later print $templateResult in your template

Isidraisidro answered 20/10, 2023 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.