Welcome Guest, Not a member yet? Register   Sign In
Checking undefined variable
#1

[eluser]John5788[/eluser]
Hello,

I am new to CodeIgniter and I am trying to catch an undefined variable. I cannot catch it with isset($var), because once I try it when $var hasn't been defined, CodeIgniter goes ahead and catches it for my with an error message.

I like the error message, but I would like to catch this specific case myself.

I am trying to accomplish injecting a $title variable into my <title> tag if $title is defined. If it is not defined, I will use a default title tag.

How can I catch this case gracefully without any errors (also without disabling errors)?
#2

[eluser]InsiteFX[/eluser]
CodeIgniter has its own Exception Class, you would need to extend the Exception Class to catch your own errors.

InsiteFX
#3

[eluser]Talifhani[/eluser]
Or do something like this

$title = empty($title) ? "I am a default Title" : $title;

Should work. Good luck
#4

[eluser]John5788[/eluser]
[quote author="Talifhani" date="1299678455"]Or do something like this

$title = empty($title) ? "I am a default Title" : $title;

Should work. Good luck[/quote]

doesnt work, wish it was that easy.
#5

[eluser]Talifhani[/eluser]
Notice im not using isset() im using empty(), empty() will not throw an error if a variable is not defined. Im sure that should work. :-)

The only downside is that it will return the true if you have a $title = 0, which is unlikely, I think. I hope.
#6

[eluser]John5788[/eluser]
[quote author="Talifhani" date="1299679163"]Notice im not using isset() im using empty(), empty() will not throw an error if a variable is not defined. Im sure that should work. :-)

The only downside is that it will return the true if you have a $title = 0, which is unlikely, I think. I hope.[/quote]

I did notice the difference and I did try it. It does not work.
#7

[eluser]steelaz[/eluser]
You're missing something, isset() and empty() will never throw "undefined" error. Check the file name and line number in your error message and make sure you're not trying to access $var on that line before you check if it exists.
#8

[eluser]John5788[/eluser]
[quote author="steelaz" date="1299681245"]You're missing something, isset() and empty() will never throw "undefined" error. Check the file name and line number in your error message and make sure you're not trying to access $var on that line before you check if it exists.[/quote]

Interesting, magically working now. I reverted back to isset() to show exact errors and code, but now it doesn't throw the exception anymore. Weird.




Theme © iAndrew 2016 - Forum software by © MyBB