Jump to content

printf function?


eduard
 Share

Recommended Posts

The first argument of the printf() function has a string with special codes where variables are supposed to go. (%s, %d ... and others) The variables from the rest of the function arguments are put in the string where the codes are. The sprintf() page has more information on what each code is for:http://es.php.net/manual/en/function.sprintf.php Here's an example copied from the manual

<?php$num = 5;$location = 'tree';$format = 'There are %d monkeys in the %s';printf($format, $num, $location);?>

Link to comment
Share on other sites

Thanks!As I wrote some times, the php manual is too complicated for a beginner like me!

Edited by eduardlid
Link to comment
Share on other sites

print can output as many variables as you want. A good use for printf() is outputting content in different languages.Here's an example:

<?php$num = 5; if($language == 'english') {  $format = 'There are %d monkeys.';}if($language == 'spanish') {  $format = 'Hay %d monos.';} printf($format, $num);?>

This example isn't perfect because it can be done without printf(), but I'm building a system that supports multiple languages which uses the sprintf() function.

Link to comment
Share on other sites

print can output as many variables as you want. A good use for printf() is outputting content in different languages.Here's an example:
<?php$num = 5; if($language == 'english') {  $format = 'There are %d monkeys.';}if($language == 'spanish') {  $format = 'Hay %d monos.';} printf($format, $num);?>

This example isn't perfect because it can be done without printf(), but I'm building a system that supports multiple languages which uses the sprintf() function.

Many thanks! But now $format. Is that just an ordinary name for a variable?And means this that there will be a selection menu for your system so you can chose what language and isn´t that html? Edited by eduardlid
Link to comment
Share on other sites

Thanks!As I wrote some times, the php manual is too complicated for a beginner like me!
I thought so too in the beginning but just look at the examples supplied on the page. If you have a function you don't know what it does just write php.net/functionnameexample:php.net/printf
Link to comment
Share on other sites

It's a complex system. It involves HTML forms and PHP. You can always choose your own variables names, or even just pass literal values to the function.

printf($a, $;printf($x, $y);printf('Literal %s', 'string');$s = 'string';printf('Variable and literal %ss', $s);

Link to comment
Share on other sites

I thought so too in the beginning but just look at the examples supplied on the page. If you have a function you don't know what it does just write php.net/functionnameexample:php.net/printf
Thanks! But where? URL, Google, php manual?
Link to comment
Share on other sites

It's a complex system. It involves HTML forms and PHP. You can always choose your own variables names, or even just pass literal values to the function.
printf($a, $;printf($x, $y);printf('Literal %s', 'string');$s = 'string';printf('Variable and literal %ss', $s);

I´m sorry, but I still don´t know what the printf function is! I´ll follow the advice of ckrudelux!
Link to comment
Share on other sites

in your addressbar in your browser like this to be more clear:http://www.php.net/printf
But then I´m still in this terrible php manual! What I´m doing: printf in Google! Not perfect, but better! Edited by eduardlid
Link to comment
Share on other sites

But then I´m still in this terrible php manual! What I´m doing: printf in Google! Not perfect, but better!
I can't tell you to like the php manual but you get quite a lot of examples on how you could use the printf function which are submitted from other users. anyway printf is used to make life easier then adding variable content to a string for example: instead of doing this:
$name = "Andreas";print("Hello ". $name ." ! How are you today?");

you could do this:

$name = "Andreas";printf("Hello %s ! How are you today?", $name);

Edited by ckrudelux
Link to comment
Share on other sites

I can't tell you to like the php manual but you get quite a lot of examples on how you could use the printf function which are submitted from other users. anyway printf is used to make life easier then adding variable content to a string for example: instead of doing this:
$name = "Andreas";print("Hello ". $name ." ! How are you today?");

you could do this:

$name = "Andreas";printf("Hello %s ! How are you today?", $name);

Thanks! But what means %s?To make life easier, I found out that you´ve: sprintf function! But the difference with printf is simple! Edited by eduardlid
Link to comment
Share on other sites

It's a placeholder for a string and there are other versions of it you can read about them at the format section from this function sprintf on in the php manual: http://www.php.net/sprintf
So, your advice is to search my questions in the php manual and read the examples there carefully?
Link to comment
Share on other sites

So, your advice is to search my questions in the php manual and read the examples there carefully?
You don't have to search I gave you the link and where to find it. You only need to scrolling down to the format section of that link. form the page I linked to you this is the part you want to read:
The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. This applies to bothsprintf() and printf().Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order:
  • An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the - sign is used on a number if it's negative. This specifier forces positive numbers to have the + sign attached as well, and was added in PHP 4.3.0.
  • An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote ('). See the examples below.
  • An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified.
  • An optional number, a width specifier that says how many characters (minimum) this conversion should result in.
  • An optional precision specifier in the form of a period (`.') followed by an optional decimal digit string that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string.
  • A type specifier that says what type the argument data should be treated as. Possible types:
    • % - a literal percent character. No argument is required.
    • b - the argument is treated as an integer, and presented as a binary number.
    • c - the argument is treated as an integer, and presented as the character with that ASCII value.
    • d - the argument is treated as an integer, and presented as a (signed) decimal number.
    • e - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).
    • E - like %e but uses uppercase letter (e.g. 1.2E+2).
    • u - the argument is treated as an integer, and presented as an unsigned decimal number.
    • f - the argument is treated as a float, and presented as a floating-point number (locale aware).
    • F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3.
    • g - shorter of %e and %f.
    • G - shorter of %E and %f.
    • o - the argument is treated as an integer, and presented as an octal number.
    • s - the argument is treated as and presented as a string.
    • x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
    • X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

and this is what you asked for:
s - the argument is treated as and presented as a string.
This is me telling you what it is and where you can find it
It's a placeholder for a string and there are other versions of it you can read about them at the format section from this function sprintf on in the php manual: http://www.php.net/sprintf
Edited by ckrudelux
Link to comment
Share on other sites

You don't have to search I gave you the link and where to find it. You only need to scrolling down to the format section of that link. form the page I linked to you this is the part you want to read: and this is what you asked for: This is me telling you what it is and where you can find it
I´m afraid I´m getting a php fobia!
Link to comment
Share on other sites

I´m afraid I´m getting a php fobia!
Haha.. well it's a lot to take in so I do understand how hard it can be to understand some of the things. In the beginning maybe you just have to accept the facts, the clues on how things goes together will revel them self later on in your path to becoming a programmer. If you really want to find out what this does I suggest you try it out and see whats happen then you run it.
Link to comment
Share on other sites

Haha.. well it's a lot to take in so I do understand how hard it can be to understand some of the things. In the beginning maybe you just have to accept the facts, the clues on how things goes together will revel them self later on in your path to becoming a programmer. If you really want to find out what this does I suggest you try it out and see whats happen then you run it.
Yes, I think that´s my problem! I want to understand too much!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...