0

I have a for else loops with 5 sections in and in each one I have the same code just with different variable names:

example:

if($name=="bob"){
    // do things with $name1
}
elseif($name=="bob2"){
    // do things with $name2
}
// ...

if there a way for me to just do a loop where I can have an array of variable names ie $namearray = "$name1","$name2" ..

and then the loop can just replace the variable names on each iteration?

3
  • you answer you own question here. so what happend when you tried it? Commented Oct 20, 2013 at 19:42
  • @ultranaut this has nothing to do with java script Commented Oct 20, 2013 at 19:42
  • There are two interpretations of this question — that you want to deal with variables named $bob, or that you want to deal with strings with the value "bob". It's somewhat unclear which it is that you want. Commented Oct 20, 2013 at 19:58

3 Answers 3

3
$names = array('bob', 'bob2');
// array of processed names

foreach($names as $name){
   // do things with current name, which is in variable $name
   // each itteration it will be next name from your array: bob, bob2
   echo "Current name - $name <br />";
}
Sign up to request clarification or add additional context in comments.

Comments

2
$namearray = array('bob','bill');

foreach ($namearray as $n){
//do something with $n (bob, bill) et al
}

Comments

0
<?php

    $name1 = 'Bob';
    $name2 = 'Sarah';
    $name3 = 'Foo';


    // http://php.net/manual/en/language.types.array.php
    // http://php.net/manual/en/function.array.php
    // http://php.net/manual/en/ref.array.php
    $arrayOfVariableNames = [$name1, $name2, $name3];
    // the above can also alternatively (if the $name variables are not used elsewhere) be written in less code to avoid having less variables 
    /// $arrayOfVariableNames[0] = 'Bob'; $arrayOfVariableNames[1] = 'Sarah'; 
    // or even like
    /// $arrayOfVariableNames = ['Bob', 'Sarah', 'Foo'];


    // http://php.net/manual/en/control-structures.foreach.php
    // http://php.net/manual/en/control-structures.for.php 
    foreach ($arrayOfVariableNames as $key => $name)
    {
        // http://php.net/manual/en/control-structures.switch.php
        // http://stackoverflow.com/questions/15903193/php-switch-vs-if
        // http://phpswitch.com/
        // the content from here could be passed into a(nother) function to keep each part of the code doing its job, and doing it well
        // these can be written with ifs, but if there are many different options a switch is most likely your best bet
        switch ($name)
        {
            case 'Bob':  
                // do things with $name...
                break;

            case 'Sarah':  
                // do things with $name... for example, previously in the code create an array $namesBeginningWith and assign each letter of the alphabet to the array as a key, and the initial value at each position to 0, then increment the count  $namesBeginningWith['S']++; could be done with any language, just for fun
                break;

            case 'Foo':  
                // do things with $name... for example, echo 'Foo is the name, Foo Bar';
                break;

            case 'Bar':  
                // do things with $name... for example, 
                /// you wanted to replace the variable names, so here you could do $arrayOfVariableNames[$key] = 'Bob2'; 
                //// No Bar's allowed! 
                break;

            default:
                // if the name is not one that you know, do something "else" with it!
            break;
        }
    }


    /* 
        // for some extra fun, you asked to "do things with $name2"

        // http://stackoverflow.com/questions/2206387/what-is-a-class-in-php/2206835#2206835
        // http://php.net/manual/en/language.oop5.php
        class Person
        {
            private $name;
            private $age;

            // http://php.net/manual/en/language.oop5.typehinting.php
            // http://www.sitepoint.com/type-hinting-in-php/
            public function doStuffWith(Person $person)
            {
                return ($this->name . ' and ' . $person->getName() . " just did stuff! \n\n");  
            }

            public function setName($name)
            {
                $this->name = $name;
            }
            public function setAge($age)
            {
                $this->age = $age;
            }
            public function getName()
            {
                return $this->name;
            }
            public function getAge()
            {
                return $this->age;
            }
        }

        $person1 = new Person();
        $person1->setName('Bob');
        $person1->setAge(21);

        $person2 = new Person();
        $person2->setName('Sarah');
        $person2->setAge(21);

        $person3 = new Person();
        $person3->setName('Foo');
        $person3->setAge(42);

        $peopleList = 
        [
            $person1->getName() => $person1, 
            $person2->getName() => $person2, 
            $person3->getName() => $person3
        ];

        foreach ($peopleList as $key => $person)
        {
            // if ('Foo' !== $person->getName())
            echo $person->doStuffWith($peopleList['Foo']);
        }
    */

?>

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.