Reputation: 1216
I know that you're supposed to define PHP variables so that you don't clog up your error log with undefined variables, but my logs are still getting filled with unnecessary information.
For instance...
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined index: site in C:\Sites\FLCBranson.org\freedownloads.php on line 34
My PHP code has $site
defined, but I do want to have option to override it...
// it's a good idea to define the variable first and then make changes as necessary (that way you don't fill up your logs with worthless errors)
$site = "flc";
// overrides the domain (useful for IP addresses)
if ($_GET["site"]) $site = $_GET["site"];
So, I have a lot of that type of issue. Then I have a bunch of these pesky errors...
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined offset: 3 in C:\Sites\FLCBranson.org\listseries.php on line 264
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined offset: 4 in C:\Sites\FLCBranson.org\listseries.php on line 265
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined offset: 5 in C:\Sites\FLCBranson.org\listseries.php on line 266
I have an array that is populated with various bits of content. If something is in one of the slots, then I want to do something with it...
// explode() takes a string of text ($item->title in this case) and creates an array comprised of parts of the text separated by the separator (- in this case)
$title = explode(" - ", $sermontitle);
// sets the sermon title variable
$sermontitle = $title[0];
if ($title[1]) $sermontitle = $sermontitle . " - " . $title[1];
if ($title[2]) $sermontitle = $sermontitle . "<br>" . $title[2];
if ($title[3]) $sermontitle = $sermontitle . " - " . $title[3];
if ($title[4]) $sermontitle = $sermontitle . " - " . $title[4];
if ($title[5]) $sermontitle = $sermontitle . " - " . $title[5];
So, what am I doing incorrectly? I define my variable. Then I only make changes to the variable if certain conditions are met. I thought that was the appropriate way of doing it.
Edit...
I found another odd instance...
[28-Jan-2013 20:07:05 UTC] PHP Notice: Undefined variable: broadcast in C:\Sites\FLCBranson.org\flconlineservices.php on line 242
It seems that if (file_exists($golive) || ($broadcast == "live")
isn't enough. Do I need to do if (file_exists($golive) || (isset($broadcast) && $broadcast == "live"))
? That seems like a lot of code to perform a simple comparison.
Edit 2...
So, I'm starting to understand why isset()
is required, but here's something that I don't get. I have some code pulling information from a database and I have if ($row["Sarasota"])
but the error log doesn't show a single thing for that. Why wouldn't isset()
be required there if it is required for if ($title[5])
? The only difference I can see is the quoted word "Sarasota" as opposed to the unquoted numeric 5.
Upvotes: 0
Views: 378
Reputation: 6752
You can also use a solution that encompasses isset's error handling, by using !empty
if(!empty($_GET['site']))
Upvotes: 1
Reputation: 57703
A common way to write a default value is by using the ternary operator ? :
like so:
$site = isset($_GET["site"]) ? $_GET["site"] : null; // null is the default value
For arrays I would recommend using the count
function rather than isset
. This reinforces the reader's understanding that you're dealing with an array.
isset
is more comment used in associative arrays (like $_GET
) so you might expect other keys that are not necessarily numeric.
Upvotes: 3
Reputation: 5084
if ($_GET["site"])
must be
if (isset($_GET["site"]))
And also for the $title things you should use isset:
if (isset($title[1])) [...]
Upvotes: 2
Reputation: 22756
Use isset to avoid these notice:
if (isset($title[1]) && $title[1]) $sermontitle = $sermontitle . " - " . $title[1];
if (isset($title[2]) && $title[2]) $sermontitle = $sermontitle . "<br>" . $title[2];
if (isset($title[3]) && $title[3]) $sermontitle = $sermontitle . " - " . $title[3];
if (isset($title[4]) && $title[4]) $sermontitle = $sermontitle . " - " . $title[4];
if (isset($title[5]) && $title[5]) $sermontitle = $sermontitle . " - " . $title[5];
Upvotes: 3