I receive the following PHP notice
PHP Notice: Undefined offset: 0
Here is a sample of what causes the error.
$file = $_SERVER['DOCUMENT_ROOT']."/savedposts/edited".$post_id.".txt";
$saved_content = file_get_contents($_SERVER['DOCUMENT_ROOT']."/savedposts/".$post_id.".txt");
$cct = 0;
$contexttext = array();
for ($i = 0; $i < strlen($saved_content); $i++) {
if (substr($saved_content, $i, 9) == "[Context]") {
$i = $i + 9;
while (substr($saved_content, $i, 10) !== "[/Context]") {
$contexttext[$cct] .= substr($saved_content, $i, 1);
$i++;
}
$cct++;
}
}
The error is on this line
$contexttext[$cct] .= substr($saved_content, $i, 1);
How to fix the notice.
To replicate this:
$cct = 0;
$contexttext = array();
$contexttext[$cct] .= 'test';
Here 'test' is being appended to $contexttext[$cct], which evaluates to: $contexttext[0]. However there is nothing at [0] yet, because it's an empty array, we can't append to something that doesn't exist
If however you'd done this:
$cct = 0;
$contexttext = array();
$contexttext[$cct] = '';
$contexttext[$cct] .= 'test';
Then the notice would dissapear, because now when we append a string, we have something to append it to
Related
My webhotel have uppgrade to PHP version 8.0 The code is working but it's showing warningmessage on the homepage now.
I'ts something with my array.
Warning: Undefined array key "JS" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1465
Warning: Undefined array key "Vinst" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1465
Warning: Undefined array key "PWB" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1468 Warning:
Undefined array key "Vinst" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1468 Warning:
Undefined array key "oavgjord" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1471 here
I Have tried with
enterif (isset($matchesLines[$Borta_Lag]['omgångar'])){
$matchesLines[$Borta_Lag]['omgångar']+=1;
} code here
but it doesnt work it's get a empty value.
Here is the code.
I hope someone can help me?
// Sorterar ut lagnamn och resultat
$file = file($filename);
$result = array_merge($file,$resultatarray)
$matchesLines = array();
foreach($result as $line) {
if(preg_match('/^([a-öA-Ö]+\D)-([a-öA-Ö]+\D) (\d+)-(\d+)/', $line, $data)){
$Hemma_Lag = $data[1];
$Borta_Lag = $data[2];
$Hemma_Resultat = $data[3];
$Borta_Resultat=$data[4];
$matchesLines[$Hemma_Lag]['Vinst'] += 0;
$matchesLines[$Borta_Lag]['Vinst'] += 0;
$matchesLines[$Hemma_Lag]['oavgjord'] += 0;
$matchesLines[$Borta_Lag]['oavgjord'] += 0;
$matchesLines[$Hemma_Lag]['Förlust'] += 0;
$matchesLines[$Borta_Lag]['Förlust'] += 0;
if ($Hemma_Resultat == $Borta_Resultat){
$matchesLines[$Hemma_Lag]['lag'] = $Hemma_Lag;
$matchesLines[$Hemma_Lag]['poang']+=1;
$matchesLines[$Hemma_Lag]['mål'] += $Hemma_Resultat;
$matchesLines[$Hemma_Lag]['måli'] += $Borta_Resultat;
$matchesLines[$Hemma_Lag]['oavgjord'] += 1;
$matchesLines[$Borta_Lag]['lag'] = $Borta_Lag;
$matchesLines[$Borta_Lag]['poang'] +=1;
$matchesLines[$Borta_Lag]['mål'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['måli'] += $Hemma_Resultat;
$matchesLines[$Borta_Lag]['oavgjord'] += 1;
}
if ($Hemma_Resultat > $Borta_Resultat){
$matchesLines[$Hemma_Lag]['lag'] = $Hemma_Lag;
$matchesLines[$Hemma_Lag]['poang']+=3;
$matchesLines[$Hemma_Lag]['mål'] += $Hemma_Resultat;
$matchesLines[$Hemma_Lag]['måli'] += $Borta_Resultat;
$matchesLines[$Hemma_Lag]['Vinst'] += 1;
$matchesLines[$Borta_Lag]['lag'] = $Borta_Lag;
$matchesLines[$Borta_Lag]['poang'] +=0;
$matchesLines[$Borta_Lag]['mål'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['måli'] += $Hemma_Resultat;
$matchesLines[$Borta_Lag]['Förlust'] += 1;
}
if ($Hemma_Resultat < $Borta_Resultat) {
$matchesLines[$Hemma_Lag]['lag'] = $Hemma_Lag;
$matchesLines[$Hemma_Lag]['poang']+=0;
$matchesLines[$Hemma_Lag]['Förlust'] += 1;
$matchesLines[$Hemma_Lag]['mål'] += $Hemma_Resultat;
$matchesLines[$Hemma_Lag]['måli'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['lag'] = $Borta_Lag;
$matchesLines[$Borta_Lag]['poang'] +=3;
$matchesLines[$Borta_Lag]['Vinst'] += 1;
$matchesLines[$Borta_Lag]['mål'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['måli'] += $Hemma_Resultat;
}
if (isset($matchesLines[$Borta_Lag])){
$matchesLines[$Borta_Lag]['omgångar']+=1;
$matchesLines[$Hemma_Lag]['omgångar']+=1;
}
}
}
Yes, PHP 8.0 has elevated this notice and a bunch of other notices to now be warnings :
A number of notices have been converted into warnings:
Attempting to read an undefined variable.
Attempting to read an undefined property.
Attempting to read an undefined array key.
...
There was a problem with the old slowAES library.
When trying to decrypt, in js it produces one, and in php it produces another.
There are a lot of errors in the console that I can’t figure out.
Tell me what's wrong? How to get the same keys?
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$a = "cd36b76f96b103402924bd5f76d3c204";
$b = "680eb6a492f48ea1b342aea7b79e18eb";
$c = "f746749b113236227058bd471f5c91dc";
function toHex($args){
if(func_num_args() != 1 || !is_array($args)){
$args = func_get_args();
}
$ret = '';
for($i = 0; $i < count($args) ;$i++)
$ret .= sprintf('%02x', $args[$i]);
return $ret;
}
function toNumbers($s){
$ret = array();
for($i=0; $i<strlen($s); $i+=2){
$ret[] = hexdec(substr($s, $i, 2));
}
return $ret;
}
function getRandom($min,$max){
if($min === null)
$min = 0;
if($max === null)
$max = 1;
return mt_rand($min, $max);
}
function generateSharedKey($len){
if($len === null)
$len = 16;
$key = array();
for($i = 0; $i < $len; $i++)
$key[] = getRandom(0,255);
return $key;
}
function generatePrivateKey($s,$size){
if(function_exists('mhash') && defined('MHASH_SHA256')){
return convertStringToByteArray(substr(mhash(MHASH_SHA256, $s), 0, $size));
}else{
throw new Exception('cryptoHelpers::generatePrivateKey currently requires mhash');
}
}
function convertStringToByteArray($s){
$byteArray = array();
for($i = 0; $i < strlen($s); $i++){
$byteArray[] = ord($s[$i]);
}
return $byteArray;
}
function convertByteArrayToString($byteArray){
$s = '';
for($i = 0; $i < count($byteArray); $i++){
$s .= chr($byteArray[$i]);
}
return $s;
}
include 'cryptovh/aes.php';
$aes = new AES();
$token = $aes->decrypt(toNumbers($c), 16, 2, toNumbers($a), 16, toNumbers($b));
echo toHex($token); // WHAT I HAVE
echo "<br>";
echo "016e9be78dd5130beb5febcd328ff588"; // WHAT I NEED
?>
cryptovh/aes.php
that is copy of: https://github.com/aleaxit/slowaes/blob/master/php/aes_fast.php
use this library:
https://github.com/aleaxit/slowaes
At the output, I get this token:
dd2f6d60b939b390dc19688babc3873d
And console errors:
Notice: Undefined offset: 16 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 20 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 24 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/panel/cryptovh/aes.php on line
386
In slowaes/php/aes_fast.php, the inversion of the MixColumns operation in the mixColumns method is implemented incorrectly, the else-block must be:
...
} else {
for ($c = 0; $c < 4; $c++) {
$t[ $c] = self::$GEX[$state[$c]] ^ self::$GBX[$state[4+$c]] ^ self::$GDX[$state[8+$c]] ^ self::$G9X[$state[12+$c]];
$t[ 4+$c] = self::$G9X[$state[$c]] ^ self::$GEX[$state[4+$c]] ^ self::$GBX[$state[8+$c]] ^ self::$GDX[$state[12+$c]];
$t[ 8+$c] = self::$GDX[$state[$c]] ^ self::$G9X[$state[4+$c]] ^ self::$GEX[$state[8+$c]] ^ self::$GBX[$state[12+$c]];
$t[12+$c] = self::$GBX[$state[$c]] ^ self::$GDX[$state[4+$c]] ^ self::$G9X[$state[8+$c]] ^ self::$GEX[$state[12+$c]];
}
}
...
The inverse of the MixColumns operation is required for decryption.
There is also a typo in the method invMain, line 3, where i must be replaced by $i.
With these changes, the expected result is obtained, which can be verified e.g. here. The warnings are also no longer displayed.
I've filed an issue here. Note the ReadMe: The code is intended more for didactic purposes. In practice, openssl_encrypt / openssl_decrypt or similar should be used.
I want to add a child with a specified value which I get from my array. But my problem is that my array is bigger then my amount of XML products...
This is why I get this error message:
PHP Notice: Undefined offset: 1589 in C:\Users\Jan\PhpstormProjects\censored\test.php on line 63
PHP Fatal error: Uncaught Error: Call to a member function appendChild() on null in C:\Users\Jan\PhpstormProjects\censored\test.php:64
To check that I made two loops which say me that I have 1588 names and 1588 products, both loops say that. Thats logical!
$markerProduct = $root->getElementsByTagName("product");
for($i = $markerProduct->length - 1; $i >= 0; $i--){
$productCounter = $productCounter + 1;
}
$markerTitle = $root->getElementsByTagName("name");
for($i = 0; $i < $markerTitle->length; $i++){
$nameCounter = $nameCounter + 1;
}
But my array in which I store the specified value for each product is 1589 (0 - 1588) values big... And I don't know.
Can anyone help me and tell me the error?
$searches = ["Steam", "Uplay", "Xbox", "Nintendo", "PS3", "PS4", "PSN"];
function isContaining($searches, $titleTag, $urlTag, $productTag, $path){
$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($path);
$root = $dom->documentElement;
$markerTitle = $root->getElementsByTagName($titleTag);
$markerURL = $root->getElementsByTagName($urlTag);
$plat = array();
for($i = $markerTitle->length - 1; $i >= 0 ; $i--){
$title = $markerTitle->item($i)->textContent;
$url = $markerURL->item($i)->textContent;
$co = count($searches);
$productFound = false;
for($j = 0; $j < $co; $j++){
if(stripos($title, $searches[$j]) !== false){
if($j > 3){
array_push($plat, "PlayStation");
}else{
array_push($plat, $searches[$j]);
}
$productFound = true;
}elseif(stripos($url, $searches[$j]) !== false){
if($j > 3){
array_push($plat, "PlayStation");
}else{
array_push($plat, $searches[$j]);
}
$productFound = true;
}
}
if($productFound == false){
array_push($plat, "Nothing");
}
}
print_r($plat);
$c = count($plat);
echo $c;
for($i = $c - 1; $i >= 0; $i--){
$node = $dom->createElement('plat', $plat[$c]);
$dom->getElementsByTagName($productTag)->item($i)->appendChild($node);
}
$dom->saveXML();
$dom->save('data/gamesplanet2.xml');
}
Error is in line 63:
$node = $dom->createElement('plat', $plat[$c]);
Greetings and Thank You!
I have a code in use and it generates a PHP error in the second "for" loop.
PHP Notice: Undefined variable: newmatches
if (empty($result['ERR'])) {
preg_match_all('(<h3><a[^<>]*href="([^<>]*)"[^<>]*>(.*)</a>\s*</h3>)siU', $result['EXE'], $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
$matches[1][$i] = urldecode($matches[1][$i]);
preg_match_all('/\*\*(http:\/\/.*$)/siU', $matches[1][$i], $urls);
$newmatches[1][$i] = $urls[1][0];
}
for ($i = 0; $i < count($newmatches[1]); $i++) { //PHP Notice: Undefined variable: newmatches
if(strstr($newmatches[1][$i], $domain))
return $i+1;
}
} else {
return '0';
}
Thank you in advance!
I don't see anywhere where $newmatches would be set, aside from the first for loop, which won't run if count($matches[1]) is 0.
Not sure what all this hardcoding of your index to 1 is about, but a simple fix is to set $newmatches[1] = array() before the first loop.
I'm working to parse the following string but at the same time trying to get rid of the current undefined offset on the index array. I would appreciate some help w/ the undefined offset issue.
error_reporting(E_ALL);
ini_set('display_errors', '1');
function my_recursion($String, &$Inc) {
$l = strlen($String);
$has_quotes = 0; $array = array();
$x= 0;
for ($Inc; $Inc < $l; $Inc++) {
$my_char = $String[$Inc];
if ($my_char == '(' && !$has_quotes) {
$Inc++;
$array[$x] = my_recursion($String, $Inc);
$x++;
} else if ($my_char == '"') {
$has_quotes = !$has_quotes;
if (!$has_quotes)
$x++;
} else if ($has_quotes) {
$array[$x] .= $my_char;
}
}
print_r($array);
}
$String = '(("HELLO"("BAR")("FOO")()""))';
$Inc = 0;
(my_recursion($String, $Inc));
To get rid of the errors, add this line to the start of your function:
$array = array();
and replace this line:
$array[$x] .= $my_char;
with this:
$array[$x] = isset($array[$x])? $array[$x].$my_char : $my_char;
For help with your recursion, you'll need to describe its desired behavior.