Home > Software design >  cannot use variable which is declared in helper function
cannot use variable which is declared in helper function

Time:01-22

helper.php

<?php

 function h()
{
    $randomchar = str_shuffle('abcdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ234567890!$%^&!$%^&');
            $password = substr($randomchar, 0, 12);
            $generated_pass = $password;
}

view

        <input type="text" id="lname" name="pass" value="<?php echo $generated_pass;?>"><br><br>

error ErrorException PHP 8.1.12 9.48.0 Undefined variable $generated_pass

tried to do it like above but got error.

CodePudding user response:

functions should return its local value:

function h()
{
    $randomchar = str_shuffle('abcdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ234567890!$%^&!$%^&');
    return substr($randomchar, 0, 12);
}

then you should call function:

<input type="text" id="lname" name="pass" value="<?php echo h();?>"><br><br>

CodePudding user response:

I think you need to call the function instead. <input type="text" id="lname" name="pass" value="<?php echo h(); ?>"><br><br>

Also make sure that the helper.php file is loaded.

  • Related