Anna
Anna

Reputation: 3

Foreach doesn't work in php

I have a code like this:

<?php

$kode ["J"]= array (20, C, D, F);
$kode ["K"]= array (50, B, G, U);
$kode ["T"]= array (70, V, W);

function kota ($start, $end){
    if (is_array($kode)) {
        foreach ($kode as $kota => $path){
            if ($kota=$end) {
                for ($i=1; $i < count ($kota); $i++){
                    $jalur=$start.$path[$i];
                }
            }
        }
        return $jalur;
    }
}
$start = "J";
$end = "T";
$hasil=kota ($start, $end);
echo "".$hasil;
?>

I want the output to be J-V-W
I don't know what is wrong, can anyone help me? please...

Upvotes: 0

Views: 3207

Answers (5)

luiges90
luiges90

Reputation: 4598

Your code has lots of issues

  1. As others pointed out, in if ($kota=$end) {, you are assigning $end to $kota and always return true i.e. the code in the IF clause always execute

  2. PHP has function scope. Simply put, variables declared inside a function cannot be used outside, and vice versa. Use parameters to pass your $kode into the function.

  3. Use of bare strings i.e. V and W in $kode ["T"]= array (70, V, W); and other places. This is highly recommended against, and PHP does warn you about this.

  4. As other pointed out, $jalur=$start.$path[$i]; would overwrite $jalur every time. The for-loop outside is meaningless. You would use .= the append-to operator. Note that you also need to initialize your variable before using this operator.

  5. $kota is always a string in your code, because in a foreach loop, the variable before => symbol means get the key of the array, and array keys can only be either String or integer. That said, for ($i=1; $i < count ($kota); $i++){ is meaningless because count($kota) cannot be greater than 1 - your for loop actually never runs.

  6. This is blatantly meaningless to append a variable with an empty string as in echo "".$hasil;

I guess this is what you want.

<?php

$kode ["J"] = array (20, 'C', 'D', 'F');
$kode ["K"] = array (50, 'B', 'G', 'U');
$kode ["T"] = array (70, 'V', 'W');

function kota ($kode, $start, $end){
    $jalur = $start;
    if (is_array($kode)) {
        foreach ($kode as $kota => $path){
            if ($kota == $end) {
                for ($i = 1; $i < count($path); ++$i) {
                    $jalur .= '-' . $path[$i];
                }
            }
        }
        return $jalur;
    }
}
$start = "J";
$end = "T";
$hasil = kota($kode, $start, $end);
echo $hasil;
?>

This code give you the string which starts with $start and all other elements except the first element in $kode[$end]

Upvotes: 0

Lix
Lix

Reputation: 47956

Syntatical errors

Looks like you forgot to use the equality operator ==

if ($kota = $end){ ... }

Should be -

if ($kota == $end){ ... }

By using only one equals sign you are actually assigning a value to $kota, not comparing the value to $end as should be done in conditional expressions.

I don't think this is the only thing that is causing trouble here.. but it definitely should be sorted out :)


Variable scope

Another thing I noticed in your code is that you are referencing variables within the kota function that were not defined in it's scope. This means that the $kota array is not accessible within the kota function. You should pass the $kota array to the function so that you can use it within scope of the function. Here is some more info on variable scopes in PHP.


Naming conventions

One final note on your variable name choice... You should possibly think of changing the variable $kota or function kota so that their names are not identical. This will help improve readability and perhaps prevent some mistakes at 4am when you've been debugging the whole night ;)

Upvotes: 4

user4035
user4035

Reputation: 23719

Not sure, that $jalur=$start; is on the correct place, but this script gives what you want:

<?php

$kode ["J"]= array (20, 'C', 'D', 'F');
$kode ["K"]= array (50, 'B', 'G', 'U');
$kode ["T"]= array (70, 'V', 'W');

function kota ($start, $end){
      global $kode;

      if(is_array($kode)){

            foreach ($kode as $kota => $path){

                  if ($kota == $end){
                        $jalur=$start;
                        for ($i=1; $i < count ($path); $i++){
                              $jalur .= "-" . $path[$i];
                        }
                  }
            }
            return $jalur;
      }
}
$start  = "J";
$end    = "T";
$hasil=kota ($start, $end);
echo $hasil;
?>

Upvotes: 0

kms
kms

Reputation: 389

On the line

if ($kota=$end){

you are not comparing, but overwriting the value in $kota, and that is always true.

Also the $kode is not available in the function scope, try adding it to the parameter list, or using global (not advised).

Upvotes: 2

addiedx44
addiedx44

Reputation: 2743

You need to either pass $kode into your function as an argument or call global $kode; inside your function. I'd recommend the former.

Additionally, if ($kota=$end) needs to be if ($kota==$end) as others have mentioned.

Upvotes: 0

Related Questions