PHP Output statements

Source: Internet
Author: User
Tags echo command learn php sprintf

Through the previous learning to understand the basic PHP syntax, today to you a brief introduction of several PHP output methods:

1. Echo Common output statements, for example: Echo ' helloworld!   '; 2. Print () Output statement with a return value. For example: Print (' helloworld! ‘); The output returned successfully to 1, and the failure returned 0. 3. printf (); Formats the output string.   For example: printf ("%d,%f", 12,12.3); 4. Print_r (); Output arrays, objects, and other composite data types.  For example: Print_r ($array); 5. Var_dump (); You can determine the type and length of a variable, and output the value of a variable. For example: Var_dump (' helloworld! '); 6. sprintf Functions are also used for string formatting. For example: $formatted = sprintf ("%01.2f", ' 123.1 ');

In order to facilitate our memory, first of these different output methods to make a comparison.

    • echo-can output one or more strings
    • Print-can only output values of simple type variables, such as int,string
    • Print_r-can output values of complex type variables, such as arrays, objects
    • The printf-function is used to format the output string, which is used primarily to replace a format string in a string that begins with%.
    • sprintf-functions are also used for string formatting. This function is basically the same as the printf function, but it can save the converted result to a string variable instead of the direct output. (because it is similar to printf, the following is not a detailed demonstration)
    • Var_dump-Prints information about the variable, including the type and value of the expression, and displays its structure by indentation.

tip:The echo output is faster than print, ECHO is a PHP statement, there is no return value, print and Print_r are PHP functions, the function has a return value.

The print return value is 1 (type int), and the Print_r return value is true (type bool).

Echo

Echo is a language structure that can be used without parentheses or parentheses: Echo or Echo ().

Display string

The following example shows how to use the echo command to output a string (the string can contain HTML tags):

<?phpecho "

Above echo output result:

PHP is fun! Hello world! I ' m about to learn php! This string is made with multiple parameters.

  


Show variables

The following example shows how to use the echo command to output variables and strings:

<?php$txt1= "Learn PHP"; $txt 2= "w3cschool.cn"; $cars =array ("Volvo", "BMW", "Toyota"), Echo $txt 1;echo "<br>"; echo "Study PHP at $txt 2"; Here is a double quotation mark, in PHP, the double quotation marks can parse the variable echo "My car is a {$cars [0]}"; >

Results:

Learn Phpstudy PHP at W3cschool.cnmy car is a Volvo

  

Print Statement

Print is also a language structure, you can use parentheses, or you can use no parentheses: Print or print ().

Display string

The following example shows how to use the Print command to output a string (the string can contain HTML tags):

<?phpprint "

The result of the above operation:

PHP is fun! Hello world! I ' m about to learn php!


Show variables

The following example shows how to use the Print command to output variables and strings:

<?php$txt1= "Learn PHP"; $txt 2= "w3cschool.cn"; $cars =array ("Volvo", "BMW", "Toyota");p rint $txt 1;print "<br>" ;p rint "Study PHP at $txt 2";p rint "My car is a {$cars [0]}";? >

Operation Result:

Learn Phpstudy PHP at W3cschool.cnmy car is a Volvo

  

Print_r statements

Print_r displays easy-to-understand information about a variable, and if given a string,integer , or float, the variable value itself is printed.

If an arrayis given, the keys and elements are displayed in a certain format. object is similar to an array.

Parentheses must be used: Print_r ().

Tip: Print_r () Moves the pointer of the array to the last edge. Use Reset () to get the pointer back to the beginning.

Display string

The following example shows how to use the Print_r command to output a string (the string can contain HTML tags):

<?phpprint_r ("Hello world!"); Print_r ("Goodbye world!");? >

Operation Result:

Hello world! Goodbye World

  

Show variables

The following example shows how to use the Print_r command to output variables and strings:

<?php$txt1= "Hello world!"; $cars =array ("Volvo", "BMW", "Toyota");p Rint_r ($txt 1);p rint_r ($cars);? >

Operation Result:

Hello world! Array ([0] = Volvo [1] = BMW [2] = Toyota) <!--the back array is a printed $cars array--

  

Printf

The printf () function outputs a formatted string.

The arg1,arg2,arg++ parameters are inserted into the percent sign (%) symbol in the main string. This function is executed step-by. At the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on .

Note : If the% symbol is more than the arg parameter, you must use a placeholder. Placeholders are inserted after the% symbol, consisting of numbers and "\$".

Grammar:

Parameters Description
format

Required. Specifies the string and how to format the variable.

Possible format values:

  • %-Returns a percent semicolon%
  • %b-binary
  • %c-ascii value corresponding character
  • %d-contains the decimal number of the sign (negative, 0, positive)
  • %e-scientific notation using lowercase (e.g. 1.2e+2)
  • % E-use uppercase scientific notation (e.g. 1.2E+2)
  • li>%u-Unsigned decimal number (greater than or equal to 0)
  • %f-floating-point number (local setting)
  • % F-floating point (not local)
  • %g-Shorter%e and%f
  • %g-Shorter%E and%f
  • %o-Eight decimal digits
  • %s-string
  • %x-16 decimal (lowercase)
  • % x-16 Decimal (capital letter) the

appended format value. Must be placed between the% and the letter (for example,%.2f):

  • ' (specifies what to use as a fill, and the default is a space.) It must be used with the width of the specified device.
  • -(left adjustment variable value)
  • [0-9] (minimum width of the specified variable value)
  • . [ 0-9] (Specify number of decimal digits or maximum string length)

Note: If you use more than one of the above format values, they must be used in the order above and cannot be disrupted.

Arg1 Necessary. Specifies The parameter that is inserted into the first% symbol in the format string.
Arg2 Necessary. Specifies The parameter that is inserted into the second% symbol in the format string.
arg++ Optional. Specifies The parameter that is inserted into the format string third to fourth, and so on, in the% symbol.
Tip: Related functions: sprintf (), vprintf (), vsprintf (), fprintf (), and vfprintf ()
<?php$number = 9; $str = "Beijing";p rintf ("There are%u millions bikes at%s. ", $str, $number);? >

Operation Result:

There are 9 million bicycles in Beijing.

Use the format value%f:

<?php$number = 123;printf ("%f", $number);? >

Results:

123.000000 is  omitted here by default to six digits after the decimal point

Use placeholders:

<?php$number = 123;printf ("There are two decimal places:%1\$.2f<br> no decimals:%1\ $u", $number);? >

Results:

There are two decimal places: 123.00 no decimals: 123

  Demo of all possible format values:

&LT;?PHP$NUM1 = 123456789; $num 2 = -123456789; $char = 50; The ASCII character 50 is the 2//Comment: The format value "percent" returns sign printf ("%%b =%b <br>", $num 1); Binary number printf ("%%c =%c <br>", $char); ASCII character printf ("%%d =%d <br>", $num 1); Signed decimal number printf ("%%d =%d <br>", $num 2); Signed decimal number printf ("%%e =%e <br>", $num 1); Scientific counting method (lowercase) printf ("%%e =%E <br>", $num 1); Scientific notation (uppercase) printf ("%%u =%u <br>", $num 1); unsigned decimal number (positive) printf ("%%u =%u <br>", $num 2); unsigned decimal number (negative) printf ("%%f =%f <br>", $num 1); Floating point number (depending on local settings) printf ("%%f =%F <br>", $num 1); Floating-point number (not local setting) printf ("%%g =%g <br>", $num 1); Shorter than%e and%fprintf ("%%g =%G <br>", $num 1); Shorter than%E and%fprintf ("%%o =%o <br>", $num 1); Octal number printf ("%%s =%s <br>", $num 1); String printf ("%%x =%x <br>", $num 1); Hexadecimal number (lowercase) printf ("%%x =%x <br>", $num 1); Hexadecimal number (uppercase) printf ("%%+d =%+d <br>", $num 1); Symbol specifier (positive) printf ("%%+d =%+d <br>", $num 2); Symbol specifier (negative)?>

Operation Result:

 A demo of the string specifier:

<?PHP$STR1 = "Hello"; $str 2 = "Hello world!"; printf ("[%s]<br>", $str 1);p rintf ("[%8s]<br>", $str 1);p rintf ("[%-8s]<br>", $str 1);p rintf ("[%08s] <br> ", $str 1);p rintf (" [% ' *8s]<br> ", $str 1);p rintf (" [%8.8s]<br> ", $str 2);? >

Operation Result:

[Hello] [Hello] [Hello] [000Hello] [***hello] [Hello wo]
   Var_dump

- Prints information about a variable that displays structure information about one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indentation.

<?php$a = Array (1, 2, Array ("A", "B", "C")); Var_dump ($a);? >

Operation Result:

Array (3) {  [0]=>  int (1) precedes the type  [1]=>  int (2)  [2]=>  Array (3) {    [0]=>    String (1) "A"    [1]=>    string (1) "B"    [2]=>    string (1) "C"  }}

  

One more example,

<?php$b = 3.1; $c = True;var_dump ($b, $c);? >

Operation Result:

Float (3.1) bool (TRUE)

  

The output of PHP is written here, there are two other functions can be understood: Die () exit (), after the words I will explain in detail.

Today's time is delayed, and now just finished one, do not stop, another article it ...

PHP Output statements

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.