开发者

php constants and EOT

is it possible to display constants php's EOT? for example:


<?
  define('Hey', "Hell开发者_开发百科O");
  echo
<<<EOT
Hey
EOT;
?>


Nope. Just like you can't display a constant inside of a string. There are only two real ways to access a constant's value:

Directly in code:

$foo = Hey;

Or using the constant function:

$foo = constant('Hey');


No, the heredoc syntax (<<<) acts just like a double quoted string in PHP. It will expand variables, but not constants.

You can see a comment in the PHP documentation indicating this here. There is also an editors note on the comment saying that it is correct.


I found the solution to being able to display HTML/PHP and even PHP constants and then storing the extended string into a variable:

my_file.php



Welcome back <?=$User -> get_display_name(); ?>!



index.php


<?

  define('HEY', "Welcome to my website!");

  ob_start(); ?>
  <div id=welcomeBox>
  <? if($User -> is_real()): ?>
  <? require_once("my_file.php"); ?>
  <? else: ?>
  <?=HEY; ?> Click <a href=JavaScript:SignUp()>here</a> to sign up!
  <? endif; ?>
  </div>
  <?
  $page_body = ob_get_contents();
  ob_end_clean(); ?>
  <div>
    Aye
  </div>
<?=$page_body;?>



If the user is real the output would be something like:


  <div>
    Aye
  </div>
  <div id=welcomeBox>
    Welcome back Johnny Cash!
  </div>


Otherwise it would say:


  <div>
   Aye
  </div>
  <div id=welcomeBox>
    Welcome to my website! Click <a href=JavaScript:SignUp()>here</a> to sign up!
  </div>


0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜