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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.