Set title with echo in PHP

Asked

Viewed 292 times

-3

I’m having a problem with the CMS I’m creating.

I want to set the site title using the php echo function, like this:

<?php
$ever = json_decode('../json/data.json');

$everthing = json_decode('../json/info.json');

?>

<!DOCTYPE html>

<head>


 <title><?php echo 'Rodrigo' ?></title>
<body>

</body>

</head>

But I’m not getting it.

Edit: Thanks for all the answers, this community is really fantastic. But it was a problem with my web server, I changed and ran everything.

  • Any error message ?

  • No, in the title of the page appears the following: <?php echo 'Rodrigo' ?>

  • your file is . php? Because the code is being displayed instead of compiled

  • Why <title><?php echo 'Rodrigo' ?></title> and not <title>Rodrigo</title>? If you will not use variable, I do not understand the reason.

3 answers

5

First you need to understand...

What is the difference between a PHP web page and HTML?

PHP files are similar to HTML files, but may include HTML and PHP code. The PHP code is parsed (or executed) by the web server when the page is accessed and the resulting output is saved as HTML on the web page. When a user accesses a PHP page, their web browser only receives the HTML code, as the web server has processed the PHP code in the background. Most PHP pages are processed so quickly that it does not significantly decrease the loading of the web page.

The . php extension is important as it informs the web server that the page can include PHP code. Therefore, it must be run through the server’s PHP engine before it is sent to a client’s web browser. This allows dynamic content to be generated every time the web page is loaded, based on the variables included in the PHP code. For example, PHP pages can load objects, such as the current date and time, form field data sent by a user, or database information. Still, when the page reaches the user’s web browser, everything is formatted as HTML.

Knowing this you should realize that your extension is . html and not . php, so the first thing to do is to change it. Then you need to understand the basic structure of HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Titulo</title>
    </head>
    <body>
        <h1>Cabeçalho</h1>
        <p>Parágrafo</p>
    </body>
</html>

That is, another error is in your structural organization of HTML. And last but not least, the ; at the end of echo that every php command should count when finished.

<!DOCTYPE html>
<html>
    <head>
        <title>
            <?php echo 'Rodrigo'; ?>
        </title>
    </head>
    <body>
    </body>
</html>

1

The code needs to be .php , probably is .html

and a semicolon is used at the end of the command as good practice, thus:

<title><?php echo 'Rodrigo'; ?></title>

0

Your code has an unwise part if you want to add more code after this

Originally you have:

<title><?php echo 'Rodrigo' ?></title>

But in PHP, at the end of each order / command it is necessary to add a ;. So the code stays.

<title><?php echo 'Rodrigo'; ?></title>

If that doesn’t solve the problem, check that the file ends with the extension .php and the server supports PHP.


If, after all, the problem remains, let me know.

  • @Rbz , if the developer wants to add more code after this code, you need to use ;, because the code is compiled all together!

  • But you cannot go by deduction, and still say it is a mistake. The example in the question is clear. No one wondered why the rest of the code? And you’re already worrying about code that doesn’t even exist!?

  • @Rbz, it’s called caution, and it’s good practice to add ; at the end, I will correct the "error" part, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.