Home » Php » conditional statements inside php heredocs syntax?

conditional statements inside php heredocs syntax?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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="[email protected]$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

How to&Answers:

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="[email protected]' . $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).

Answer:

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:

// - 

Answer:

Answer:

Answer:

Answer:

# If statement + function to set

Answer:

Answer:

Answer:

Answer:

Answer:

## $result = function ($arg1 = false, $arg2 = false) { return 'function works'; }; $if = function ($condition, $true, $false) { return $condition ? $true : $false; }; // -

Answer:

Answer:

Answer:

Answer:

# Setting Variables (also using heredoc)

Answer:

Answer:

Answer:

$set = <<<HTML bal blah dasdas<br/> sdadssa HTML; $empty = <<<HTML data is empty HTML; $var = 'setting the variable'; // -

Answer:

Answer:

Answer:

Answer:

## The Heredoc

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

## 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;

Answer:

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="[email protected]{$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');