55502f40dc8b7c769880b10874abc9d0

How can I put the php-code in an function?

<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 !

Ad2ffc6b05fb4390643f36a258b86362

slaskis

November 25, 2007, November 25, 2007 15:51, permalink

2 ratings. Login to rate!

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');?>" />
54cb150c98bc09e66324263887554a05

BetaDevil

November 25, 2007, November 25, 2007 16:50, permalink

1 rating. Login to rate!
<?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 ?>" />
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

November 26, 2007, November 26, 2007 23:00, permalink

No rating. Login to rate!

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;
?>
D41d8cd98f00b204e9800998ecf8427e

asd

January 17, 2010, January 17, 2010 02:17, permalink

No rating. Login to rate!
asd
B298f1c7fc757fa0906c27219f221f39

saurabh

March 3, 2010, March 03, 2010 13:42, permalink

No rating. Login to rate!

manasdnmas

436fe69dcd2e4f07301ade376d7c2daa

clonecd074

May 9, 2011, May 09, 2011 00:30, permalink

No rating. Login to rate!

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.

Your refactoring





Format Copy from initial code

or Cancel