#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    45
    Rep Power
    47

    Syntax to determine if GET variable holds a value


    I'm working on a php based blog, basically to teach myself PHP. On my homepage I have a short synapsis of two "blog articles", with links to the entire article. The URL of the links are like so:

    PHP Code:
    http://localhost/website/blog_post.php?id=$id 
    which brings up the page and the article corresponding to the id.

    On that page, I have a comment forum, which is extremely primitive but as I said I'm teaching myself PHP. What I want to have is, if someone posts a comment, it generates a random number associated with that comment (which it does).

    But if someone is "replying" to another comment, when they click the Reply button the url then becomes:

    PHP Code:
    http://localhost/website/blog_post.php?id=$id&blogid=$blogid 
    The problem is if I put code in that page looking for the
    PHP Code:
    $_GET['blogid] 
    when there is none, I get an error message about an undefined variable. So my convoluted question is, what is the syntax to check if the GET variable blogid exists in the url or not?

    In other words, the correct way to say
    PHP Code:
    "If $_GET['blogid] exists then $blogid == $_GET['blogid]. else $blogid = rand(1,1000000)" 
    I know how to lay out an if/then statement, it's the checking of the existence of the GET variable that's throwing me.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,117
    Rep Power
    9398
    isset lets you check that the value exists at all. More useful to you would be empty which not only checks if the value doesn't exist but also if it's empty: in this case that means not an empty string (?blogid=) or the number 0 (?blogid=0).

    PHP Code:
    if (!empty($_GET["blogid"])) { 
    You should also do some additional checks like whether it's a number, and of course whether the thing with that ID exists.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    45
    Rep Power
    47
    Originally Posted by requinix
    isset lets you check that the value exists at all. More useful to you would be empty which not only checks if the value doesn't exist but also if it's empty: in this case that means not an empty string (?blogid=) or the number 0 (?blogid=0).

    PHP Code:
    if (!empty($_GET["blogid"])) { 
    You should also do some additional checks like whether it's a number, and of course whether the thing with that ID exists.
    I figured someone would bring up isset. I had tried that, it worked in that it removed the error message, but later it gave me a syntax error in my editor when I used it in an if statement.

    I will try your other suggestions and report back.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    Originally Posted by mystic7
    I figured someone would bring up isset. I had tried that, it worked in that it removed the error message, but later it gave me a syntax error in my editor when I used it in an if statement.
    Then tell us that! Show us your code and the exact error message you get.

    What's the point of pretending you don't know isset() when it's obvious that everybody will suggest that function first?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    45
    Rep Power
    47
    Originally Posted by Jacques1
    Hi,



    Then tell us that! Show us your code and the exact error message you get.

    What's the point of pretending you don't know isset() when it's obvious that everybody will suggest that function first?
    Wow, you don't like me. I can tell

    I didn't know isset. After I posted my question I found it, and tried it. Like I said, it made the error message disappear but then when I tried it again in an if statement (which was how it was presented in the tutorial I found) it threw a syntax error.

    Please don't yell at me. I'm sensitive.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    This is the internet, you're going to get yelled at.

    isset() isn't a syntax error, your code was incorrect. Going back to what jacques said: show the error. Show the code. If you're sensitive to be scolded, at least follow the instructions which were virtual-yelled at you. What was the code that had bad syntax, and what was the error? isset() is your answer.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    45
    Rep Power
    47
    Originally Posted by ManiacDan
    This is the internet, you're going to get yelled at.

    isset() isn't a syntax error, your code was incorrect. Going back to what jacques said: show the error. Show the code. If you're sensitive to be scolded, at least follow the instructions which were virtual-yelled at you. What was the code that had bad syntax, and what was the error? isset() is your answer.
    Let me tell ya something, ManiacDan. I've been booted off more message boards and had to change identities more times than I can count. I've been "yelled at" for years. You could do a lot worse to me and it won't break a sweat. One more thing you should know about me. I don't use smiley faces or //sarc tags to indicate sarcasm. If you all don't like my personality I suggest you revoke my membership now. But I was a member (and still am) of the ASP section of devshed for 7 years now and they loved me over there. Now...

    I can't give the error code because I fixed it another way and it no longer appears, but it had something to do with variable not being set. As for the syntax error, it happened as soon as I added "if" to my code:

    PHP Code:
    if isset($_GET['blogid'])
    {
    $blogid $_GET['blogid']);
    }
    else
    {
    $blogid rand(1,1000000);

    Something like that. btw, I just pasted that code into my script, and it says I have a syntax error on each of the first three lines.

    This clears the syntax errors but it's not the way it was presented in the tutorial, or here:

    PHP Code:
    if (isset($_GET['blogid'])) 

    $blogid = ($_GET['blogid']); 

    else 
    {
    $blogid rand(1,1000000); 

    That also is not throwing the error. I've still got some tweaking to do, though because I'm not getting the result I was hoping for.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0
    In your first example there are two reasons it didn't work:
    Yes, you do need parenthesis to contain the conditions of your "if" statements.
    And, on line 3, you have a ")" but with no matching "(".

    In your second example (the one that you say works),
    use
    PHP Code:
    {
    $blogid $_GET['blogid'];  

    No need for the parenthesis there.

    It looks like what we have here is simply a syntax learning curve. No problem. I'm still there too.
    I gotta tell ya, with the cumulative knowledge base that these guys have, and are willing to share, they can yell at me all they want. What I can gain from them is well worth it's weight.

    Comments on this post

    • DonR agrees : I'd take the "yelling", too ;)
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Ok I have no idea what you're talking about, claiming to be sensitive and asking not to be yelled at and then claiming to be a die-hard veteran who requires respect and isn't sarcastic.

    Unless you mean you were sarcastic when you said "please don't yell at me" and somehow you were expecting an entire community to assume you were meaning the opposite of what you were actually saying. That makes a lot of sense, given that nobody knows you. (Since you don't know me, the previous sentence was sarcastic)

    Regardless, the IF syntax is:
    if ( <CONDITION> ) {

    }

    You must open the parens after the word IF, and all other control structures in PHP which require a condition (elseif, else, else if, while, for, foreach, etc.) but NOT for the control structures which do not require conditionals (try, catch, do, etc.)

    The parens you were erroneously using can sometimes have special meaning in PHP and it's recommended you don't put them around single variables unless you know why you're doing that.

    If the tutorial matches code which throws basic syntax errors, you should stop using it.
    Last edited by ManiacDan; October 8th, 2013 at 03:21 PM.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    45
    Rep Power
    47
    Originally Posted by ManiacDan
    Ok I have no idea what you're talking about, claiming to be sensitive and asking not to be yelled at and then claiming to be a die-hard veteran who requires respect and isn't sarcastic.

    Unless you mean you were sarcastic when you said "please don't yell at me" and somehow you were expecting an entire community to assume you were meaning the opposite of what you were actually saying. That makes a lot of sense, given that nobody knows you. (Since you don't know me, the previous sentence was sarcastic)

    Regardless, the IF syntax is:
    if ( <CONDITION> ) {

    }

    You must open the parens after the word IF, and all other control structures in PHP which require a condition (elseif, else, else if, while, for, foreach, etc.) but NOT for the control structures which do not require conditionals (try, catch, do, etc.)

    The parens you were erroneously using can sometimes have special meaning in PHP and it's recommended you don't put them around single variables unless you know why you're doing that.

    If the tutorial matches code which throws basic syntax errors, you should stop using it.
    Wow, you can read me like a book //facetiousness

    Seriously the only reason I was a tad po'd was that Jacque assumed I knew about isset and was just yanking you all. I know the guy is a super genius with PHP and all but cut me SOME slack in my first two posts ever. I can play guitar like 60's era Clapton but I'm not gonna yell at ya the first time you ask me for the chords to Crossroads and then tell me "Yeah, I knew about the D7 chord"...

    Does THAT clear things up? And the guy who is also just starting out, I hear ya. I do appreciate what these guys have to offer. Just don't treat me like an idiot because YOU misread my post. Cheers!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    45
    Rep Power
    47
    Originally Posted by c-cat
    In your second example (the one that you say works),
    use
    PHP Code:
    {
    $blogid $_GET['blogid'];  

    No need for the parenthesis there.
    They didn't throw an error so I assume they were merely superfluous rather than wrong syntax, right?
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I'm sorry for the harsh reply, mystic7. But don't you think you're overreacting a bit?

    The reason why we're so picky about getting info is because many people (not you) don't understand that we need to know what's going on before we can help them. They'll start with something like "My website don't work. Plz help!!!", and then it goes back and forth for hours until we finally have the code and a concrete error description. It's just annoying, and it makes you suspicious after a while.

    Anyway, I guess this was just a misunderstanding. I have no problem with you. Why should I?



    Originally Posted by mystic7
    They didn't throw an error so I assume they were merely superfluous rather than wrong syntax, right?
    Right.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Superfluous in this particular case, incorrect syntax in other cases. Putting parentheses around something can remove its by-reference status.

    I'll close this thread before it turns into an argument about who is overreacting and how we should treat people who may be well loved by other people and get sarcastic and confrontational in their first couple posts.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo