Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String concatination

    I have this code:

    for($i=1; $i<=6; $i++)
    {
    echo "<br>picture.$i._name";
    }

    The output of this is:
    picture.1._name
    picture.2._name
    picture.3._name
    picture.4._name
    picture.5._name
    picture.6._name

    How can I get it to print this instead:
    picture1_name
    picture2_name
    picture3_name
    picture4_name
    picture5_name
    picture6_name

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Either

    PHP Code:
    for($i=1$i<=6$i++) 

    echo 
    "<br>picture${i}_name"

    or

    PHP Code:
    for($i=1$i<=6$i++) 

    echo 
    "<br>picture{$i}_name"

    or

    PHP Code:
    for($i=1$i<=6$i++) 

    echo 
    "<br>picture" $i "_name"

    All should do the job. For reference, read the section about simple and complex syntax for variable parsing at http://www.php.net/manual/en/languag...string.parsing

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    for($i=1; $i<=6; $i++)
    {
    echo "<br>picture".$i."_name";
    }
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you tell me why this isn't working:

    for($i=1; $i<=6; $i++)
    {
    if($this->picture".$i."_name == "")
    continue;
    }

    Only get parse error in the if-test

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    not enough info to go on there

    is that code part of a class function ? where is $this coming from ?

    the concat is wrong i.e. it should be


    $this->picture.$i."_name";

    but if you are saying that there is an object variable called eg
    $this->picture1_name

    then you may have to do something similar to.


    for($i=1; $i<=6; $i++)
    {
    $temp=$this->picture.$i."_name";
    echo ${$temp};
    }

    but without knowing what $this is is hard to say
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i want to test if the objects $picture1_name is empty...
    got a solution to it:

    if($this->{"picture".$i."_name"} == "")
    continue;

    thanx anyway


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •