www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Mixing array elements with a variable string

  1. #1
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    108

    resolved [RESOLVED] Mixing array elements with a variable string

    PHP Code:
    $lang = array(
    'city'  => 'Dallas',
    'stateabb'  => 'TX'
    );

    $title "About {$lang['city']}{$lang['stateabb']} USA";
    $title "About " . {$lang['city']} . ", " . {$lang['stateabb']} . " USA";
    $title "About " $lang['city'] . ", " $lang['stateabb'] . " USA";
    $title "About " $lang['city']; . ", " $lang['stateabb']; . " USA";
    $title "About $lang['city'];, $lang['stateabb']; USA"
    I can't seem to get this to work. I either get an error message or the city and state is blank.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    PHP Code:
    // yes:
    $title "About {$lang['city']}{$lang['stateabb']} USA";

    // no (mixing concatenation and complex notation:
    $title "About " . {$lang['city']} . ", " . {$lang['stateabb']} . " USA";

    // yes:
    $title "About " $lang['city'] . ", " $lang['stateabb'] . " USA";

    // no (first semicolon terminates statement):
    $title "About " $lang['city']; . ", " $lang['stateabb']; . " USA";

    // no (need complex notation [curly braces] when using quoted array key within double-quoted string):
    $title "About $lang['city'];, $lang['stateabb']; USA"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,518
    Don't forget the sprintf function, this can also help with string formatting

    PHP Code:
    $title sprintf"About %s, %s USA"$lang['city'], $lang['stateabb']); 
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Quote Originally Posted by \\.\ View Post
    Don't forget the sprintf function, this can also help with string formatting

    PHP Code:
    $title sprintf"About %s, %s USA"$lang['city'], $lang['stateabb']); 
    +1

    And when it gets at all complex, you can break everything into separate lines:
    PHP Code:
    $text sprintf(
        
    "blah blah blah %s blah blah %f blah blah blah %s.",
        
    preg_replace('/\s+/'' '$foo['bar'],
        
    $foo['total'] / $foo['num_entries'],
        
    date('Y-m-d'$foo['date'])
    ); 
    ...which keeps things pretty compact while still being pretty readable (and maintainable).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    108
    Thanks to both of you for the information. It was a great help! I'll study up on the sprintf function, sounds interesting.
    Thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles