0

I am a student with PHP and I have a question from this course I am taking which I am able to produce my own solution which works, however the course uses some sort of "fill in the blank" testing method on their web page which means parts of the code cannot be edited.

Basically I have this right now:

<?php

$myArray = array(2,4,6,8);

foreach($myArray as $value)

    echo "Points are $value\n";

?>

But it prints "Points are 2 Points are 4 Points are 6 Points are 8"... It needs to print Points are 2 4 6 8 (so print a value with each loop with a space). I know that there is a method that measures the number of arrays against what is being printed before it stops, however I haven't been able to figure it out.

EDIT: Here is the actual question from the webpage of my school's course:

<?php

$numberstring = $_GET['numberstring'];
$numberarray = explode(',',$numberstring);

//your code here

echo "Points are $value\n";  
?>
4
  • 3
    Echo the "Points are" part outside the loop Commented Sep 16, 2019 at 18:23
  • 3
    You can 'implode' the contents of the array into a string to stick on the end of your output. Commented Sep 16, 2019 at 18:23
  • 1
    echo "Points are 2 4 6 8"; :) Commented Sep 16, 2019 at 18:28
  • There are still multiple ways to do this ($value = str_replace(","," ",$numberstring), ignoring the array entirely) or as already pointed out, use implode() ($value = implode(" ", $numberarray)) or more convoluted possibilities. You can also use a foreach to concatenate as a string if that's the requirement ($value;foreach($numberarray as $number){$value .= $number." ";}) --- you have to pick the way that answers the question since we can't see it. Commented Sep 16, 2019 at 20:19

2 Answers 2

3

To put them together you can join them.

echo 'Points are ', implode(' ', $myArray), PHP_EOL;

Edit:

After you updated your question, use this as answer to the 2nd part:

$points is empty, so you can directly use implode on $numberrray.

echo 'Points are ', implode(' ', $numberarray), PHP_EOL;
Sign up to request clarification or add additional context in comments.

3 Comments

I can't edit the line where "echo" is.... this assignment is pretty weird, and I got it to work on my own computer but not on the webpage where the course is held (since part of the code can't be edited). I think it's better that I paste how the question was phrased: <?php $numberstring = $_GET['numberstring']; $numberarray = explode(',',$numberstring); //your code here echo "Points were: $points\n"; ?>
$points is empty. If you only can edit the echo-line, try echo 'Your points are ', implode(' ', $numberarray), PHP_EOL.
Thank you, it's working perfectly. So the teacher expected us to have an understanding of both explode and implode functions for this.
2

Foreach loops do everything in the loop, every time. Easy mistake, you'll get used to it.

<?php

$myArray = array(2,4,6,8);

echo "Points are ";
foreach($myArray as $value)

    echo $value." ";

?>
echo "\n";

See Markus Zeller's answer for a better way to do this, if your predefined code templates allow it.

Comments

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.