2

I made a php script where you have to add things to your bucketlist, but it isn't working properly. The script needs to output the bucketlist, but it only outputs the last value that is entered.

<?php
echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";

if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

$bucketlist = [$c];
array_push($bucketlist);

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}
?>

I need it to output everything that is entered in readline $c

2
  • array_push($bucketlist) wants two parameter Commented Nov 23, 2020 at 10:39
  • 1
    What have you tried to debug the problem? Commented Nov 23, 2020 at 10:54

3 Answers 3

3

as mentioned by the above authors, $c = readline(); happens inside a loop so it is overridden with each iteration. Try this instead:

echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";
$bucketlist = [];
if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
        array_push($bucketlist, $c);
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

$bucketlist = [$c]; happens inside a loop, no that's not what I said, and it's not what happens in the OP's code. Read a little more carefully. However, your actual solution is fine (although it doesn't differ from stackoverflow.com/a/64966810/5947043 which was already posted).
Ok. Your code will work, but interestingly, the manual (php.net/manual/en/function.array-push.php) says Note: If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.
It still doesn't work right. Right now it outputs: How much activities would you like to add? 2 What do you want to add to your bucket list? a What do you want to add to your bucket list? w On your bucket list: a On your bucket list: w But the correct output has to be: How much activities would you like to add? 2 What do you want to add to your bucket list? a What do you want to add to your bucket list? w On your bucket list: a w
For that you will have to remove the last foreach for the bucketlist and replace it with echo "On your bucket list:\n" . implode(", ", $bucketlist);
Thank you so much dopesky, all the others, thank you aswell. You were all a really good help! :)
2

$bucketlist = [$c]; happens outside your loop, so it only has access to the value of $c as it stood when the loop ended.

Move it inside your for loop and also make it say $bucketlist[] = $c; so it appends to bucketlist instead of overwriting it.

And you don't need your array_push line (which I think did nothing anyway, since you didn't specify anything to push into the array).

Also you should define $bucketlist as an empty array before you start looping.

echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";
$bucketlist = array();

if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
        $bucketlist[] = $c;
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}

Comments

1

on each iteration of for loop, $c = readline(); is reading a new value and without adding it to $bucketlist = []; you lose the value so you need to also add the value of $c on each iteration to the $bucketlist[] and you have to add $c value to $bucketlist[] inside the for loop:

  1. define $bucketlist = []; as an array
  2. use array_push($bucketlist, $c); or $bucketlist[] = $c in loop
echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";

$bucketlist = [];

if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
        $bucketlist[] = $c;
        // array_push($bucketlist, $c);
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}
?>

1 Comment

This will work, but interestingly, the manual (php.net/manual/en/function.array-push.php) says Note: If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.

Your Answer

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