concatanate 2 strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Desmond

    concatanate 2 strings

    Can someone please tell me how to concatanate to strings like this
    please


    $name = $_POST["Name"];
    $from = $_POST["Email"];

    $headers = "From: $name $from\r\n";

    i beleve in C there is a strcat() is this similar.
    if so is there an online site for PHP function.

    Desmond.

  • Philip Ronan

    #2
    Re: concatanate 2 strings

    "Desmond" wrote:
    [color=blue]
    > Can someone please tell me how to concatanate to strings like this
    > please
    >
    >
    > $name = $_POST["Name"];
    > $from = $_POST["Email"];
    >
    > $headers = "From: $name $from\r\n";[/color]

    Did you try that? It ought to work exactly as you wrote it.

    However, if you want a valid "From" header, put the email address in angle
    brackets like this:

    $headers = "From: $name <$from>\r\n";

    And please check that the $_POST[] data is valid. For example, check that
    the email address is correctly formatted, and that the "Name" data doesn't
    contain any newline characters. You might also need to apply stripslashes()
    to the data from your form.

    The PHP documentation is here: <http://www.php.net/docs.php>

    --
    phil [dot] ronan @ virgin [dot] net



    Comment

    • Jacob Atzen

      #3
      Re: concatanate 2 strings

      On 2005-06-20, Desmond <pd_otoole@hotm ail.com> wrote:[color=blue]
      > Can someone please tell me how to concatanate to strings like this
      > please
      >
      >
      > $name = $_POST["Name"];
      > $from = $_POST["Email"];
      >
      > $headers = "From: $name $from\r\n";
      >
      > i beleve in C there is a strcat() is this similar.
      > if so is there an online site for PHP function.[/color]

      In PHP the concatenation operator is the dot. So you can concatenate two
      strings like:

      $concatenated = $string1.$strin g2;

      This is also described in the manual (which is really a great ressource
      for PHP developers):

      <http://www.php.net/manual/en/language.types. string.php>

      --
      Cheers,
      - Jacob Atzen

      Comment

      • Desmond

        #4
        Re: concatanate 2 strings

        Thanks I have seen some code on the internet and it uses a full stop

        $aaa . $bbb

        and I have seen $zzz .= $vvvv is the .= rthe equivelent as +=

        Desmond.

        Comment

        • Kimmo Laine

          #5
          Re: concatanate 2 strings

          "Desmond" <pd_otoole@hotm ail.com> kirjoitti
          viestissä:11192 84581.016755.16 170@o13g2000cwo .googlegroups.c om...[color=blue]
          > Thanks I have seen some code on the internet and it uses a full stop
          >
          > $aaa . $bbb[/color]

          That works too.
          [color=blue]
          > and I have seen $zzz .= $vvvv is the .= rthe equivelent as +=[/color]

          That also works.

          Full stop is the concatenation operator, but php also has the quite unique
          mechanism for inserting variables directly into strings.

          $foo .= $bar;
          is just short for
          $foo = $foo . $bar; and they both concatenate the two variables.
          But this works as well, just in a slightly different way:
          $foo = "$foo$bar";

          Variables inside double qoutes are replaced with their values. inside single
          qoutes they'r not evaluated, but interpreted literally.

          --
          "I am pro death penalty. That way people learn
          their lesson for the next time." -- Britney Spears

          eternal.erectio nN0@5P4Mgmail.c om


          Comment

          • luke

            #6
            Re: concatanate 2 strings


            Not wanting to repeat what has been written, but I think someone should
            point out that the concatenation of global variables has to be done with the
            .. (dot) operator, whereas using local variables, like you have, you can
            directly stick strings together _or_ use the . operator.

            So these are all ok:

            $bigstring = "$first $second";
            $bigstring = "$first some non variable text $second";
            $bigstring = $first . $second;
            $bigstring = $first . " some non variable text " . $second;

            But with globals only . concatenation is ok:

            $bigstring = $_POST['Name'] . $_POST['Email'];
            $bigstring = "normal text with $localvariable" . $_POST['Name'] . "more
            text";

            (above code has not been tested in real-life) :>

            Luke

            "Desmond" <pd_otoole@hotm ail.com> wrote in message
            news:1119282029 .932162.16230@f 14g2000cwb.goog legroups.com...[color=blue]
            > Can someone please tell me how to concatanate to strings like this
            > please
            >
            >
            > $name = $_POST["Name"];
            > $from = $_POST["Email"];
            >
            > $headers = "From: $name $from\r\n";
            >
            > i beleve in C there is a strcat() is this similar.
            > if so is there an online site for PHP function.
            >
            > Desmond.
            >[/color]


            Comment

            • Norman Peelman

              #7
              Re: concatanate 2 strings

              "luke" <lduncalfe@eml. nope> wrote in message
              news:ulaue.9436 $U4.1244811@new s.xtra.co.nz...[color=blue]
              >
              > Not wanting to repeat what has been written, but I think someone should
              > point out that the concatenation of global variables has to be done with[/color]
              the[color=blue]
              > . (dot) operator, whereas using local variables, like you have, you can
              > directly stick strings together _or_ use the . operator.
              >
              > So these are all ok:
              >
              > $bigstring = "$first $second";
              > $bigstring = "$first some non variable text $second";
              > $bigstring = $first . $second;
              > $bigstring = $first . " some non variable text " . $second;
              >
              > But with globals only . concatenation is ok:
              >
              > $bigstring = $_POST['Name'] . $_POST['Email'];
              > $bigstring = "normal text with $localvariable" . $_POST['Name'] . "more
              > text";
              >
              > (above code has not been tested in real-life) :>
              >[/color]

              This is fine and works perfectly (you can drop the quotes around the key
              name when inside double quotes):

              $localvariable = 'white background';
              //$_POST['Name'] previously set to 'John'
              $bigstring = "normal text with $localvariable for $_POST[Name] to read.";

              outputs: normal text with white background for John to read.

              and...

              $localvariable = 'white background';
              //$_POST['Name'] previously set to 'John'
              $bigstring = "normal text with $localvariable for '$_POST[Name]' to read.";

              outputs: normal text with white background for 'John' to read.


              Norm
              --
              FREE Avatar hosting at www.easyavatar.com


              Comment

              Working...