D
D
djienbaev972020-04-21 20:57:38
PHP
djienbaev97, 2020-04-21 20:57:38

How to write php code in php variable?

<?php $html = ' '; ?> can I write php code there? I can't. Can

this code be written to the $html variable?

<div class="news-box2">
                                    <img class="news-img" src="<?= $new->img ?>" alt="">
                                    <div class="news-title-g"><?= StringHelper::truncate($new->title,25,'...'); ?></div>
                                </div>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Sarvarov, 2020-04-21
@djienbaev97

Using concatenation:

$var = '<div class="news-box2">
    <img class="news-img" src="' . $new->img . '" alt="">
    <div class="news-title-g">' . StringHelper::truncate($new->title,25,'...') . '</div>
</div>';

Using HEREDOC:
$var = <<<EOL
<div class="news-box2">
    <img class="news-img" src="{$new->img}" alt="">
    <div class="news-title-g">{StringHelper::truncate($new->title,25,'...')}</div>
</div>
EOL;

Using output buffering:
ob_start(); ?>

<div class="news-box2">
    <img class="news-img" src="<?= $new->img ?>" alt="">
    <div class="news-title-g"><?= StringHelper::truncate($new->title,25,'...') ?></div>
</div>

<?php

$var = ob_get_contents(); 

ob_end_clean();

S
scottparker, 2020-04-21
@scottparker

<?php $html = '<div class="news-box2">
                                    <img class="news-img" src="<?= $new->img ?>" alt="">
                                    <div class="news-title-g"><?= StringHelper::truncate($new->title,25,'"..."); ?></div>
                                </div>'; ?>

R
rasschitai, 2020-04-21
@rasschitai

here is the mantra and there on the links

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question