开发者

Can I echo multiple html tags containing php variables with a single echo?

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开发者_JS百科>Pay:' . $pay. '</p>';
} else { 
    echo'';
} 
?>


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://<?=$url?>/" title="<?=$url?>">http://<?=$url?></a></p>
 <p>Pay: <?=$pay?></p>
<?php } ?>


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}</a></p>";
  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}</a></p>
        <p>Pay: {$pay}</p>";
}

?>


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.


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</a></p>"
    . "<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</a></p>
<p>Pay:$pay</p>
STR;

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


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}</a></p><p>Pay:{$pay}</p>";

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</a></p>
    <p>Pay:$pay</p>
HTML;

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


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.


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

<?php echo sprintf('<p>Job: <span>%s</span></p>', $job); ?>


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}</a></p>
 <p>Pay: {$t_pay}</p>
{/if}


This looks like a good opportunity to use printf() so that you don't need to bounce in and out of <?php/?> tags.

Create a clean template string with placeholders with HEREDOC syntax so that you don't need to worry about juggling and escaping quotation marks.

Because you are using the $url variable more than once, use numbered placeholders so make your code more D.R.Y.

$template = <<<HTML
    <p>Job: <span>%1$s</span></p>
    <p>Skills: <span class="caps">%2$s</span></p>
    <p>Website: <a href="http://%3$s" title="%3$s">http://%3$s</a></p>
    <p>Pay: %4$s</p>
HTML;
if (!empty($url)) {
    printf($template, $job, $skills, $url, $pay);
}

Using placeholders like this makes your code very easy to maintain. Removing the noise of variables and concatenation may help you to identify typos/mistakes -- such as a missing space after Pay:.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜