Jump to content

[SOLVED] Help to understand echo


G_Dem

Recommended Posts

Hi G_Dem,

 

Yes, you can do that, for example:

 

<div class="someclass"><a href="<?php echo 'Hello World!'; ?>">Hello World!</a>

 

Make sure you rename your HTML page to end with a PHP extension, i.e. index.html would need to become index.php.  You only need to do this if the page contains PHP code.

 

Hope this helps.

Link to comment
Share on other sites

Thanks. Is that the normal procedure to enter variables into the html code?

 

Also can I open a database, create a recordset and set variables at the beginning of the php file and then use those variables later on in a totally separate <php ...?> later on in the file?

Link to comment
Share on other sites

Some people prefer to output most of the code with PHP, e.g.

 

<?php
$var = 'Hello World!';
echo '<div class="someclass"><a href="' . $var . '">Hello World!</a>';
?>

 

while others prefer what Bricktop demonstrated. I tend to prefer the former.

 

To answer your second question: Yes.

Link to comment
Share on other sites

Thanks. Is that the normal procedure to enter variables into the html code?

 

Also can I open a database, create a recordset and set variables at the beginning of the php file and then use those variables later on in a totally separate <php ...?> later on in the file?

 

To your first question, I would state that is the "preferred" method (but, that is debatable). As for the second question the answer is yes as well.

 

Just about any PHP programme worth his salt is going to tell you that you should separate the logic (PHP) and the presentation (HTM/CSS) of your pages. It makes things MUCH easier to debug and to make changes later. It also makes development much easier as you don't have to mentally interpret the PHP code and the HTML together. For instance, taking the example Bricktop posted, let's say the URL of the hyperlink can be dynamic based upon time of day.

 

Some people might write it like this

<div class="someclass">
<?php
$hour = date('G');
if($hour<6)
{
  echo "<a href="night.php">Hello World!</a>";
}
elseif ($hour<12)
{
  echo "<a href="morning.php">Hello World!</a>";
}
elseif ($hour<18)
{
  echo "<a href="afternoon.php">Hello World!</a>";
}
elseif ($hour<24)
{
  echo "<a href="evening.php">Hello World!</a>";
}
?>
</div>

 

However, I believe that this would be a better approach:

 

At top of page or in separate file

<?php
$hour = date('G');
if($hour<6)
{
  $helloWorldURL = 'night.php';
}
elseif ($hour<12)
{
  $helloWorldURL = 'morning.php';
}
elseif ($hour<18)
{
  $helloWorldURL = 'afternoon.php';
}
elseif ($hour<24)
{
  $helloWorldURL = 'evening.php';
}
?>

 

At bottom of page where HTML is declared or in included file

<div class="someclass">
<a href="<?php echo $helloWorldURL; ?>">Hello World!</a>
</div>

 

A quick look at the presentation (HTML) shows exactly what the page should look like. If there is a problem with the URL you know where to look. And, if needed you can quickly debug the script to determine if you have an HTML or PHP problem. By combining the HTML and PHP it can sometimes be difficult to determine where the problem really is. Plus, you have the flexibility of changing your design very easily without risking regression in your logic.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.