0

I've no clue if what I want is possible so a "no is not possible" could also be an answer for me.

Im making a PHP function like this:

<?php
function set_block_title($post){
    echo "<p class='title'>$post->get_title()</p>";
}

I would like to do this without an echo and instead get the html out of the PHP page (mainly because the designers who will break all the PHP when they don't have the HTML syntax).

I know it's possible with an if statement like this:

<?php if( condition ): ?>
    <p class='title'>
        <?php echo $post->get_title(); ?>
    </p>
<?php endif; ?>

Is there a way to do the same thing with a PHP function? I suppose I could do something like this:

<?php
function set_block_title($post){
    <?php if( true ): ?>
        <p class='title'>
            <?php echo $post->get_title(); ?>
        </p>
    <?php endif;
}?>
3
  • instead of echo just return the statement. But am not clear with what your need actually is Commented May 21, 2016 at 9:12
  • Oops, sorry, will try to avoid it in the future. Commented May 21, 2016 at 9:56
  • @StefanJanssen You could try to use one of the many view frameworks currently available that blend into the markup.... Commented May 21, 2016 at 10:32

3 Answers 3

2

There is another way of printing a php variable in your html:
instead of using:

"php echo $variable;"


you can use:

php =$variable;"

Sign up to request clarification or add additional context in comments.

1 Comment

That is only possible with a certain setting of PHP, and I also have more things to echo within the function :P
1

You could try this: Steps required BEFORE doing this are -- 1) A template file, preferably with the extension of PHP. 2) Basic knowledge of PHP. -- This is the fastest way.

$tmplt = readfile("PageTemplate.php");
echo $tmplt;

I hope I helped! Read up on the Readfile function at PHP.net - Readfile -- By the way: I know you asked for no echo, but you also asked to see the html syntax, so despite this having echo, it will still have the marks and everything. Sorry!

1 Comment

It has been quite some time since I asked this question, I don't know why I asked this and how I solved it in the end, so I gave you a vote up but I can't really give an "Correct answer", sorry.
0

you can do this way also.

<?php
function set_block_title($post){
   $html = "<p class='title'>$post->get_title()</p>";
  return $html;
}
?>

try above code may helps you.

1 Comment

hmm the main reason why I wanted to remove the echo is to get HTML syntax highlighting and seperating PHP and HTML. So this is not what I'm looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.