0
<?php
function szczesliwy ($od, $do, $ile) {
    $zasieg = range($od, $do);
    $losowe = array_rand($zasieg, $ile);
    $wynik = array();
    foreach ($losowe as $index) {
        $wynik []= $zasieg[$index];
    }   
    return $wynik;
}

szczesliwy(1, 31, 20);
print($wynik);
?>

It says Notice:

Undefined variable: wynik in C:\xampp\htdocs\21\index.php on line 14

and nothing else happens, and my question is how to put result on screen?

1
  • 1
    instead of szczesliwy(1, 31, 20); print($wynik); write $wynik = szczesliwy(1, 31, 20); print($wynik); , you misunderstood variable scope Commented Nov 14, 2016 at 9:40

3 Answers 3

1

You need to get what function returns

$wynik = szczesliwy(1, 31, 20);
print($wynik);
Sign up to request clarification or add additional context in comments.

Comments

0

Check the following line:

szczesliwy(1, 31, 20);
print($wynik);

where $wynik is defined??

Try this:

$wynik = szczesliwy(1, 31, 20);
// hold the return value of function
print($wynik);

Comments

0
    <?php
function szczesliwy ($od, $do, $ile) {
$zasieg = range($od, $do);
$losowe = array_rand($zasieg, $ile);
$wynik = array();
foreach ($losowe as $index) {
    $wynik []= $zasieg[$index];
}   
return $wynik;


}
$wynik = szczesliwy(1, 31, 20);
print_r($wynik);
?>

4 Comments

Thanks! That helped a lot.
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
Don't just code educate as well,you must explain your code for future readers as @Rizier123 have stated above.
print Outputs only a single string Returns 1, so it can be used in an expression e.g. print "Hello" or, if ($expr && print "foo") print_r() Outputs a human-readable representation of any one value Accepts not just strings but other types including arrays and objects, formatting them to be readable Useful when debugging May return its output as a return value (instead of echoing) if the second optional argument is given

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.