Man oh man I cannot figure this out... Please help.
What a missing here?
here are the errors:
Notice: Uninitialized string offset: 62 in /..../libraries/functions.php on line 316
Notice: Undefined variable: stilldo in /..../libraries/functions.php on line 322
Here is the code:
function generate_password($length = 12, $letters = true, $mixed_case = true, $numbers = true, $punctuation = false){
## Generate the alfa
$alfa = '';
if($letters == true) $alfa .= 'abcdefghijklmnopqrstuvwxyz';
if($mixed_case == true) $alfa .= strtoupper('abcdefghijklmnopqrstuvwxyz');
if($numbers == true) $alfa .= '0123456789';
if($punctuation == true)$alfa .= '~!##$%^&*(),.=+<>';
## Set Random Seed
if(!function_exists('_generate_password_seed')):
function _generate_password_seed(){
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
endif;
srand(_generate_password_seed());
## Generate the password
$token = "";
for($i = 0; $i < $length; $i ++) {
$token .= $alfa[#rand(0, #strlen($alfa))];
}
## Check the length
if(strlen($token) < $length){
// echo $stilldo = $length - strlen($token);
$token .= generate_password($stilldo);
}
## Return the password
return $token;
}
I get this error 5 out of 10 times i run this function and I can't seem to crack it.
Notice: Undefined variable: stilldo means, that a variable $stilldo is not defined, nor initialized... That means, that You call function generate_password in recursion when passing it an undefined variable, that in fact has a null value. Therefore Your function cannot work properly.
The part of your code should look like this:
## Check the length
if(strlen($token) < $length){
$stilldo = $length - strlen($token);
$token .= generate_password($stilldo);
}
Yes, a stupid mistake - You commented out the right line (where only echo should be commented out or deleted).
Enjoy!
Related
I am writting my own calculator on PHP.
I have a problem with my code because i don't know where i am trying to read too far in the string. So if anyone can enlighten me ..
The exact error i get is :
PHP Notice: Uninitialized string offset: 4 in /home/salim/Bureau/web/piscine_php/d01/ex11/do_op_2.php on line 76
Here is the code below :
function decoupe ($argv)
{
global $nbr1;
global $nbr2;
global $sign;
$string = NULL;
$string = trim($argv[1], " \t");
echo $string;
echo "\n";
$x = 0;
while($string[$x])
{
if (is_numeric($string[0]) == false)
error_msg();
if (is_numeric($string[$x]) && $string[$x + 1])
{
while (is_numeric($string[$x]))
{
$nbr1 .= $string[$x];
$x++;
}
}
if (is_thisoperator(substr($string, $x)))
{
$sign .= $string[$x];
$x++;
}
else
{
error_msg();
}
if ($string[$x + 1] && is_numeric($string[$x]))
{
while (is_numeric($string[$x]))
{
$nbr2 .= $string[$x];
$x++;
}
}
else
{
error_msg();
}
}
Don't use $string[$x] as a way to test whether $x is a valid index in the string. It prints a warning when $x is outside the string. Use $x < strlen($string) instead. So change:
while ($string[$x])
to
while ($x < strlen($string))
and change
if ($string[$x + 1] && is_numeric($string[$x]))
to
if ($x + 1 < strlen($string) && is_numeric($string[$x]))
This is my script where I am calculating the length of string without library function, but I am getting error?
<?php
$name = "Mohammad Umar";
$i = 0;
while ($name[$i] != ''){
$i++;
}
echo $i;
?>
In your string there are 13 offsets, from 0 to 12.
There isn't any offset which equals to '', since you only have alpha characters and a space.
So, your while loop cannot stop and reaches the undefined 13th offset.
Were you actually trying to reach the space ? If this is the case, try to test $name[$i] on ' '
EDIT
To compute the length of the string without strlen, but with at least one variable handling function called isset, I would do this :
$name = "Mohammad Umar";
$i = -1;
while (isset($name[++$i]));
echo $i;
Try this,
$name = "Mohammad Umar";
$name_length=strlen($name);
$i = 0;
for($i=0;$i<$name_length;$i++)
{
if($name[$i] == '')
{
echo $i;
}
}
I have this code:
$len = strlen($string);
if ($string{$len-1} == '-') {
// Do stuff...
}
However I get the following NOTICE error:
Uninitialized string offset: -1
When I var_dump($len-1) the value I get:
int 3
When I var_dump($string) I get:
string 'bobo' (length=4)
So could anyone tell me why this is causing a NOTICE error?
To prevent notice add additional condition to if statement:
$len = strlen($string);
if ($len > 0 && $string{$len-1} == '-')
// Do stuff...
}
or use substr function:
if (substr($string, -1) == '-') {
// Do stuff...
}
$len = strlen($string);
if(isset($string{$len-1})){
if ($string{$len-1} == '-') {
// Do stuff...
}
}
Hello I have this code which selects from a list of 20 items 12 and from those 12 1 is randomly selected and echoed:
class Gamble
{
public static function doPointsCheck()
{
global $Gamble;
$Gamble_points = '2';
if($Gamble_points <= 1)
return false;
else
return true;
}
public static function Spin()
{
global $Wheel;
if(!self::doPointsCheck())
return 'You need way too more points to gamble';
else
{
$Result = array();
$Result = range(1, 12);
shuffle($Result);
$Result = array_slice($Result, 0, 20);
$SendToCheck = self::CheckPrize($Result);
}
}
public static function CheckPrize($Array)
{
global $Wheel;
echo '<h1>List of items you can win today:</h1><form action="class.MysticWheel.php" method="post">';
for($i = 1; $i <= count($Array); $i++)
{
$Won = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
echo $Won;
}
echo '<input name="Feeling lucky" type="submit" value="Feeling Lucky" /></form>';
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$i = rand( 1, 12 );
$Prize = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
echo $Prize;
}
}
}
When I test it I recieve the following error:
Notice: Undefined offset: 12 on line 54
Notice: Undefined index: on line 54
So i won't show the last item, it will only show 11 possible items that could have been won and get the notice.
How can I fix it?
$Result = range(1, 12);
-->
foreach (range(1, 12) as $number) {
$Result[] = $number;
}
Try this.
my goal is to convert every space " " in string to "%".
Here is my function:
<?php
$nazov = "dasa sdas da sd";
$buttonNazov = "";
for($i=0;$i<=strlen($nazov);$i++) {
if($nazov[$i] === " ") {
$buttonNazov .= "%"; // Line# 6
} else {
$buttonNazov .= $nazov[$i]; // Line#12
}
}
echo $buttonNazov;
?>
I am getting output but also 2 errors:
( ! ) Notice: Uninitialized string offset: 15 in C:\wamp\www\test.php on line 6
( ! ) Notice: Uninitialized string offset: 15 in C:\wamp\www\test.php on line 12
dasa%sdas%da%sd
from Mark Baker comment: Offset begins at 0, not at 1; so $i<strlen($nazov) and not $i<=strlen($nazov)
below is more better way of writing same
<?php
$nazov = "dasa sdas da sd";
$buttonNazov = "";
$len = strlen($nazov);
for($i=0; $i<$len; $i++) {
if("" === $nazov[$i]) {
$buttonNazov .= "%";
} else {
$buttonNazov .= $nazov[$i];
}
}
echo $buttonNazov;
?>
alternative way if you want to replace space with %
$buttonNazov = str_replace(' ', '%', $nazov);
the last index of a string $nazov is $nazov[strlen($nazov)-1], so use < instead of <= in the loop condition:
for($i=0;$i<strlen($nazov);$i++)