PHP Notice: String offset cast occurred line 251 & 258 - php

i get php notice with this
$code = "";
while ($id > $length - 1) {
// determine the value of the next higher character
// in the short code should be and prepend
$code = self::$chars[fmod($id, $length)] . $code; //<-- line 251
// reset $id to remaining value to be converted
$id = floor($id / $length);
}
// remaining value of $id is less than the length of
// self::$chars
$code = self::$chars[$id] . $code; //<-- line 258
return $code;
The NOTICE code is this part:
$code = self::$chars[fmod($id, $length)] . $code;
$code = self::$chars[$id] . $code;
How to fix it? I can’t find it, please your help all.. :)

Using intval should fix it:
..$chars[intval(fmod($id, $length))]..
That is caused because fmod returns a float, and it casts it to integer. See this example:
$a = 'abcdef';
$b = $a[1.0];
echo $b;
And the output:
E_NOTICE : type 8 -- String offset cast occurred -- at line 3
b
If you think about it (at least in this case), you can either be at position 1, or at position 2, for example. You can't be at position 1,5. So when you try to access something float, it automatically casts the value to integer and lets you know that it did that.

Related

PHP errror in cos() function

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.

PHP bcmod or gmp_mod input type issue

I'm using bcmod and gmp_mod functions in php for handling large numbers.
This works fine:
// large number must be string
$n = "10000000000000000000001";
$y = 1025;
$c = 1025;
// Both works the same (also tested in python)
$y = gmp_mod( (bcpowmod($y, 2, $n) + $c) , $n);
$y = bcmod ( (bcpowmod($y, 2, $n) + $c) , $n);
But the input $n is not static. So I must use type casting like:
$n = (string)10000000000000000000001;
This doesn't work anymore.
for gmp gives this error:
gmp_mod(): Unable to convert variable to GMP - string is not an integer
And about bc, gives me this error:
bcmod(): Division by zero
The problem is, (string) doesn't convert it to string fine. Any idea?
Edit: I found a solution here, but still the input is string:
$bigint = gmp_init("9999999999999999999");
$bigint_string = gmp_strval($bigint);
var_dump($bigint_string);
If You are taking an input for $n it would give you as a string not as int and if you have type casted as int at any point ... taking care the max size of int, the above given no is converted to 1.0E+22 now what happens when you try to type cast (string)1.0E+22 it becomes "1.0E+22" which is obviously just a string and can not be converted to gmp number.
So You need to convert scientific notation to string which will auto include , hence you also need to replace those commas
$n = 10000000000000000000001;
$n = str_replace(",", "", number_format($n));

Warning: str_repeat(): Second argument has to be greater than or equal to 0

I used this a while back to grab images from something but since I just tried using it again it is giving me this error:
Warning: str_repeat(): Second argument has to be greater than or equal to 0 in C:\inetpub\wwwroot\resource_update.php on line 121
This is the function for what its referring to so if anyone could help that would be great:
function consoleLogProgressBar($current, $size, $unit = "kb")
{
$length = (int)(($current/$size)*100);
$str = sprintf("\r[%-100s] %3d%% (%2d/%2d%s)", str_repeat("=", $length).($length==100?"":">"), $length, ($current/($unit=="kb"?1024:1)), $size/($unit=="kb"?1024:1), " ".$unit);
consoleLog($str, true);
}
Sounds like $length is returning a negative number? You could troubleshoot as follows:
$length = (int)(($current/$size)*100);
var_dump($length);
exit;
If that is in fact the case, then you could wrap it in the abs() function which will always return an absolute value:
$length = (int) abs(($current/$size)*100);
Of course, that is an ugly hack and doesn't solve the real issue. Either way, the first step is determine why $length isn't what you expect it to be.

How to filter Just Number in string, then how to add with another number if the number is start with zero

I'm beginning in PHP or cakephp.
I have a case, when I want to get Just The Number in a code which type is string,
This is example string:
PR00006757
I want to get the number 00006757 without PR
I already tried using:
$code = filter_var($this->request->data['Price']['code'], FILTER_SANITIZE_NUMBER_INT);
pr ($code); exit;
then I get result "00006757",
But I want +1 that result,
to be "00006757 + 1" = 00006758,
But in reality, the result after I add 1 ( + 1) the result is 6758 not 00006758.
How do I get the answer formatted this way?
The output you are getting is correct as adding 1 automatically converts $code to integer type. You should use str_pad to pad the integer value you get by adding 1 to make it back to string of the required length.
$code = filter_var($this->request->data['Price']['code'], FILTER_SANITIZE_NUMBER_INT);
$code +=1;
$code = str_pad($code, 8, '0', STR_PAD_LEFT); //convert to padded string
print_r ($code);
Edit: Demo Here
Edit 2:
Added auto detection of the length
$code = filter_var($this->request->data['Price']['code'], FILTER_SANITIZE_NUMBER_INT);
$len = strlen($code); // store the length of the code
$code +=1;
$code = str_pad($code, $len, '0', STR_PAD_LEFT);
print_r ($code);
Demo Here
You're on the right track. Using filter_var is the correct way to grab the number you want. There isn't going to be a way to alter the way that PHP does basic addition. Your best chance is to identify the padding on the number and add that padding back after addition occurs. Here is an update to your code that could help.
$code = filter_var($this->request->data['Price']['code'], FILTER_SANITIZE_NUMBER_INT);
$len = strlen($code);
$newcode = $code + 1;
str_pad($newcode, $len, "0", STR_PAD_LEFT);
After testing with your example input I received: "00006758".
Here is another example
<?php
$str_code = 'PR00006757';
$code = filter_var($str_code, FILTER_SANITIZE_NUMBER_INT);
$code +=1;
$num_padded = sprintf("%08d", $code);
echo $num_padded;

read single byte as an unsigned byte

I have some a java server that I'm trying to get to play with a php script.
The format provides the number of bytes the message will take as an unsigned byte, and then the bytes that comprise the string.
here's my function with commentary
function read($socket, $readnum) {
$endl = "<br>"; // It's running on apache
// attempt to get the byte for the size
$len = unpack("C",socket_read($socket, 1))[0];
// this prints "after ", but $len does not print. $endl does
echo "after " . $len . $endl;
// obviously this fails because $len isn't in a valid state
$msg = socket_read($socket, $len);
// print the message for debugging?
echo $len . " " . $msg;
return $msg;
}
I'm not really a php guy, more of a Java guy, so I'm not sure how I should go about getting this length.
On #c0le2's request, I made the following edit
$lenarr = unpack("C",socket_read($socket, 1));
$len = $lenarr[0]; // line 81
Which gave the following error
PHP Notice: Undefined offset: 0 in simple_connect.php on line 81
The unpack format string is actually like code [repeater] [name], separated by forward slashes. For example, Clength. The output array will be associative and keyed by name, with repeated fields having a numeric suffix appended, starting at 1. E.g. the output keys for C2length will be length1 and length2.
The documentation is not super-clear about this.
So when you don't specify any name, it just appends the numeric suffix, starting at 1. So the length you are looking for is $lenarr[1].
But you should try this instead:
$v = unpack('Clength', "\x04");
var_export($v); // array('length' => 4)
Here are some other examples:
unpack('C2length', "\x04\x03");
// array ( 'length1' => 4, 'length2' => 3, );
unpack('Clength/Cwidth', "\x04\x03");
// array ( 'length' => 4, 'width' => 3, );
Also, in php you can't generally use array-access notation on an expression--you need to use it on the variable directly. So functioncall()[0] won't work, you need $v = functioncall(); $v[0]. This is a php wart.
You can't access a returned array like that. You'd do $len[0] after calling unpack().
$len = unpack("C",socket_read($socket, 1));
echo "after " . $len[0] . $endl;

Categories