Assign variables and display results
<?php
$somevariable = echo $anothervar;
?>
i get a T_ECHO unexpected error . what is the right way of accomplishing the above task?
I am extending this question 开发者_运维知识库a little bit :
<?php
$num_posts = get_option($shortname.'_num_posts');
$args = array(
'posts_per_page' => $num_posts,
'post_type' => 'post'
);
?>
This is a bit of wordpress code . The *get_option* function doesn't echo the value , so i tried
<?php
$num_posts = echo get_option($shortname.'_num_posts');
$args = array(
'posts_per_page' => $num_posts,
'post_type' => 'post'
);
?>
and i messed it . what is the right way to do it ?
I don't know what you are trying to do. echo
is a language construct, so returns nothing, so its return value cannot be assigned.
If you want to echo a value and assign it to another variable, it is best (most legible) to do it in two statements:
<?php
echo $anothervar;
$somevariable = $anothervar;
?>
It should just be
<?php $somevariable = $anothervar; ?>
What you are trying to do? If you want print, the code is:
<?php
$somevariable = $anothervar;
?>
If you want to assign the value of the another var, the code is:
<?php
echo $anothervar;
$somevariable = $anothervar;
?>
In order for you to catch any data from the function get_option you most return it from within the function itself:
<?php
function get_option($var){
return $var;
}
$num_posts = get_option($shortname.'_num_posts');
//outputs what ever was returned from the function
echo $num_posts;
?>
精彩评论