简体   繁体   中英

Variable in php echo

I would really need help with a primitive thing.

I need this:

<?php echo do_shortcode('[cycloneslider id="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo $my_meta['odkaz']; ?>"]'); ?>

I do not know how exactly do it. I made it by this solution:

   <?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);?> 
    <?php echo do_shortcode('[cycloneslider id="'.$my_meta['odkaz'].' "]'); ?>

Is it right or..Could you edit it to right way?

You can write it using a string interpolation :

<?php 
    $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
    echo do_shortcode("[cycloneslider id=\"{$my_meta['odkaz']}\"]"); 
?>

Or using a heredoc if extra spaces are ok:

<?php 
    $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
    $shortcode = <<<TEXT
[cycloneslider id="{$my_meta['odkaz']}"]
TEXT;
    echo do_shortcode($shortcode); 
?>

Concatenate string and variables with the . operator:

$var = "Hello, I am made up of " . $some . $variables . " and strings";

I suppose you don't see clear your own code.
If that happens, then adopt a more verbose way of programming and create variables that describe more the values:

<?php 

$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$idCycloneSlider = $my_meta['odkaz'];
$doShortCodeResult = do_shortcode('[cycloneslider id="'.$idCycloneSlider.'"]');
echo $doShortCodeResult; 

?>

That way we see clearly what happens in each line and the string concatenation is more simple to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM