Reputation: 3
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
Reputation: 4598
Your code has lots of issues
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
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.
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.
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.
$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.
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
Reputation: 47956
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 :)
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.
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
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
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
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