0

I need some help with this function to finish my project.

3 variables:

$cityName1 = "New York";
$cityName2 = "Madrid";
$cityName3 = "Paris";

The function:

function cityNameFunction($cityName) {
    $city_name = $cityName;
    return $city_name;
}

Calling the function:

$cityName = array();
for($x = 1; $x <= 3; $x++) {
    $cityName[$x] = ${'cityName'.$x};
}
$cityName1 = cityNameFunction($cityName1);
$cityName2 = cityNameFunction($cityName2);
$cityName3 = cityNameFunction($cityName3);

What do I have to do if I have 2000 cities?

Thanks for any help

4
  • 1
    What should be the use of this code? Commented Dec 27, 2015 at 19:07
  • This is just a example for my real code, otherwise you won't understand my language. I just want to return the name of every city. Commented Dec 27, 2015 at 19:12
  • 1
    Where do you have 2000 cities. What I mean is, in what data structure do you have these city names? Commented Dec 27, 2015 at 19:15
  • 1
    The question makes no sense without more background Commented Dec 27, 2015 at 19:16

2 Answers 2

1

Strange example, but you may write something like this

$cityName = array();
for ($x = 1; $x <= 3; $x++) {
    $cityName[$x] = ${'cityName'.$x};
    ${'cityName'.$x} = cityNameFunction(${'cityName'.$x});
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you mnv. I will try it.
Your code works perfect of my example. Now I will try it on my real code. You will hear it from me:)
0

An example of Itterating over a PHP Function at work:

$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;
    }

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.