Introduction to echo, print, print_r, printf, sprintf, and var_dump in php

Source: Internet
Author: User
Tags string methods
This article introduces echo, print, print_r, printf, sprintf, and var_dump. For more information, see. 1. define echo and use the PHPecho () function to output one or more string methods.

This article introduces echo, print, print_r, printf, sprintf, and var_dump. For more information, see.

1. echo

Definition and usage

The PHP echo () function outputs one or more strings.

Echo "" This method can also be used without brackets.

Syntax

Echo (strings)

Parameter description

Strings required. one or more strings to be sent to the output.

Tips and comments

Note. echo () is actually not a function, so you do not need to use parentheses for it. however, if you want to pass one or more parameters to echo (), a parsing error occurs when brackets are used.

The prompt is that the. echo () function is a little faster than the print () function.

Note: The echo () function can use the simplified syntax. see example 5.

The instance code is as follows:

  1. $ Str = "Who's John Adams? ";
  2. Echo $ str;
  3. Echo" ";
  4. Echo $ str ." I don't know! ";
  5. ?>

Output.

Who's John Adam?

Who's John Adam?

I don't know!

Example 2

  1. Echo "This text spans multiple lines .";
  2. ?>

Output.

This text spans multiple lines.

Example 3

  1. Echo 'this', 'string', 'was', 'Made ', 'With multiple parameters ';
  2. ?>

Output.

This string was made with multiple parameters

Example 4

The difference between single quotes and double quotes. single quotes only output variable names, not values.

  1. $ Color = "red ";
  2. Echo "Roses are $ color"; echo" ";
  3. Echo 'roses are $ color';?>

Output.

Roses are red Roses are $ color

Example 5

Simplified syntax.

  1.   
  2. $ Color = "red ";
  3. ?>

    Roses are

       

II. print

Print () is the same as echo (), but the echo speed is a little faster than print. in fact, it is not a function, so you do not need to use parentheses for it. however, if you want to pass more than one parameter to print (), a parsing error occurs when brackets are used. note that print always returns 1, which is different from echo, that is, print can be used to assign values, but it has no practical significance.

Example.

The instance code is as follows:

  1. $ A = print ("55nav"); // This is allowed
  2. Echo $ a; // The value of $ a is 1.
  3. ?>

III. print_r functions

The print_r function prints easy-to-understand information about variables.

Syntax. mixed print_r (mixed $ expression [, bool return])

If the variable is string, integer or float, its value is directly output. if the variable is an array, a formatted array is output for ease of reading, that is, the format corresponding to the key and value. object objects are similar. print_r has two parameters. The first parameter is a variable, and the second parameter can be set to true. if it is set to true, a string is returned. Otherwise, a Boolean value is returned.

The instance code is as follows:

  1. $ A = "55nav ";
  2. $ C = print_r ($ );
  3. Echo $ c; // The value of $ c is TRUE.
  4. $ C = print_r ($ a, ture );
  5. Echo $ c; // The value of $ c is the string 55nav.
  6. ?>

IV. printf functions

The printf function returns a formatted string.

Syntax. printf (format, arg1, arg2, arg ++)

The format parameter is the conversion format, starting with the percent sign ("%") to the end of the conversion character. below is the possible format value.

* %-Percentage sign returned

* % B-binary number

* % C-ASCII characters

* % D-signed decimal number

* % E-resumable counting (for example, 1.5e + 3)

* % U-unsigned decimal number

* % F-floating point number (local settings aware)

* % F-floating point number (not local settings aware)

* % O-octal values

* % S-string

* % X-hexadecimal (lowercase letter)

* % X-hexadecimal number (uppercase letters)

Parameters such as arg1, arg2, arg ++ are inserted to the percent sign (%) in the main string. this function is executed step by step. in the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on. if the % symbol is greater than the arg parameter, you must use a placeholder. after the placeholder is inserted with the % symbol, it consists of numbers and "$. you can use numbers to specify the displayed parameters. For more information, see the example.

The instance code is as follows:

  1. Printf ("My name is % s.", "55nav", "com"); // My name is 55nav com.
  2. Printf ("My name is % 1 $ s % 1 $ s", "55nav", "com "); // add 1 $ or 2 $ ..... the position where the following parameters are displayed. this line outputs My name is Ricky because only the first parameter is displayed twice.
  3. Printf ("My name is % 2 $ s % 1 $ s", "55nav", "com"); // My name is com 55nav
  4. ?>

5. function/reschedule HTM target = _ blank> sprintf letterQuantity

The format parameter is the conversion format, starting with the percent sign ("%") to the end of the conversion character. the possible format values below.

%-Percentage sign returned

% B-binary number

% C-characters based on ASCII values

% D-signed decimal number

% E-scientific notation (for example, 1.5e + 3)

% U-unsigned decimal number

 

% F-floating point number (local settings aware)

% F-floating point number (not local settings aware)

% O-octal characters % s-string

% X-hexadecimal (lowercase letter)

% X-hexadecimal (uppercase letters)

Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. this function is executed step by step. in the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.

Tips and comments

Note. if the % symbol is greater than the arg parameter, you must use a placeholder. placeholder after the % symbol, which consists of numbers and "$". See Example 3.

Tips: Related functions: fprintf (), printf (), vfprintf (), vprintf (), and vsprintf ().

The instance code is as follows:

  1. $ Str = "Hello ";
  2. $ Number = 123;
  3. $ Txt = sprintf ("% s world. Day number % u", $ str, $ number );
  4. Echo $ txt;
  5. ?>

Output.

Hello world. Day number 123

  1.  
  2. $ Number = 123;
  3. $ Txt = sprintf ("% f", $ number );
  4. Echo $ txt;
  5. ?>

Output.

123.000000

  1. $ Number = 123;
  2. $ Txt = sprintf ("With 2 decimals. % 1 $. 2f With no decimals. % 1 $ u ", $ number );
  3. Echo $ txt;
  4. ?>

Output.

With 2 decimals. 123.00 With no decimals. 123

PHP String function

VI. var_dump function

Var_dump (PHP 3> = 3.0.5, PHP 4, PHP 5)

Var_dump -- print information about a variable

Void var_dump (mixed expression [, mixed expression [,...])

This function displays the structure information about one or more expressions, including the expression type and value. The array recursively expands the value and displays its structure through indentation.

Prompt. to prevent the program from outputting the result directly to the browser, you can use the output-control function to capture the output of this function, and save them to a variable of the string type, for example.

We can compare var_dump () and print_r ().

The instance code is as follows:

  1.  
  2.   
  3. $ A = array (1, 2, array ("a", "B", "c "));
  4. Var_dump ($ );
  5. /* Output.
  6. Array (3 ){
  7. [0] =>
  8. Int (1)
  9. [1] =>
  10. Int (2)
  11. [2] =>
  12. Array (3 ){
  13. [0] =>
  14. String (1) ""
  15. [1] =>
  16. String (1) "B"
  17. [2] =>
  18. String (1) "c"
  19. }
  20. }
  21. */
  22. $ B = 3.1;
  23. $ C = TRUE;
  24. Var_dump ($ B, $ c );
  25. /* Output.
  26. Float (3.1)
  27. Bool (true)
  28. */
  29. ?>
  30.   

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.