Following function implements a way to find all the indices of a given $needle in $haystack. There is at least one bug in the implementation. Find and fix the bug(s).
<?php
function findAllIndices($haystack, $needle) {
  $indices = array();
  while ($index = strpos($haystack, $needle)) $indices[] = $index;
  return $indices;
}

Recommended Answers

I can see the errors, but this looks like it is for your homework.
Please be polite, and dont just paste the question.

Jump to Post

All 3 Replies

I can see the errors, but this looks like it is for your homework.
Please be polite, and dont just paste the question.

error in while loop...
there should be some condition so that first time some value can be given to index...

{
while (1)
{
if($index = strpos($haystack, $needle)) 
$indices[] = $index;

but it will result into infinite loop....so provide a condition according to ur problem...

more effing homework, eye dee ten Tee errors

I D 10 T

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.