<input id="username" name="username" type="text" size="30" value="<?php
if (isset($_POST['username'])) {
echo $_POST['username'];
}
elseif (isset($_SESSION['username'])) {
echo $_SESSION['username'];
}
else {
echo "Username";
}
?>" />
Refactorings
No refactoring yet !
slaskis
November 25, 2007, November 25, 2007 15:51, permalink
Long time ago i coded any php so i'm not sure this is valid code, but maybe something like this?
<?php
function has( var , default ) {
if( isset( $_POST[var] )
return $_POST[var];
elseif( isset( $_SESSION[var] )
return $_SESSION[var];
else
return default;
}
?>
<input id="username" name="username" type="text" size="30" value="<?=has('username','Username');?>" />
BetaDevil
November 25, 2007, November 25, 2007 16:50, permalink
<?php $username = (isset($_POST['username'])) ? $_POST['username'] : (isset($_SESSION['username'])) ? $_SESSION['username'] : 'Username'; ?> echo '<input id="username" name="username" type="text" size="30" value=' . $username . '" />'; <input id="username" name="username" type="text" size="30" value=<?= $username ?>" />
Eineki
November 26, 2007, November 26, 2007 23:00, permalink
I prefer heredoc (http://www.php.net/manual/en/language.types.string.php #language.types.string.syntax.heredoc),
You can set all the variables you need in the first part of the script and concentrate on the layout in the second part
without worrying of single or double quotes and using the graph parenthesis to enclose the variables.
<?php
$username = (isset($_POST['username'])) ? $_POST['username'] : (isset($_SESSION['username'])) ? $_SESSION['username'] : 'Username';
echo <<< EXAMPLE
<input id="username" name="username" type="text" size="30" value="{$username}" />
EXAMPLE;
?>
clonecd074
May 9, 2011, May 09, 2011 00:30, permalink
It is easier to perceive error than to find truth, for former lies on the surface and is easily seen, while the latter lies in the depth, where few are willing to search for it.
How can I put the php-code in an function?