I created a function to see if a lat/lon are inside a polygon. I'm getting these notice: undefined variable: xx.xx but its an array of lat/lons being passed and its the value that is undefined not the array. I'm confused help please. The line is the second / the last if statement
public function checkCoordinates($lat, $lon, $polylat, $polylon) {
$j = count($polylat)-1;//number of sides and -1 because its an array
$result = true;
for($i=0;$i<count($polylat);$i++){
if($polylat[$i]<$lat && $polylat[$j]>=$lat
|| $polylat[$j]<$lat && $polylat[$i]>=$lat){ //if the latitude at the beggining is bigger than and at the end is smaller than or vise versa
if($polylon[$i]+($lat-$polylat[$i])/($$polylat[$j]-$polylat[$i])*($polylon[$j]-$polylon[$i])<$lon){ //
$result = false;
}
}
$j=$i;
}
return $result;
}
I didn't notice the extra $, that explains it
Related
I get a website as a cURL response, and I pass it to this function. Then I itterate through the string, doing some processing. It works, but I get this error:
Notice: Uninitialized string offset: 75817 in
/var/www/html/wp-content/plugins/crg-daily/lib/CragslistRawDumpProcessor.class.php
on line 7
Here is the code:
public function rollSausage($dump){
$anchorArray = array();
$dumpLength = strlen($dump);
$skipToLetter = 1;
while($skipToLetter < $dumpLength){
$skipToLetter++;
$letter = $dump[$skipToLetter];
...
}
}
Any ideas? I think it has something to do with the type of string being submitted. It is a raw cUrl response. I'm scraping a web page.
Increment your $skipToLetter after you use it (preferably at the end of the while loop). And you might also start at 0, not 1
$skipToLetter = 0;
while($skipToLetter < $dumpLength){
$letter = $dump[$skipToLetter];
...
$skipToLetter++;
}
}
Here's the reason: assume you have a string with length of 4. This means that the last index in the string is 3. Your index goes up to 3. It gets compared in the while loop (3<4)? and the answer is true. The code enters the while loop and increments the value of the index which will be greater than the last index of the string, thus causing the warning.
Updated your code...
public function rollSausage($dump){
$anchorArray = array();
$dumpLength = strlen($dump);
$skipToLetter = 1;
while($skipToLetter < $dumpLength){
$skipToLetter++;
if( siset( $dump[$skipToLetter]) )
$letter = $dump[$skipToLetter];
...
}
}
}
This code below has Undefined offset error on line 5.
I don't know why this appears, I'm fighting with this for about an hour.
It says it's in a line where for is, but as I see syntax is correct :/
<?php
function palindrom($broj) {
$brojniz=str_split($broj);
for ($x=0; $x<3; $x++) {
if ($brojniz[$x] != $brojniz[5-$x]) {return;}
}
return($broj);
}
$n=100;
$m=$n;
while ($n<1000) {
while ($m<1000) {
$br=$m*$n;
palindrom($br);
++$m;
}
$m=100;
++$n;
}
?>
but as I see syntax is correct
Yes, the syntax is correct. But the runtime values are not. This syntax is also correct, but will produce an error:
$x = 1 / 0;
The line in question is indexing an array:
if ($brojniz[$x] != $brojniz[5-$x])
And the value $x goes from 0-2 in that loop. So you're indexing as such:
if ($brojniz[0] != $brojniz[5])
if ($brojniz[1] != $brojniz[4])
if ($brojniz[2] != $brojniz[3])
Does that array go from 0-5? If not, then you're referencing an undefined index.
Showing error as undefined index cnt.
please help me regarding this issue.thanks in adcance
if($_REQUEST["cnt"]!=""){
$count=$_REQUEST["cnt"];
$cntprev=$count-2;
}else
{
$count=1;
}
You have to check if the index is set. Actually you just check if it is an empty string. But you have to check if it is set, not empty and the value is numeric (if you want to cat it to int float ...). This should work:
if(isset($_REQUEST["cnt"]) && !empty($_REQUEST["cnt"]) && is_numeric($_REQUEST["cnt"])) {
$count=$_REQUEST["cnt"];
$cntprev=$count-2;
} else {
$count=1;
}
I'm trying to perform two calculations in one function and return the values of both calculations to seperate variables. I think i have everything setup nearly correct but i'm getting an error message which i'll post after the code. I could easily write two functions to do what I want but i'd rather have it all in one function since the function is initiated by the click of one button.
function sg_gc1($gv_1, $g_1) {
$gv_1 = $gv_1 - 1;
Return $gv_1;
$g_1 = $g_1 + 20;
Return $g_1;
}
// This second part in my code is mostly irrelevant to my question but it helps paint the picture.
if (isset($_POST['Calculate']) && ($_SESSION['gv_1'] != 0) ) {
$_SESSION['gv_1'] = sg_gc1($_SESSION['gv_1']); // line 55
$_SESSION['g_1'] = sg_gc1($_SESSION['g_1']); // line 56
} // This statement assigns the new calculated values to my existing session variables and it worked just fine before i tried adding in the second argument
Warning: Missing argument 2 for sg_gc1(), called in F:PATH on line 55 and defined in F:PATH on line 6
Warning: Missing argument 2 for sg_gc1(), called in F:PATH on line 56 and defined in F:PATH on line 6
So the error is happening inside the function which leads me to believe the function is not set up properly for two arguments. It works just fine with 1 argument if i were to remove all the g_1 parameters.
Call function like this, because your function accepts two arguments and you are passing only one.
sg_gc1($_SESSION['gv_1'],$_SESSION['g_1']);
and in function you have to return like below. Your second return would never get executed.
function sg_gc1($gv_1, $g_1) {
$gv_1 = $gv_1 - 1;
$g_1 = $g_1 + 20;
return array($gv_1,$g_1);
}
Calling function part
if (isset($_POST['Calculate']) && ($_SESSION['gv_1'] != 0) ) {
list($_SESSION['gv_1'],$_SESSION['g_1']) = sg_gc1($_SESSION['gv_1'],$_SESSION['g_1']);
echo $_SESSION['gv_1']."--".$_SESSION['g_1'];//for checking purpose
}
You can't return two variables in a function. Return an array instead:
function sg_gc1($gv_1, $g_1) {
$gv_1 = $gv_1 - 1;
$g_1 = $g_1 + 20;
return array($gv_1, $g_1);
}
Now to retrieve the values, you can simply access the array indices:
$arr = sg_gc1($_SESSION['gv_1'], $_SESSION['g_1']);
$gv_1 = $arr[0];
$g_1 = $arr[1];
Or you could use the list construct to do it in one line:
list($gv_1, $g_1) = sg_gc1($_SESSION['gv_1'], $_SESSION['g_1']);
Im trying to get to grips with php (rather unsuccessfully). I keep getting tripped up on the syntax for defining and calling functions
the code ive written is
$alpha = array(1, "alpha");
$beta = array(2, "beta");
function find_best_provider($provider1, $provider2){
if($provider1 > $provider2){
return array($provider1[0], $provider1[1]);
}
else {
return array($provider2[0], $provider2[1]);
}
}
$winner = find_best_provider($alpha, $beta);
echo $winner;
But i keep getting this notice -
Notice: Array to string conversion in /Applications/MAMP/htdocs/find_best_provier_func.php on line 17
Array
Im aware what the problem is but im not quite sure how to solve it, any helps much appreciated !
If you're trying to evaluate the first element in the array, try this:
function find_best_provider($provider1, $provider2)
{
if($provider1[0] > $provider2[0])
{
return array($provider1[0], $provider1[1]);
}
else
{
return array($provider2[0], $provider2[1]);
}
}