printf / sprintf function usage

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

    #1

    printf / sprintf function usage

    Hi,

    In awk I can do this:

    var1="x";
    temp = sprintf("Variab le 1: %s Variable 2: %%s", var1);
    # now the value of temp is "Variable 1: x Variable 2: %s"

    var2="y";
    printf(temp,var 2);

    The above will output: "Variable 1: x Variable 2: y"

    Can I do a similar operation with printf/sprintf in PHP?

    Thanks


  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: printf / sprintf function usage

    Dave wrote:
    Can I do a similar operation with printf/sprintf in PHP?
    Yes.

    RTFM on sprintf and printf: http://php.net/printf ; http://php.net/sprintf

    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Un ordenador no es un televisor ni un microondas, es una herramienta
    compleja.

    Comment

    • Dave

      #3
      Re: printf / sprintf function usage

      "Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
      wrote in message news:fk6k09$blu $1@hercules.coh p1...
      Dave wrote:
      >
      >Can I do a similar operation with printf/sprintf in PHP?
      >
      Yes.
      >
      RTFM on sprintf and printf: http://php.net/printf ; http://php.net/sprintf
      I did, read the manual before posting. There is nothing mentioning the usage
      I have described in my original post. Note question was, whether I could use
      %%s in the first instance of sprintf and than use the resulting %s literal
      as a placeholder to the second printf.


      Comment

      • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

        #4
        Re: printf / sprintf function usage

        Dave wrote:
        Note question was, whether I could use %%s in the first instance of
        sprintf and than use the resulting %s literal as a placeholder to the
        second printf.
        Yes.

        --
        ----------------------------------
        Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

        Q: What's the difference between an Irish wedding and an Irish wake?
        A: One less drunk.

        Comment

        • Jerry Stuckle

          #5
          Re: printf / sprintf function usage

          Dave wrote:
          "Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
          wrote in message news:fk6k09$blu $1@hercules.coh p1...
          >Dave wrote:
          >>
          >>Can I do a similar operation with printf/sprintf in PHP?
          >Yes.
          >>
          >RTFM on sprintf and printf: http://php.net/printf ; http://php.net/sprintf
          >
          I did, read the manual before posting. There is nothing mentioning the usage
          I have described in my original post. Note question was, whether I could use
          %%s in the first instance of sprintf and than use the resulting %s literal
          as a placeholder to the second printf.
          >
          >
          >
          Dave,

          No, it didn't say that in so many words - but the rules sprintf() and
          printf() follow are in there.

          And yes, it works just fine. Did you try it?

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Dave

            #6
            Re: printf / sprintf function usage


            "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
            news:G9mdnbQF_d JYbvvanZ2dnUVZ_ v7inZ2d@comcast .com...
            Dave wrote:
            >"Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
            >wrote in message news:fk6k09$blu $1@hercules.coh p1...
            >>Dave wrote:
            >>>
            >>>Can I do a similar operation with printf/sprintf in PHP?
            >>Yes.
            >>>
            >>RTFM on sprintf and printf: http://php.net/printf ;
            >>http://php.net/sprintf
            >>
            >I did, read the manual before posting. There is nothing mentioning the
            >usage I have described in my original post. Note question was, whether I
            >could use %%s in the first instance of sprintf and than use the resulting
            >%s literal as a placeholder to the second printf.
            >>
            >>
            >>
            >
            Dave,
            >
            No, it didn't say that in so many words - but the rules sprintf() and
            printf() follow are in there.
            >
            And yes, it works just fine. Did you try it?
            >
            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===
            >
            Jerry,

            Yes, I tried the concept. For some reason it did not work in the real code I
            was using, but the awk example I provided in my original post worked when I
            rewrote for PHP. So I now know it's working. Thanks:
            <?php
            /*
            # awk example
            var1="x";
            temp = sprintf("Variab le 1: %s Variable 2: %%s", var1);
            var2="y";
            printf(temp,var 2);
            # In awk the above will output: "Variable 1: x Variable 2: y"
            */
            $var1="x";
            $temp = sprintf("Variab le 1: %s Variable 2: %%s", $var1);
            $var2="y";
            printf($temp,$v ar2);
            // In PHP the above will output: "Variable 1: x Variable 2: y"
            ?>


            Comment

            Working...