futrose Posted January 25, 2011 Share Posted January 25, 2011 If I am trying to say something like: if a variable is not empty, would I do this? if ($id != "") or does the "" mean ANYTHING instead of NOTHING? The reason I'm asking is because my home.php page does not have an id associated with it when a person first comes to the site, but when a user clicks on the "home" links then the id for the home page is set to 13. So I get an error for some of the lines when a user first visits the page because all the info for the page is based on the id (which isn't set yet). home.php is pointed to by my controller index.php script which so far only says <?php include $_SERVER['DOCUMENT_ROOT'] . './includes/magicquotes.inc.php'; include $_SERVER['DOCUMENT_ROOT'] . './includes/db.inc.php'; include 'home.php'; ?> is there a way to make the include 'home.php'; line point to something like include 'home.php?id=13' without having to have an actual file called home.php?id=13 on the server? Thanks Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 25, 2011 Share Posted January 25, 2011 "" means nothing, wildcard only works with regular expressions I believe. Personally, while != "" works, I think it looks sloppy, and it's a waste when you could use the empty function. if (!empty($id)) { // Do if ID isn't empty } As for the includes, it'll work if you include the entire URL. See below from php.net on include: // Works. include 'http://www.example.com/file.php?foo=1&bar=2'; However there's probably a better way, not sure at the moment since I don't know where id=13 is getting pulled from. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 25, 2011 Share Posted January 25, 2011 I don't see any problems with using if($id != ""); that's what I typically use. Note that if you chose to use the empty() function make sure you're aware that $id=0 will be considered empty. line point to something like include 'home.php?id=13' without having to have an actual file called home.php?id=13 on the server? It shouldn't matter, you'll just need to have a page called "home.php". The id part just passes a variable. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 25, 2011 Share Posted January 25, 2011 The reason I'm asking is because my home.php page does not have an id associated with it when a person first comes to the site, but when a user clicks on the "home" links then the id for the home page is set to 13. So I get an error for some of the lines when a user first visits the page because all the info for the page is based on the id (which isn't set yet). Don't use $var!="", or empty. Instead you should be using isset(). While the other two methods will work, they are not technically correct. If you were to turn error reporting on to display all errors and warning you would see that using $var!="" or empty($var) will both generate errors because you are trying to compare a variable to something and the variable doesn't exist. I typically do something such as this: $id = isset($_GET['id']) ? trim($_GET['id']) : false; Then if($id!==false) { //Id was set do something with it } else { //Id was not set, do something else } Note the use of !== with the double equal signs. If I just used ! and one of the possible IDs is '0' that would incorrectly resolve for false for that condition. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 25, 2011 Share Posted January 25, 2011 ...will both generate errors because you are trying to compare a variable to something and the variable doesn't exist. I'm not sure what you mean. Doesn't this: $id = $_GET['id']; declare the variable. If the variable exists, why would if($id != "") generate an error? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 25, 2011 Share Posted January 25, 2011 Don't use $var!="", or empty. Instead you should be using isset(). While the other two methods will work, they are not technically correct. If you were to turn error reporting on to display all errors and warning you would see that using $var!="" or empty($var) will both generate errors because you are trying to compare a variable to something and the variable doesn't exist. According to the manual, empty() will not generate an error if the variable is not set (and I have never seen it generate one): empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 25, 2011 Share Posted January 25, 2011 I'm not sure what you mean. Doesn't this: $id = $_GET['id']; declare the variable. Yes, but that line would produce the warning "Notice: Undefined index: id ". That is why I set values using the ternary operator with an isset condition a sshown above. ... empty() will not generate an error if the variable is not set... Ah, right you are. But, as stated by the OP himself the problem was encountered when the GET value isn't set. So, the test should logically be done on whether the GET value is set. That is why I would set the local variable using isset() as either the passed value or as boolean false. Then test the logical value of the variable. Quote Link to comment Share on other sites More sharing options...
futrose Posted January 26, 2011 Author Share Posted January 26, 2011 Thanks for the info. I'll try and make some adjustments to the code I have using your suggestions and see if I can get it to work. I know there has to be and easier way to build this website but I don't know what it is yet. 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.