不使用变量重写PHP代码
PHP代码示例:不使用变量重写PHP代码,php,variables,Php,Variables,PHP代码示例: <?php $image_attributes = wp_get_attachment_image_src( '8' ); ?> <img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_at
<?php
$image_attributes = wp_get_attachment_image_src( '8' );
?>
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
当我像这样编码它时,我如何做同样的事情
foreach ( $attachments as $att_id => $attachment ) {
$attachment_attributes = wp_get_attachment_image_src( '8' );
// Should it be done like this? If not, how do I do it?
$output .= '<media:content height="' . $attachment_attributes[0]; . '" type="image/jpeg">';
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag}>" . wptexturize($attachment->post_excerpt) . "</{$captiontag}>";
}
$output .= '
</media:content>';
}
foreach($att_id=>attachment的附件){
$attachment_attributes=wp_get_attachment_image_src('8');
//应该这样做吗?如果不是,我该怎么做?
$output.='';
如果($captiontag&&trim($attachment->post_摘录)){
$output.=”
“.wptexturize($attachment->post_摘录)。”;
}
$output.='
';
}
不确定您为什么要避免变量,但您可以通过以下方式逃脱:
<?php
vprintf(
'<img src="%s" width="%d" height="%d">',
wp_get_attachment_image_src( '8' )
);
我不知道你在问什么。使用变量有什么问题?@MartinBean我有一个例子,我的知识不足以使用变量(它变得复杂)。所以,我想试试另一种方法around@its_me变量是编程的重要组成部分。如果你不能处理变量,那你就错了。据我所知,没有变量编程几乎是不可能的。试试看,这是更有效的方法。另一种方式会使服务器消耗比正常情况更多的CPU。@It_me:说出你的目标是什么,而不是你试图采取什么步骤来达到这个目标是非常重要的。如果你从一开始就说这段代码太复杂和难看,有没有更简单的方法来编写它?它会让你更快地找到答案。这会更慢、更令人困惑,但会满足OP的要求。
<?php
vprintf(
'<img src="%s" width="%d" height="%d">',
wp_get_attachment_image_src( '8' )
);
<?php
foreach ( $attachments as $att_id => $attachment ) {
$attachment_attributes = wp_get_attachment_image_src( '8' );
$output .= '
<media:content
url="' . $attachment_attributes[0] . '"
width="' . $attachment_attributes[1] . '"
height="' . $attachment_attributes[2] . '"
type="image/jpeg">';
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag}>" . wptexturize($attachment->post_excerpt) . "</{$captiontag}>";
}
$output .= '
</media:content>';
}