What does %S mean in PHP, HTML or XML?

79,977

Solution 1

with printf or sprintf characters preceded by the % sign are placeholders (or tokens). They will be replaced by a variable passed as an argument.

Example:

$str1 = 'best';
$str2 = 'world';

$say = sprintf('Tivie is the %s in the %s!', $str1, $str2);
echo $say;

This will output:

Tivie is the best in the world!

Note: There are more placeholders (%s for string, %d for dec number, etc...)


Order:

The order in which you pass the arguments counts. If you switch $str1 with $str2 as

$say = sprintf('Tivie is the %s in the %s!', $str2, $str1); 

it will print

"Tivie is the world in the best!"

You can, however, change the reading order of arguments like this:

$say = sprintf('Tivie is the %2$s in the %1$s!', $str2, $str1);

which will print the sentence correctly.


Also, keep in mind that PHP is a dynamic language and does not require (or support) explicit type definition. That means it juggles variable types as needed. In sprint it means that if you pass a "string" as argument for a number placeholder (%d), that string will be converted to a number (int, float...) which can have strange results. Here's an example:

$onevar = 2;
$anothervar = 'pocket';
$say = sprintf('I have %d chocolate(s) in my %d.', $onevar, $anothervar);
echo $say;

this will print

I have 2 chocolate(s) in my 0.

More reading at PHPdocs

Solution 2

In printf, %s is a placeholder for data that will be inserted into the string. The extra arguments to printf are the values to be inserted. They get associated with the placeholders positionally: the first placeholder gets the first value, the second the second value, and so on.

Solution 3

%s is a type specifier which will be replaced to valuable's value (string) in case of %s.

Besides %s you can use other specifiers, most popular are below:

d - the argument is treated as an integer, and presented as a (signed) decimal number.

f - the argument is treated as a float, and presented as a floating-point number (locale aware).

s - the argument is treated as and presented as a string.

Solution 4

$num = 5; 
$location = 'tree';

$format = 'There are %d monkeys in the %s'; 
echo sprintf($format, $num, $location); 

Will output: "There are 5 monkeys in the tree."

Solution 5

The printf() or sprintf() function writes a formatted string to a variable. Here is the Syntax:

sprintf(format,arg1,arg2,arg++)

format:

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greater than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

arg1:

  • The argument to be inserted at the first %-sign in the format string..(Required.)

arg2:

  • The argument to be inserted at the second %-sign in the format string. (Optional)

arg++:

  • The argument to be inserted at the third, fourth, etc. %-sign in the format string (Optional)

Example 1:

$number = 9;
$str = "New York";
$txt = sprintf("There are approximately %u million people in %s.",$number,$str);
echo $txt;

This will output:

There are approximately 9 million people in New York.

The arg1, arg2, arg++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "\$". Let see another Example:

Example 2

$number = 123;
$txt = sprintf("With 2 decimals: %1\$.2f
<br>With no decimals: %1\$u",$number);
echo $txt;

This will output:

With 2 decimals: 123.00
With no decimals: 123

Another important tip to remember is that:

With printf() and sprintf() functions, escape character is not backslash '\' but rather '%'. Ie. to print '%' character you need to escape it with itself:

printf('%%%s%%', 'Nigeria Naira');

This will output:

%Nigeria Naira%

Feel free to explore the official PHP Documentation

Share:
79,977
Wolfpack'08
Author by

Wolfpack'08

Updated on April 07, 2020

Comments

  • Wolfpack'08
    Wolfpack'08 about 4 years

    I'm looking at Webmonkey's PHP and MySql Tutorial, Lesson 2. I think it's a php literal. What does %s mean? It's inside the print_f() function in the while loops in at least the first couple of code blocks.

    printf("<tr><td>%s %s</td><td>%s</td></tr>n", ...

  • Mike Christensen
    Mike Christensen almost 12 years
    +1 - BTW, the correct term for this is string interpolation.
  • David
    David almost 12 years
    Thanks for the edit, Ned. I had four edits, and it still didn't work. Markdown must have messed up for a second.
  • Fabrício Matté
    Fabrício Matté almost 12 years
    Same syntax as C's printf, apparently.
  • Wolfpack'08
    Wolfpack'08 almost 12 years
    I think your example alongside with Ned's explanation make a fairly solid answer. Question: if I write sprintf('Tivie is the %s in the %s!', $str2, $str2);, will it echo "Tivie is the world in the best!"?
  • Tivie
    Tivie almost 12 years
    it will output "Tivie is the world in the world" since str2 = world,
  • Wolfpack'08
    Wolfpack'08 almost 12 years
    Hooray for typo's. I got a laugh out of it. So %strn returns the value of %s[n], so to speak? With that same notation, %s %s returns the values of %s[0] %s[1]?
  • Tivie
    Tivie almost 12 years
    Yes. Although you can name the variables whatever you want, its the order in which they are passed that counts. The first placeholder takes the value of the first passed argument, the second placeholder the value of the second argument, and so on. The letter after the % sign tells sprint what type of variable it is (integer, string, etc...)
  • Wolfpack'08
    Wolfpack'08 almost 12 years
    Thank you. I'll have to experiment with it for a while because I'm at the point where I'll be able to do that, now. I got one more for you: I'm trying to learn Node.js. Do you know if there's anything equivalent, since their MySQL driver is so different?
  • Tivie
    Tivie almost 12 years
    Humm... heres a list en.wikipedia.org/wiki/…
  • Pankaj Bisht
    Pankaj Bisht almost 8 years
    how we handle, $str = "tst"; sprintf("Congratulations %s basic installation completed!\n\nThank you for choosing %s!", $str, $str);