PHPKonf Istanbul PHP Conference 2019 - Call for Papers
add a note add a note

User Contributed Notes 3 notes

up
-9
jsb17 at cornell dot edu
11 years ago
As an addendum to David's 10-Nov-2005 posting, remember that curly braces literally mean "evaluate what's inside the curly braces" so, you can squeeze the variable variable creation into one line, like this:

<?php
 
${"title_default_" . $title} = "selected";
?>

and then, for example:

<?php
  $title_select
= <<<END
    <select name="title">
      <option>Select</option>
      <option
$title_default_Mr  value="Mr">Mr</option>
      <option
$title_default_Ms  value="Ms">Ms</option>
      <option
$title_default_Mrs value="Mrs">Mrs</option>
      <option
$title_default_Dr  value="Dr">Dr</option>
    </select>
END;
?>
up
-22
josh at PraxisStudios dot com
13 years ago
As with echo, you can define a variable like this:

<?php

$text
= <<<END

<table>
    <tr>
        <td>
            
$outputdata
        </td>
     </tr>
</table>

END;

?>

The closing END; must be on a line by itself (no whitespace).

[EDIT by danbrown AT php DOT net: This note illustrates HEREDOC syntax.  For more information on this and similar features, please read the "Strings" section of the manual here: http://www.php.net/manual/en/language.types.string.php ]
up
-22
Mike at ImmortalSoFar dot com
13 years ago
References and "return" can be flakey:

<?php
//  This only returns a copy, despite the dereferencing in the function definition
function &GetLogin ()
{
    return
$_SESSION['Login'];
}

//  This gives a syntax error
function &GetLogin ()
{
    return &
$_SESSION['Login'];
}

//  This works
function &GetLogin ()
{
   
$ret = &$_SESSION['Login'];
    return
$ret;
}
?>
To Top