4

I have a problem trying to run a code inside the loop, my loop consist of a function.

Here is my coding:

$new = array(1,2,3,4);
for($i=0;$i<=3;$i++){
$val = $new[$i];



function myfunction($value) {   
    //Do something
}

echo $val;
}

The problem is the code outputs only the 1st value in my array. I am very confused, am I not suppose to declare a function inside the loop?

3
  • 2
    Why is there a function in your loop? Commented Oct 17, 2012 at 7:46
  • 2
    as the correct answers have already been given, a short notice as to why this happens: your code tries to redefine the function 3 times, which is not allowed in PHP. A tip: use error_reporting(E_ALL); at the start of your script or set it in the php.ini while learning / developing - with this setting, you would have seen something along the lines of "fatal error, trying to redefine function myFunction in..." Commented Oct 17, 2012 at 7:54
  • for($i=0;$i<=3;$i++){ $val = $new[$i]; can be written as foreach ($new as $val) { ... Commented Oct 17, 2012 at 7:55

9 Answers 9

9

Your code ends up with Fatal error, since at the second iteration it tries to redeclare function myfunction. That's why it is printing only first value of array.

In order to avoid that fatal error you can check if that function has been already defined using function_exists() function like this:

$new = array(1,2,3,4);
for($i=0;$i<=3;$i++)
{
    $val = $new[$i];
    if(!function_exists('myfunction'))
    {
        function myfunction($value) {
            //Do something
        }
    }
    echo $val;
}

PHP is a scripting language and it is syntactically correct to declare a function inside for loop or inside if statement, but it is a bad practice and can cause a lot of errors afterwards.
The best way is to declare a function outside loop, and, if needed, call it from within a loop like this:

<?php
function myfunction($value) {
    //Do something
}

$new = array(1,2,3,4);
for($i=0;$i<=3;$i++)
{
    $val = $new[$i];
    myfunction($value); //may you was intended to pass $val here?
    echo $val;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I feel like if(!function_exists('myfunction')) should be built into the language spec. If someone wants to declare a function inside a loop (which is allowed and has interesting uses), they shouldn't have to resort to the same boilerplate every time.
6

Don't declare the function inside the loop, declare it before the loop and then call to it inside the loop with myFunction($value);

2 Comments

"declare it before" ... or after ;-)
Psh, just following programming best practices, you typically want to declare your functions first. But true, PHP will let you declare it after. =P
5

the function should be in a separate procedure

$new = array(1,2,3,4);
for($i=0;$i<=3;$i++)
{
   $val = $new[$i];
   myfunction($val)
   echo $val;
}

then this is your function

function myfunction($value) 
{   
    //Do something
}

1 Comment

Thanks man, I knew I was missing something small, I have wasted an hour trying to figure dis out, I feel so dum rite now :-D
1
  • Declare the function outside of the loop
  • either return a value from the function, or let the function output data

For example:

function myfunction($value) {   
  //Do something
  echo $value;
}

$new = array(1,2,3,4);
for($i=0;$i<=3;$i++) {
  myfunction($new[$i]);
}

Comments

0

I am assuming you want to print out the first 4 elements of the array. do something like this

function myfunction() {   
  $new = array(1,2,3,4);
  for($i=0;$i<=3;$i++){
    $val = $new[$i];
    echo $val;
  }
}

myfunction();

Comments

0

You should declare the function outside the loop

function myfunction($value) {    
    return ($value + 25); // an example
}

$new = array(1,2,3,4); 
for($i = 0; $i < count($new); $i++){
    echo myfunction($new[$i]); 
} 

Also you should set the loop from 0 to the end of the array, so if you'll have more than 4 entries in the array the code should be ok

Comments

0

You can declare an anonymous function instead:

for ($i=0; $i<=3; $i++) {
    // code
    $myFunction = function($value) { /* code */ }
    $myFunction($val);
    // code
}

Comments

0

that is not the right way to do it... first declare the function outside the loop, then call the function in the loop

function myfunction($value) {   
    //Do something
}


$new = array(1,2,3,4);
for($i=0;$i<=3;$i++){
   $val = $new[$i];
   myfunction( $val);  //call function where u wanted... here (in your  case)
   echo $val;
}

Comments

-3

You are not suposed to declare the function inside a loop...

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.