Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Basic PHP tidy-up please

Featured Replies

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

  • 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..........

  • 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

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

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.