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.
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];
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.
if ($_GET["site"])
must be
if (isset($_GET["site"]))
And also for the $title things you should use isset:
if (isset($title[1])) [...]
You can also use a solution that encompasses isset's error handling, by using !empty
if(!empty($_GET['site']))
Related
In an exercise I did :
<?php
$initial = '555';
$a = octdec($initial);
echo $a . "\n";
$b = deg2rad($a) . "\n";
echo $b;
$c = cos($b). "\n"; *(line 9)*
echo $c;
and it does show the correct answer, as well as an error:
365
6.3704517697793
0.99619469809175
PHP Notice: A non well formed numeric value encountered in /home/ccuser/workspace/hg2pmf/index.php on line 9
if I change line 9 with : $c = cos(6.3704517697793). "\n"; the message of error disappear. Why can't I use the $ inside the cos() when in deg2rad() works perfectly?
The problem is that on this line...
$b = deg2rad($a) . "\n";
You are adding the new line onto the end of the calculated value.
So remove it and your value will be the number you expect...
$b = deg2rad($a);
As a help, I use
declare(strict_types=1);
and I get the error...
Fatal error: Uncaught TypeError: cos(): Argument #1 ($num) must be of
type float, string given in
which shows how it's changed the value from a number to a string.
This question already has an answer here:
A non-numeric value encountered
(1 answer)
Closed 1 year ago.
I don't know what I changed but suddenly I'm getting an error on the code that worked before:
Warning: A non-numeric value encountered in C:\xampp\htdocs.apps\DeSplinterRekenen\Code\addAnswer.php on line 32
These are line 31, 32 and 33, I put the 1 in a seperate line to point out that the error is actually pointed on the 1:
header("Location: assignment.php?assign=" . $_SESSION['activeAssign'] . "&question=" . $_SESSION['activeQuestion'] +
1
);
Why is php saying a literal 1 is non-numeric?
I've tried this and it worked, but it just seems weird I have to do it this way:
$var = $_SESSION['activeQuestion'] + 1;
header("Location: assignment.php?assign=" . $_SESSION['activeAssign'] . "&question=" . $var);
exit();
+ and . have the same precedence, and they're left-associative. So ($a . $b + $c) is equivalent to ($a . $b) + $c, not $a . ($b + $c).
In your case, the concatenations don't produce a number, so when you try to add 1 to it, you get an error. You can use parentheses to specify the desired grouping.
header("Location: assignment.php?assign=" . $_SESSION['activeAssign'] . "&question=" . ($_SESSION['activeQuestion'] + 1));
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 2 years ago.
I'm trying to make a couple functions to get the amount of hours of light and dark based on sunrise and sunset times. Before moving my code into functions everything was working fine, but now it is unable to define my variables despite the fact that those variables are printing perfectly outside of the function. I've looked through a few answers on here, but every one seems to suggest using either global or by passing the variables as arguments. Using global causes it to not work at all, and passing the variables as arguments didn't change anything. I have two files, one with my functions, and one to print them.
The error is this:
Notice: Undefined variable: sunset in C:\xampp\htdocs\test.php on line 13
Notice: Undefined variable: sunrise in C:\xampp\htdocs\test.php on line 13
My functions are as follows:
<html>
<body>
<?php
$lat = 38.7;
$long = -75;
$sunrise = (date_sunrise(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5)); //get local time, offset -5 hours from gmt
$sunset = (date_sunset(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5)); //get local time, offset -5 hours from gmt
function light(){ //passing the variables gave me the same result
(strtotime($sunset) - strtotime($sunrise))/3600; //$sunset and $sunrise are undefined
}
function dark(){
24 - light(); //same issues as light()
}
?>
</body>
</html>
And this is what I'm using to print everything:
<?php
require "test.php"; //name of function page
echo "Date: " . date("D M j Y");
echo "<br>Latitude: " . $lat;
echo "<br>Longitude: " . $long . "<br>";
//these variables are the ones that are undefined in my function, but they're printing just fine here
echo "<br>Sunrise Time: " . $sunrise;
echo "<br>Sunset Time: " . $sunset . "<br>";
echo "<br>Hours of Light: " . (round(light(),2)); //round to two decimals
echo "<br>Hours of Dark: " . (round(dark(),2)); //round to two decimals
?>
I'm kind of at a loss here, I was thinking that perhaps it was because I was using pre-built functions in my own, but I had thought for sure that it was possible to do this. I've been trying to troubleshoot this for the past day or so with no progress, so any help would be hugely appreciated. Thank you!
You need to read more on variable scope. You cannot access the outside variables from inside the function.
Instead modify your function like this:
<?php
$lat = 38.7;
$long = -75;
$sunrise = (date_sunrise(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5)); //get local time, offset -5 hours from gmt
$sunset = (date_sunset(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5)); //get local time, offset -5 hours from gmt
function light($sunset, $sunrise){ //passing the variables gave me the same result
(strtotime($sunset) - strtotime($sunrise))/3600;
}
$light = light($sunset, $sunrise)
function dark($light){
24 - $light;
}
?>
I was actually able to fix this by reading through here https://www.php.net/manual/en/language.variables.scope.php
turns out that what I needed to do was define the variables as global in the function itself, not outside of it!
I have the following error;
Note: Array to string conversion in [file_path] on line 919
which relates to this line of code where I'm trying to assign this string as a value in an array
$contents[11] = "$hours:$minutes:$seconds\n$ca_1[remaining_time]\n$h:$m:$s";
Why am I getting this error, and how do I resolve it?
It's a bad practice to interpolate string this way because it makes the code very difficult to read, so you should rather use "{$h}" instead of "$h".
As Terminus mentioned in comments, depending on the PHP version,
echo "$ca_1[remaining_time]"
Does not necessarily give a
PHP Notice: Use of undefined constant
Like echo $ca_1[remaining_time] would. But since that didn't work for you, you'd better quote that like ['remaining_time'].
You might also find some interesting things on this topic here.
Second, use curvy braces to explicitly tell what you want to insert:
$contents[11] = "$hours:$minutes:$seconds\n{$ca_1['remaining_time']}\n$h:$m:$s";
This really improves readability.
Try:
$contents[11] = $hours . ':' . $minutes . ':' . $seconds + "\n" . $ca_1['remaining_time'] . "\n " . $h . ':' . $m . ':' . $s";
If this still fails, check your variables. Maybe one of them is an array!?
I have the following string:
CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550
which in the code below corresponds to $track_matches[0][0].
The only constant-length field is the first (CAE33D8E804334D5B490EA273F36830A9849ACDF), which is 40 characters long. I am trying to get the values xx and yy which are an unknown length and value along with the rest of the column.
So I am trying something like this:
$seperator= '|';
$end_seed= strpos($track_matches[0][0], $seperator, 41 );
$seeders[$i] = substr($track_matches[0][0], 41, $end_seed - 41);
$end_leech= strpos($track_matches[0][0], $seperator, $end_seed +1 );
echo "end_seed" . $end_seed . " end_leach: " . $end_leech;
$leechers[$i] = substr($track_matches[0][0], $end_seed +1, $end_leech - $end_seed - 1);
The problem I am getting is the line $end_leech= doesn't seem to work properly (and doesn't recognize the $seperator) and retuns the entire line ($track_matches[0][0]) as it's value when echo'd while $end_seed returns the proper value. ... so what's going on why is this happening? howw do i fix it?
try:
$temp = explode("|", $track_matches[0][0]);
That will return an array and you can then reference the vars as $temp[1] (xx) and $temp[2] (yy)
try :
$myString="CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550";
$splitString=explode('|',$myString);
$xx=$splitString[1];
$yy=$splitString[2];
of course you can replicate manually with strpos, substr etc but will take more effort