Php print () and printf () function tutorial

Source: Internet
Author: User
Tags php print

Php print () and printf () function tutorial

Definition and usage
The print () function outputs one or more strings.

Syntax

print(strings)
 
 
Parameter Description
strings Required. One or more strings to be sent to the output

Tips and instructions
Note: The print () function does not have actual functions, so you do not need to use parentheses.

Tip: the print () function is slightly lower than echo ().


--------------------------------------------------------------------------------

Example 1

<?php$str = "Who's Kai Jim?";print $str;print "<br />";print $str."<br />I don't know!";?>
Result.
Who's Kai Jim?Who's Kai Jim?I don't know!
Now let's take a look at the printf function.
 

Definition and usage
Output the formatted string in the printf function.

The arg1, arg2, ++ parameter inserts a major string showing signs of percentage (%. Works of this function are "One step ". In first % signs, arg1 inserts, signs in second %, arg2 inserts, etc.

Syntax

printf(format,arg1,arg2,arg++)
 
    
    
Parameter Description
Format Required. The specified string and how to format the variable. Possible format values: %-return percent Number % number B-Binary Number % C group-character according to a SC II value % d-signature decimal number % specialized-scientific symbol (such as 1. 2 e + 2) % u-Unsigned decimal number % Female-floating point number (realized by local settings) % Female-floating point number (not realized by local settings) % s-string % of % o-No octal characters-hexadecimal (lower case) % of devices-additional value of hexadecimal (upper case) format. The % and text (for example, 0.2%, female) between these locations: + (Power Capital + and-the front number. By default, only the negative number is marked) '(specifies what is used as the fill. Default space. Must use the same width. Example: % 'x20s (this uses "X" as the fill)-(left reason variable value) [0-9] (specified variable value with the lowest hold width ). [0-9] (specify the number of decimal places or maximum String Length) Note: if there are multiple additional formats for value, 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

 

Tips and instructions
Note: if there are more signs % of the above arguments, you must use placeholders. Reserved location inserted % signed by argument, quantity and "$ ". See three examples.

Tip: related functions: fprintf (), sprintf (), vfprintf (), vprintf (), and vsprintf ().


--------------------------------------------------------------------------------

Example 1

<?php$str = "Hello";$number = 123;printf("%s world. Day number %u",$str,$number);?>
Output result
Hello world. Day number 123
 
Instance 2.
  
  
<?php$number = 123;printf("%f",$number);?>
Result.
  
  
123.000000

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.