0

Rookie here, so please correct me if I have anything wrong.

So here's a snippet of my HTML:

<html><body>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
</body></html>

The function, $fgmembersite->UserFullName(), returns a string (100%, if I call the function within tags it prints out correctly). How do I get it to echo out in the HTML?

I think an alternative would be to echo the entire HTML code, and I think it'd work then, but I don't want to do it that way because I read somewhere that echoing all of your HTML is bad. Could somebody also confirm/deny that?

Right now, this is what shows on the site: Welcome back UserFullName(); ?>!

No idea why

Thanks for your time!

10
  • What happens when you use <?php echo $fgmembersite->UserFullName(); ?>? Commented Mar 30, 2014 at 15:32
  • You need to keep the <?= ... ?> (or <?php echo ... ?>) surrounding the code itself. Commented Mar 30, 2014 at 15:32
  • I'm also relatively new to stackoverflow, why would you add a comment instead of post an answer? Commented Mar 30, 2014 at 15:34
  • This is because short tags are not "on". @MattyAyOh Commented Mar 30, 2014 at 15:38
  • 1
    The reason it's a comment instead of an answer is that it was intended to gather more data. The real answer is @Fred-ii- comment, and my comment highlighted that. To be fair, though, it's common to have them disabled and using the longer syntax is often the way to go. Commented Mar 30, 2014 at 15:42

5 Answers 5

3

I think you want the code to look like this...

Welcome back <?php echo $fgmembersite->UserFullName(); ?>!

This will ECHO the result of the function call.

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

1 Comment

<?= is equivalent to <?php echo.
2

What you are seeing is the result of the browser trying to parse the PHP. It is trying to treat <?= $fgmembersite-> as an unknown tag and then renders the rest as text.

There are two possible reasons for this:


You need to pass it through a PHP engine first.

You need to access the file through a web server which supports PHP and is configured to run your file through PHP when it is requested (typically this is done by using a .php file extension).

Note that installing a web server then double clicking a PHP file in your file manager isn't sufficient - the browser will just load the file from the file system. You need to type http://etc etc.


It is also possible that you have short_open_tag disabled (which is common and sensible) and are using PHP 5.3 or older (which isn't a great idea, 5.3 is still supported but it is the oldest branch that is).

If this is the case, your options are:

  • Upgrade PHP
  • Use <?php echo ... ?> instead of <?= ... ?>
  • Enable short_open_tag

3 Comments

the file extension is .php, is that what you meant by pass through a php engine? (pardon my ignorance)
@MattyAyOh — See the third and fourth paragraphs of the answer.
Yup it is the short_open_tag thing. Thanks for the help!
2

Can't you just do

<?php echo $fgmembersite->UserFullName(); ?>

?

2 Comments

He is. <?= is equivalent to <?php echo.
Obviously, short tags are not on. @Quentin See the comments under OP's question.
2

use it as follows:

<html><body>
<h2>Home Page</h2>
Welcome back <?php echo $fgmembersite->UserFullName(); ?>!
</body></html>

2 Comments

He is. <?= is equivalent to <?php echo.
@Quentin the short-tags behavior depends on configuration, echo does not.
1

Do you have short_open_tag enabled on your server? You need to have that enabled to use that syntax. If you don't have that, use <?php echo $fgmembersite->UserFullName(); ?> like the other users suggested.

You can check the setting on you server by creating a file containing <?php phpinfo(); in it, and accessing that.

Also, echoing all your HTML inside the <?php tags are considered a bad practice because you should separate your business logic and your HTML. Echoing HTML directly from inside the PHP tags quickly leads to a mix of HTML and PHP, and it will be harder to read (for others, and yourself later on)

2 Comments

Doesn't short_open_tag only influence <? not <?=?
Yes, from PHP 5.4 and up.

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.