SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    printing two variables

    Code:
    <?
    $a=my;
    echo $a
    ?>
    The code above prints "my."


    Code:
    <?
    $b=book;
    echo $b
    ?>
    The code above prints "book."

    I like to prints the two variables together.
    So I made the following code.
    But the code below produces an error.
    Code:
    <?
    $a=my;
    ?> 
    
    <?
    $b=' book';
    ?> 
    
    <?
    echo $a$b
    ?>
    My target result of the code above is printing "my book."

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why all the php open/close tags?

    You need to join the variables in the echo statement-

    Code:
    echo $a.$b;

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by m_c View Post
    Why all the php open/close tags?

    You need to join the variables in the echo statement-

    Code:
    echo $a.$b;
    Thank you. it works fine.

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    note you should not write $a=my; $a=book;
    "my" and "book" are strings here. and strings in php must be enclosed in quotes.

    also, the dot mentioned above is concatenation operator. you can use it not only with echo but everythere when you need to concatenate strings.
    For example
    PHP Code:
    <?
    $a
    ="my";
    $b="book";
    $c=$a." ".$b;
    echo 
    $c;
    ?>


Bookmarks

Posting Permissions

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