Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:30:21+00:00 2026-06-17T23:30:21+00:00

Suppose using a isset() function, we checked whether an ID did exist or the

  • 0

Suppose using a isset() function, we checked whether an ID did exist or the user did submit an ID, before assigning it to variables or using it some place in the code that follows.

     <?php

        if(isset($_GET['Id']))
    {

     $Id=$_GET['Id'];
    }
?>

Now I understand, it’s good to do this

1)So the code as per the expected behavior i.e. does what it’s expected to accomplish. For Example – if id was going to be used in some query which would return a row based on that id, it’s always a good idea to provide a valid id to be sure that the query indeed returns a row as a result… This avoids the unexpected behavior that the query fails if it wasn’t provided with a valid id.

2)And as obvious, minimizing any runtime errors that our code might generate.

So, I’m looking for more relevant examples and context as to why this is always a good programming practise.

Basically more examples to help me sink in my understanding why it’s really good to do so … Many Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:30:22+00:00Added an answer on June 17, 2026 at 11:30 pm

    Think about the future.

    If you simply stated $id = $_GET['id'] and used the variable later on, your application may behave in an unexpected manner.

    Say next week you come back and write some code that uses that convinenent $id variable. From glancing up in the file you see it used elsewhere and don’t plan on it not being set. That code works when you test it quickly, but fails under real-world use. Maybe it errors out, maybe it spits out an admin page, maybe it explodes and kills puppies, who knows?

    If you’d been a bit more careful, and done something like:

    $id = 0; //default document
    if (isset($_GET['id'])
    {
      $id = $_GET['id'];
    }
    

    Your code would always have an $id to work with. Note the small addition there, I haven’t simply ensured that $_GET['id'] exists before I assign it’s value to another variable, I’ve ensured that $id /always/ has a value.

    If I was an ounce smarter, I’d probably do something like this instead: if (isset($_GET['id'] && ctype_digit($_GET['id']) && isValidId($_GET['id']))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose I let the user to write a condition using Javascript, the user can
Suppose I am using an ApplicationContext implementation in Spring. ApplicationContext is an interface in
Suppose I'm using transactional replication to replicate articles from one server to another (
Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter =
Suppose one person is using Bazaar 2.3 on Linux, and another is using Bazaar
Suppose I am building an app using current mysql database tables. I have this
Suppose I'm following TDD and using VS/ReSharper. I start out writing a test like
Suppose I created index with descending order CREATE INDEX `MyTable.MyIndex` USING BTREE ON `MyTable`
Suppose you're writing a simple database web application using Ruby and MySQL. Access to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.