G_Dem Posted September 21, 2009 Share Posted September 21, 2009 Hi I'm a complete noob at php. I was wondering if you have to echo a full html tag and code, or can you put <php 'some code' ?> half way through html code; like <a href= <php 'some code' ?>> Quote Link to comment Share on other sites More sharing options...
Bricktop Posted September 21, 2009 Share Posted September 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
G_Dem Posted September 21, 2009 Author Share Posted September 21, 2009 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? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 21, 2009 Share Posted September 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Bricktop Posted September 21, 2009 Share Posted September 21, 2009 Hi G_Dem, I normally output the HTML via PHP, so for example: $content .= '<div class="someclass">'; $content .= '<p>'.$variable.'</p>'; $content .= '</div>'; echo $content; but the method you're using isn't uncommon. In answer to your second question, yes! Quote Link to comment Share on other sites More sharing options...
G_Dem Posted September 21, 2009 Author Share Posted September 21, 2009 Thanks to both of you. Thats been a big help to get me started. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 21, 2009 Share Posted September 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
G_Dem Posted September 21, 2009 Author Share Posted September 21, 2009 Thanks. Got plenty to get me on my way now. I'll do my database connection, my recordsets and set my variables separately from the presentation part of the html. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.