Jump to content

explanation needed


futrose

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.