November 11, 201015 yr I am new to php and have generated something that works but I am sure can be tidied up! A variable is passed from page 1 to page 2 and then it needs to to be passed on again to page 3 but only if set on page 1. therefore as part of the link to page 3: <?php if(isset ($_GET['item'])) {echo "&item=";}{echo $_GET['item'];} ?> Surely the 2 echos can be joined? There is probably even a better way to achieve this as well but I would still like an answer to the above if possible? Thanks
November 11, 201015 yr Author Thanks for that. Yes I can see that might be much better than passing variables in the url between my pages and I think will be the way to go in the long term as the site grows. For the moment however, I don't have the time to learn how to use sessions so will make it part of my to-do list :-) What is the best way to tidy up the code I already have?............just for now..........
November 11, 201015 yr Author <?php if(isset ($_GET['item'])) echo "&item=".$_GET['item']; ?> Ah, a full stop is all I was missing...easy when you know how. Thanks :-) Should the { } be around the echo statement or not? Also, does it matter if I use ' or " around 'item' and '&item=' thanks again
November 11, 201015 yr Since it's only one line, you don't need the braces. I do actually use them for one line statements, but you asked for it to be cleaned up. The following are the same if(...) { // Do something here} if(...) // Do something here When you want to do more than one thing based on a condition, you put them in curly braces if(...) { // Do something here // Do something else here // Do something else here too } As for the single/double quotes, I generally don't use double quotes unless necessary such as writing SQL queries where you need single quotes in them. There's no real right or wrong answer for them, but single quotes used to be parsed quicker since you can have variables in double quoted strings (I don't recommend this personally, but many people are happy to do it) so it takes extra processing power to find those variables, and convert them. That said, I've heard that in the latest versions of php these differences are almost unnoticable
November 11, 201015 yr Author wonderful, thanks very much for taking the time to be so informative. All makes sense now :-) I think I'll keep the {} so I don't forget when I come back to it at a later stage. thanks again.
Create an account or sign in to comment