tags:

views:

519

answers:

6

I keep getting this as a warning I want to avoid getting this warning when it is undefined without turning warnings off

here is the context

  $url_items = array("foo");
  $article_id = db_escape($url_items[1]);
  $article = get_article($article_id);

  function get_article($article_id = NULL) {.....}
A: 

put this before your code:

error_reporting(E_ALL ^ E_NOTICE);

PHP Manual: error_reporting

Iraklis
That's wrong at all, but especially in this very case. Why not E_ALL?
Col. Shrapnel
because E_ALL will still display the errors?
Iraklis
Ahaha, I thought you were going to help to find error, not to hide it :) It just didn't came to my mind. What a ridiculous answer from a developer.
Col. Shrapnel
I know how to turn on and off error reporting the point is to avoid the warning
mcgrailm
@mmcgrail So, turn it on, read the error message and then repair it. Not a big deal, eh?
Col. Shrapnel
Then maybe the OP should rephrase the question from "but I don't care if its not set so how do I get rid of the warning" to "How do I fix my code".
Iraklis
@Iraklis It whill be same dumb question. Errors must be repaired, not gagged. OMG I have to say such basic things on the stacroverflow. unbelievable.
Col. Shrapnel
start believing.
Iraklis
A: 

i think this would be a nicer way maybe there is a better way but i think just hiding the warning is the wrong way...

$article_id = db_escape($url_items[1]);
if(empty($article_id)){
    $article_id = null;
}

edit corrected the code

Nexum
The argument to empty **must** be a variable, it doesn't work on expressions.
R. Bemrose
this actually gives me a parse error
mcgrailm
Powerlord is right, as it says in the docs: *Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).*
Felix Kling
sry just wrote this on the fly... wanted it to stay in one line of code...
Nexum
A: 

I don't know if this is related to the error, but you should know that PHP indexes arrays starting with 0, so the second line should be

$article_id = db_escape($url_items[0]);

Also, it is probably a typo, but the first line should be

var $url_items = array("foo");
oli
I know they start at zero the point is that $url_items[1];has no valueand I don't need var in front of a variable and I'm not sure but I think that might be a problem if I did put it there
mcgrailm
the var will cause errors
mcgrailm
+1  A: 

You don't say exactly which line is causing the error, but you ought to use isset for any variables you are not sure exist. For example:

$url_items = array("foo");
if ( isset($url_items[1]) )
{
    $article_id = db_escape($url_items[1]);
    $article = get_article($article_id);
}

function get_article($article_id = NULL) {.....}

You'll also want to check the content of the db_escape method, in case that is also doing something with an undefined variable.


One other way around the problem is to pass the variable to the function by reference using &:

function get_article(&$article_id) {
    if ( $article_id == null ) {
        // handle null case here
    }
    else {
        // get the article
    }
}
DisgruntledGoat
ahh yes but I still need to make the call to get_article even if $article_id is not set
mcgrailm
@mmcgrail: yeah I gave your code a try but did not get any warnings. I did get `Notice: Undefined offset: 1` though. The only time I get "Undefined variable" is if I remove the line that sets `$article_id`. Are you sure your code is the same as you posted above? Posting the content of `db_escape()` would help too.
DisgruntledGoat
yes your right I get that one too and I still fail to see how db_escape has anything to do with it. but that does lead me to the answer
mcgrailm
+2  A: 

I'm thinking that the easiest way to solve it is like this:

$url_items = array("foo");
$article = empty($url_items[1]) ? get_article() : get_article(db_escape($url_items[1]));

function get_article($article_id = NULL) {.....}

This should works because you give $article_id a default value in the function. However, you could just as easily change the middle ternary part to null if you don't want to execute at all if there is no $article_id.

Edit: If you have an article_id 0, you may want to change empty to !isset
Edit 2: Modified to avoid the undefined offset warning.

R. Bemrose
A: 

so it was pretty simple after all that and yes maybe I could have phrased my question better, but it still bothers me that there is not a simpler way to do this

    if (isset($url_items[1])){
        $article_id = db_escape($url_items[1]);
    }else{
        $article_id = null;
    }
    $article = get_article($article_id);
mcgrailm