views:

201

answers:

9
+1  Q: 

PHP echo question?

Do I really need this many echo's or can I make it shorter?

<?php
if (!empty($url))
{
    echo '<p>Job: <span>' . $job .'</span></p>';
    echo '<p>Skills: <span class="caps">' . $skills . '</span></p>';
    echo '<p>Website: <a href="http://' . $url . '" title="' . $url . '">http://' . $url . '</a></p>';
    echo '<p>Pay:' . $pay. '</p>';
} else { 
    echo'';
} 
?>
A: 

You can do all that with single echo and concatenation, which is just "." , and yes you ll need one more echo for the else stmt.

else statement is a no-op.
Michael Krelin - hacker
+10  A: 

Why not revert it?

<?php if(!empty($url)) { ?>
 <p>Job: <span><?=$job?></span></p>
 <p>Skills: <span class="caps"><?=$skills?></span></p>
 <p>Website: <a href="http://&lt;?=$url?&gt;/" title="<?=$url?>">http://&lt;?=$url?&gt;&lt;/a&gt;&lt;/p&gt;
 <p>Pay: <?=$pay?></p>
<?php } ?>
Michael Krelin - hacker
Note that short tags like `<?` instead of `<?php` may be turned off on some PHP installations.
ceejayoz
True. The thing to watch out for.
Michael Krelin - hacker
that be a nice way to do it there, arrrr. also look into if: endif: syntax. http://stackoverflow.com/questions/564130/php-difference-between-if-and-if-endif
pxl
It's a matter of taste, but I really don't like this verbose syntax.
Michael Krelin - hacker
I don't really understand the appeal of if:endif. If your code is properly indented, there should never be any ambiguity that would require endif.
eyelidlessness
eyelidlessness, moreover, if you have nested `if`s, `endif` is still ambigous ;-)
Michael Krelin - hacker
Good solution for this specific case, but it doesn't actually answer the question as it does not actually use echo.
Citizen
Citizen, there's no `echo` requirement. The question is whether one needs *this many echos* or one *can make it shorter* ;-)
Michael Krelin - hacker
+7  A: 

Sure, and there's no need for an else that does nothing:

<?php

if(!empty($url)) {
  echo "<p>Job: <span>{$job}</span></p>";
  echo "<p>Skills: <span class=\"caps\">{$skills}</span></p>";
  echo "<p>Website: <a href=\"http://{$url}\" title=\"{$url}\">http://{$url}&lt;/a&gt;&lt;/p&gt;";
  echo "<p>Pay: {$pay}</p>";
}

?>

... or even:

<?php

if(!empty($url)) {
  echo "<p>Job: <span>{$job}</span></p>
        <p>Skills: <span class=\"caps\">{$skills}</span></p>
        <p>Website: <a href=\"http://{$url}\" title=\"{$url}\">http://{$url}&lt;/a&gt;&lt;/p&gt;
        <p>Pay: {$pay}</p>";
}

?>
ceejayoz
A: 

you may want to try sprintf. You can do something like:

<?php echo sprintf('<p>Job: <span>%s</span></p>', $job); ?>
pivotal
you just made it worse! now instead of a ton of echo's, he's got a ton of sprintf's as well. hacker's answer works well.
pxl
echo "<p>{$job</p>";orecho '<p>'.$job.'</p>';Are both more efficient.
Citizen
I didn't mean replace the echos with a ton of sprintfs, I meant replace all of them with one sprintf - I was hoping he would extrapolate that without me going to the effort of typing out his entire string.
pivotal
That's true, Citizen, I wanted to write a comment like that too, but then I thought - why would one want to use php if performance is *that* much of a concern ;-)
Michael Krelin - hacker
`echo sprintf()` is a really weird replacement for `printf`, btw ;-)
Michael Krelin - hacker
A: 

You can, but would it be best to keep it as is for readability purposes? I don't think it will hurt the performance of your script having multiple echos.

Sky's The Limit Software
I find that sort of concatenation less readable than some alternatives, though.
ceejayoz
If there's no need for mulitples, you shouldn't have them.
Citizen
@Citizen Not necessarily. Sometimes, a few extra echo statements drastically improves readability with essentially zero performance loss.
ceejayoz
+3  A: 

You dont need to concatenate multiple lines or revert if you want to use echo. The actual answer to your question is this (because it still uses echo):

  <?php
        if (!empty($url))
        {
        echo '<p>Job: <span>' . $job .'</span></p>
    <p>Skills: <span class="caps">' . $skills . '</span></p>
        <p>Website: <a href="http://' . $url . '" title="' . $url . '">http://' . $url . '</a></p>
        <p>Pay:' . $pay. '</p>';
        }
        ?>

Php doesnt care how many lines you use.

Citizen
It does. It won't be the same output, it will be infested with whitespace.
Michael Krelin - hacker
Infested? This method will have the appropriate html formatting. p tags are supposed to be done on new lines in most code conventions.
Citizen
"appropriate" for what? Often the most appropriate html formatting is the one that takes less bytes. And, assuming, `p` is not a top-level element it's going to be broken, anyway.
Michael Krelin - hacker
The use is choosing to go multiline for the html for the same reason... html should be easy to maintain and read as well. Unless a few bytes are an issue, returns are not all bad.
Citizen
"easy to maintain"? Easy to maintain automatically generated html? What are you talking about? ;-)
Michael Krelin - hacker
+3  A: 

I suppose you could either :

  • use the dot (.) for strings concatenation, and use a single echo
  • or use several strings in a single echo, separated by commas ','

See the echo manual page, for examples.

For the first solution :

echo 'a' . 'b' . 'c';

And, for the second :

echo 'd', 'e', 'f';

And the output will be :

abcdef


Another solution would be to use variable interpolation in double-quoted string :

$my_var = "test";
echo "this is a $my_var";

Which will get you :

this is a test


For instance, using a bit of both :

$job = 'my job';
$skills = 'my skills';
$url = 'google.com';
$pay = 3.14;

echo "<p>Job: <span>$job</span></p>"
    . "<p>Skills: <span class=\"caps\">$skills</span></p>"
    . "<p>Website: <a href=\"http://$url\" title=\"$url\">http://$url&lt;/a&gt;&lt;/p&gt;"
    . "<p>Pay:$pay</p>";

You'll get :

Job: my job
Skills: my skills
Website: http://google.com
Pay:3.14

But note that you'll have to escape the ", which is not easy to do :-(


So, yet another solution, based on heredoc syntax :

echo <<<STR
<p>Job: <span>$job</span></p>
<p>Skills: <span class="caps">$skills</span></p>
<p>Website: <a href="http://$url" title="$url">http://$url&lt;/a&gt;&lt;/p&gt;
<p>Pay:$pay</p>
STR;

Only one echo, no string concatenation, no escaping -- what else could one ask for ?
;-)

Pascal MARTIN
You don't have to escape the ", you can use single quotes in HTML.
Skilldrick
+2  A: 

Of course this would do the job too:

echo '<p>Job: <span>' . $job .'</span></p><p>Skills: <span class="caps"></span></p><p>Website: <a href="http://' . $url . '" title="' . $url . '">http://' . $url . '</a></p><p>Pay:' . $pay. '</p>';

You may add \ns for more readable HTML.

Or you may use this one which is more readable on the PHP side:

echo "<p>Job: <span>{$job}</span></p><p>Skills: <span class=\"caps\"></span></p><p>Website: <a href=\"http://{$url}\" title=\"{$url}\">http://{$url}&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Pay:{$pay}&lt;/p&gt;";

Or you could use the heredoc syntax which is even more readable:

echo <<<HTML
    <p>Job: <span>$job</span></p>
    <p>Skills: <span class="caps"></span></p>
    <p>Website: <a href="http://$url" title="$url">http://$url&lt;/a&gt;&lt;/p&gt;
    <p>Pay:$pay</p>
HTML;

At last you can cut the else branch if you really want to echo - well - nothing.

Koraktor
A: 

I would start using templates if i were you. there are a multitude of templating engines out there.. I highly recommend Smarty. http://www.smarty.net/

This will allow you to separate your code from your display logic. This means that you can choose a different template, or give your template to a designer, who does not need to know anything about php. It will also allow you to go over your code at a later date and easily debug/change it. Nothing worse than trying to read code that has html and php intermingled throughout.

code:

//this part will instigate a copy of smarty
require_once('/location_of_smarty_lib/Smarty/Smarty.class.php');
$blah = new Smarty();
$blah ->template_dir = '/location_of_smarty_lib/smarty/templates';
$blah ->compile_dir  = '/location_of_smarty_lib/smarty/templates_c';
$blah ->cache_dir    = '/location_of_smarty_lib/smarty/cache';
$blah ->config_dir   = '/location_of_smarty_lib/smarty/configs';

//this part assigns variables to tags
$blah->assign('t_url',$url);
$blah->assign('t_job',$job);
$blah->assign('t_skills',$skills);
$blah->assign('t_pay',$pay);
//this will display the out put using the given template
$blah->display('yargh.tpl');

template (called yargh.tpl):

{if $t_url}
 <p>Job: <span>{$t_job}</span></p>
 <p>Skills: <span class="caps">{$t_skills}</span></p>
 <p>Website: <a href="http://{$t_url}/" title="{$t_url}">http://{$t_url}&lt;/a&gt;&lt;/p&gt;
 <p>Pay: {$t_pay}</p>
{/if}
Bingy
Are you using a pirate theme in that template example?
random
php is a template engine that happens to be used for other things as well. it was designed to integrate easily with markup languages. why complicate it with things like smarty?
kristofer
that is true.. but have you tried editing someones code who has mixed their display logic with their php code... It gets nighmare like, both in the short term and the long term.
Bingy