9lucky Posted August 2, 2012 Share Posted August 2, 2012 I just started learning php today and so far it's been going good although I'm wondering what the best way to use php and html together is, I know that I can do it like this: <html> <?php echo "Hello" ?> </br> <? echo "World" ?> </html> Although is there a better way to do this especially when I just want to add something small like a space? I was also wondering what the best way to use a variable on multiple pages would be, I know I can use the get and post functions but if I do use them someone could easily change the variables values by editing the url, is there a more secure way to use the same variables on multiple pages? Thanks, 9lucky Quote Link to comment Share on other sites More sharing options...
id Posted August 2, 2012 Share Posted August 2, 2012 I just started learning php today and so far it's been going good although I'm wondering what the best way to use php and html together is, I know that I can do it like this: <html> <?php echo "Hello" ?> </br> <? echo "World" ?> </html> Although is there a better way to do this especially when I just want to add something small like a space? I was also wondering what the best way to use a variable on multiple pages would be, I know I can use the get and post functions but if I do use them someone could easily change the variables values by editing the url, is there a more secure way to use the same variables on multiple pages? Thanks, 9lucky Im no pro, but for me for your 1st question it suppose to be: <html> <?php echo "<br />Hello" ?> </html> In php you can add html tags inside of echo's just like my example above. For you second issue, the post method doesn't show on the url only the "get" method does. The post method is mainly used for more secure items; like usernames, passwords, etc... While the get method is normally used to display certain items... Example, if you have a forum board or something similar, then in order to display a member's profile you would do www.site.com/userid=1 for the url, and to go onto a different member then you would simply change the url to do that. While if you was to login then you would use the post method in order to keep your information secure. Again im no pro but i hopes this helps. Quote Link to comment Share on other sites More sharing options...
hakimserwa Posted August 2, 2012 Share Posted August 2, 2012 there are so many ways to mix php and html. you can use your normal html and apply php only where you need it by opening and closing php tags. <div>do your html then only when you need some php then open the php tag and olways after php code remember to close it.<?php code?></div> second way you can echo html in php code. <?php echo "hallo World"." ".$valuable; //that you may want to echo // for space inside php code you can echo "&nsb;"; or concatenate an empty string ." ". ?> for using valuables on multiple pages you need to use sessions <?php session_start() // wright at the top of every thing or at the beging of the page before any html. then store any valuable value in a session $_session['name'] = $your_valuable; ?> then on the other page where you want to use the same valuable, it is now stored in a session. start the session as described above again on the other page. and you can now store the session value back to your original valuable or you can use the session valuable. $your_valuable = $session['name']; and start using you valuable. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 2, 2012 Share Posted August 2, 2012 The absolute best way to use PHP and HTML together, is to deal with all of the PHP processing before you send anything to the browser. This includes starting on the HTML output. Ideally the only PHP code you should use inside your HTML code is "echo", or similar, and as few of of those as logic dictates. (That means only one echo per piece of generated content, and not stuffing all of the HTML into one echo statement.) This will not only help you maintain readability, but also give you the most flexibility in controlling the resulting output. As you can't manipulate anything that's already sent to the browser, and headers get sent when the first byte of "content" is sent to the browser. If you don't, you'll get the dreaded "headers already sent" error. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 2, 2012 Share Posted August 2, 2012 To further separate your business and your presentation, look into templating engines like Smarty. Quote Link to comment Share on other sites More sharing options...
Berre Posted August 2, 2012 Share Posted August 2, 2012 The "best" way to reuse code would probably be a full object oriented framework. But that's way into the future, so for now just know that whatever you do it will be problematic for you. Everything will get messy, and you'll hate it. But that's how you'll learn A simple way to reuse code is with functions. A function is basically some code you call whenever you need an algorithm run. function get_highest($item_a, $item_b) { if (!is_numeric($item_a) || !is_numeric($item_b)) { return false; } return $item_a > $item_b ? $item_a : item_b; } Example use: $moms_age = 50; $dads_age = 53; echo "The highest age is " . get_highest($moms_age, $dads_age); This is essentially what you do when using strpos() and similar. To make it possible to use the functions on multiple pages you can sort them in their own files, for example functions.php. Then, you just write this in each document: require_once "functions.php"; By including pages (require_once or include_once to avoid the page being included multiple times) you can use the same variables as well, like your database connection. Quote Link to comment Share on other sites More sharing options...
9lucky Posted August 2, 2012 Author Share Posted August 2, 2012 Thanks for all the help guys I'll mark this topic as solved. 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.