What is printf in php

What is printf used for in PHP?

Solution 1: is being ignored because as stated in documentation first parameter is which should be printed and rest of parameters are values which this format can use, for instance: will print because in format you decided to print only one digit after dot in floating point number. For example, to right-justify and zero-pad a string to 10 characters, but truncate if longer than 10 characters: vs You can easily format numbers in octal, hexadecimal or binary without the clutter of running them through a function, storing the result in a temporary variable and passing it through .

What is printf used for in PHP?

When would I use printf instead of echo in PHP and why? I just don’t understand why it’s important to understand. Thanks!

It is used the same way it is used in C, to substitute formatted values into a format string.

There are literally hundreds of examples of its use on the sprintf manual page.

You can achieve some useful formatting of variables (zero padding, alignment, width etc) which would require an echo accompanied by several function calls.

Читайте также:  Gcc error directory no such file or directory python

For example, to right-justify and zero-pad a string to 10 characters, but truncate if longer than 10 characters:

 $tmp = ''; if (strlen($string) > 10) $tmp = substr($string, 0, 10); else $tmp = str_pad($x, 10, '0', STR_PAD_LEFT); echo $tmp; 

You can easily format numbers in octal, hexadecimal or binary without the clutter of running them through a function, storing the result in a temporary variable and passing it through echo . There are many, many more uses for the printf family of functions.

printf allows you to pass parameters so you can do this:

printf("My name is %s and my favorite color is %s", $name, $color); 

or you can use echo which does the same thing but its not as clean:

echo "My name is $name and my favorite color is $color" 

It just which one you prefer.

  1. printf — Output a formatted string: http://us2.php.net/manual/en/function.printf.php
  2. echo — Output one or more strings: http://us2.php.net/manual/en/function.echo.php

printf() allows you to format the variables in a certain way. For example, you can be sure that something that gets put in %d always gets displayed as a number. You can display floating point numbers with a certain number of digits after the decimal point. You can display numbers in scientific notations. You can convert numbers to octal/hex.

If you wanted to do any of the above using echo, you’d need to call some function or another before putting those variables inside a double-quoted string. Knowing how to use printf() makes your job simpler.

And it gets even more powerful when you can grab the output using sprintf() .

Printf — Why use sprintf function in PHP?, PHP sprintf has all the formatting capabilities actually is missing an important one, %,8d for instance to get the rightly aligned 8 digit and commas …

How to format array out with printf in php

I know the printf statement in PHP can format strings as follows:

//my variable $car = "BMW X6"; printf("I drive a %s",$car); // prints I drive a BMW X6, 

However, when I try to print an array using printf , there does not seem to be a way to format it. Can anyone help?

Here’s an extract from one of the comments on http://php.net/manual/en/function.printf.php:

[Editor’s Note: Or just use vprintf. ]

If you want to do something like:

// this doesn't work printf('There is a difference between %s and %s', array('good', 'evil')); 
printf('There is a difference between %s and %s', 'good', 'evil'); 

You can use this function:

function printf_array($format, $arr)
$goodevil = array('good', 'evil'); printf_array('There is a difference between %s and %s', $goodevil); 
There is a difference between good and evil 

Are you looking for something like this using print_r with true parameter:

printf("My array is:***\n%s***\n", print_r($arr, true)); 

You can’t «print» an array just like that, you’ll have to iterate through it by using foreach , then you can printf all you want with the values. For example:

$cars = array('BMW X6', 'Audi A4', 'Dodge Ram Van'); foreach($cars as $car)

or on your code only place:

Php — How can i print a image using printf?, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Php — How can i print a image using printf?

Im trying to print a image using printf, but it doesnt work.

The column «imagem» show the directory of the image.

function listaCDs() < $sql = "SELECT * FROM `cds`"; if(($rs=$this->bd->executarSQL($sql)))< while ($row = mysql_fetch_array($rs)) < printf(" ", $row['imagem']); echo "
"; > > else < return false; >>
printf("", $row['imagem']); 
printf("", $row['imagem']); 
printf("", $row['imagem']); 

You have double quotes inside double quotes for attributes, which is the problem. Check this demo.

Make sure the image’s directory is public (i.e copy and paste $row[‘imagem’] in a browser to see if it’s accessible).

Php — How to print all properties of an object, or These are the same things you use for arrays too. These will show protected and private properties of …

Why the the newline \n doesn’t work with printf() in PHP

IGNORE THIS QUESTION. ASKED IN ERROR:

What’s the difference between the following 2 printf statements:

printf($variable, "\n"); printf("Hello\n"); 

The newline gets ignored in the 1st printf() statement. But it works fine with the 2nd statement.

The only way I can use the new line is by splitting the 1st statement in to 2 separate statements:

This sounds like a query from an absolute novice however I feel the newline was supported pretty good in Java but not in PHP.

\n is being ignored because as stated in documentation first parameter is format which should be printed and rest of parameters are values which this format can use, for instance:

$num = 2.12; printf("formatted value = %.1f", $num); // ^^^^^^^^^^^^^^^^^^^^^^ ^^^^ // | | // format | // value which can be put in format in place of `%X` where `X` represents type 

will print formatted value = 2.1 because in format %.1f you decided to print only one digit after dot in floating point number.

To make format use string argument you need to use %s placeholder like in case of print(«hello %s world»,»beautiful») which would put beautiful in place of %s and print hello beautiful world .

Now lets back to your code. In printf($variable, «\n»); $variable represents format, and it most probably doesn’t have any %s for string in it which would let you put use «\n» argument in this format. This means that «\n» will be ignored (not used in format) so will not be printed.

doesn’t have this problem because it explicitly use \n in format which should be printed.

So it seems that you may either want to use

which seems like overkill because you can simply concatenate strings using . operator and print them like

Java != PHP

As it is written in official docs:

There are two string operators. The first is the concatenation operator (‘ . ‘), which returns the concatenation of its right and left arguments.

The correct syntax in PHP is:

Do it from \r\n instead of \n

You’re using printf() incorrectly. You need to use %s to represent where the newline character will be placed:

Источник

PHP printf() Function

what is the syntax of the PRINTF() function in php?

Parameter Description
format Required. Specifies the string and how to format the variables in it.Possible format values:%% – 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 greather 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)Additional format values. These are placed between the % and the letter (example %.2f):+ (Forces both + and – in front of numbers. By default, only negative numbers are marked)’ (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %’x20s (this uses “x” as padding)- (Left-justifies the variable value)4 (Specifies the minimum width held of to the variable value).7 (Specifies the number of decimal digits or maximum string length)Note: If multiple additional format values are used, they must be in the same order as above.
arg1 Required. The argument to be inserted at the first %-sign in the format string
arg2 Optional. The argument to be inserted at the second %-sign in the format string
arg++ Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string

PHP PRINTF() method

examples of the PRINTF() function

Examples 1. In this example, we output a formatted string.

Examples 2. In this example, we Using the format value %f.

Examples 3. In this example, we Use of placeholders.

With no decimals: %1\$u",$number); ?>

Источник

PHP printf() Function

The arg1, arg2, ++ 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 «\$». See example two.

Syntax

Parameter Values

  • %% — 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 greather 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)

Additional format values. These are placed between the % and the letter (example %.2f):

  • + (Forces both + and — in front of numbers. By default, only negative numbers are marked)
  • ‘ (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %’x20s (this uses «x» as padding)
  • — (Left-justifies the variable value)
  • 9 (Specifies the minimum width held of to the variable value)
  • .6 (Specifies the number of decimal digits or maximum string length)

Note: If multiple additional format values are used, they must be in the same order as above.

Technical Details

More Examples

Example

Example

Example

A demonstration of all possible format values:

$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

// Note: The format value «%%» returns a percent sign
printf(«%%b = %b
«,$num1); // Binary number
printf(«%%c = %c
«,$char); // The ASCII Character
printf(«%%d = %d
«,$num1); // Signed decimal number
printf(«%%d = %d
«,$num2); // Signed decimal number
printf(«%%e = %e
«,$num1); // Scientific notation (lowercase)
printf(«%%E = %E
«,$num1); // Scientific notation (uppercase)
printf(«%%u = %u
«,$num1); // Unsigned decimal number (positive)
printf(«%%u = %u
«,$num2); // Unsigned decimal number (negative)
printf(«%%f = %f
«,$num1); // Floating-point number (local settings aware)
printf(«%%F = %F
«,$num1); // Floating-point number (not local settings aware)
printf(«%%g = %g
«,$num1); // Shorter of %e and %f
printf(«%%G = %G
«,$num1); // Shorter of %E and %f
printf(«%%o = %o
«,$num1); // Octal number
printf(«%%s = %s
«,$num1); // String
printf(«%%x = %x
«,$num1); // Hexadecimal number (lowercase)
printf(«%%X = %X
«,$num1); // Hexadecimal number (uppercase)
printf(«%%+d = %+d
«,$num1); // Sign specifier (positive)
printf(«%%+d = %+d
«,$num2); // Sign specifier (negative)
?>

Example

A demonstration of string specifiers:

Источник

Оцените статью