PHP100 excellent: PHP printing function set

Source: Internet
Author: User
1 echo (); 2 print (); 3die (); 4 printf (); 5 sprintf (); 6print_r (); 7var_dump (); 1 echo () multiple strings can be output at the same time, and multiple parameters are allowed. Parentheses are not required and no return value is required. 2 print () can only output one string and one parameter at the same time, which requires parentheses and a return value. When it is executed, 1 echo (); 2 print (); 3 die (); 4 printf (); 5 sprintf (); 6 print_r (); 7 var_dump ();

1 echo ()

Multiple strings can be output at the same time, and multiple parameters are allowed. Parentheses are not required and no return value is required.
2 print ()

Only one string and one parameter can be output simultaneously. Parentheses are required and return values. flase is returned when execution fails. the usage of print is similar to that of C language, so it will make a special explanation of % in the output content.

3 die ();

There are two functions: output the content first, and then exit the program. (Commonly used in linking servers and databases)
Mysql_connect ("locahost", "root", "root") or die ("link server failed! ");

4 printf ();
Printf ("parameter 1", parameter 2): parameter 1 = output in what format; parameter 2 = output variable. ($ S: by string; $ d: by integer; $ B: By binary; $ x: by hexadecimal; $ o: by octal; $ f: by floating point)
   $ Num = 100.001;
Printf ("% d", $ num); // 100
Printf ("% s", $ num); // 100.001
Printf ("% s --- % d --- % B --- % x --- % o --- % f", $ num)
// 100.001 --- 100---1100100---64---144---1001.00100
Printf ("%. 2f", $ num); // 100.00 (2 decimal places)
Printf ("%. 1f", $ num); // 100.0 (one decimal point)
Printf ("% '#10 s", $ num); // #### 100.001
Printf ("% '@ 10 s", $ num); // @ 100.001
Printf ("% '_ 10 s", $ num); // ___ 100.001
Printf ("% '#-10 s", $ num); // 100.001 ###
Printf ("% '@-10 s", $ num); // 100.001 @@@
Printf ("% '_-10 s", $ num); // 100.001 ___
Printf ("% '#20 s", $ num); // ############# 100.001
?>

5 sprintf ();
This cannot be output directly. Assign a variable first and then output the variable.
   $ Num = 100.001;
$ A = sprintf ("% d", $ num );
Echo $ a; // The value 100.
?>

6 print_r ();
Function: only outputs an array.

7 var_dump ();
Function: output the content of a variable, type or string, type, and length. It is often used for debugging.
   $ A = 100;
Var_dump ($ a); // int (3) 100
?>
 

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


Network name: phpinfo (PHP100 Forum)

The upstairs is really wonderful, but I believe some details are beyond my grasp.
Function introduction: several parameters are required. The type of each parameter is required. The parameters are optional, return values, and return values.
Include
Require
Echo ()
The above three can be in the bracket format, or not in the bracket format, such as: include () include ""
Note that:
Echo ("hello ");
?>
The output is hello.
Echo "aaa", 10, "bbb ";
?>
Output result: aaa10bbb
Echo ("aaa", 10, "bbb ");
?>
Incorrect output result
........................................ ........................................ ........................................ ........................................ ......................
Print ()
A parameter with or without parentheses can be output, and multiple parameters cannot be output. The return value is.
If (! Print ("hello php "))
{
Echo "output failed ";
}
Running result hello php
........................................ ........................................ ........................................ ......................
Die ()
The die outputs the content and exits the program.
The output must contain parentheses and only one parameter. We can only write the following parameters:
Die ("###");
?>
Running result ###
........................................ ........................................ .........................
Sprintf ()
Function: sprintf () -- format the string.
Syntax: string sprintf (string format, mixed [args]...);
Return value: string
This function is used to format strings. The format parameter is the conversion format, starting with the percent sign % to the conversion character.
The type code of the conversion description is as follows:
The integer B is converted into binary.
The c integer is converted to the corresponding ASCII character.
D integer to decimal place.
F returns a single precision number to a floating point number.
O integer to octal.
S is converted into a string.
Convert an integer to a lowercase hexadecimal value.
Convert X to uppercase hexadecimal.
........................................ ........................................ ........................................ ......................
Printf ()
Function: printf ()
Output the formatted string.
Syntax: int printf (string format, mixed [args]...);
Return value: integer
This function formats the string according to the content format specified by the parameter format, which is the same as sprintf ().

Printf ("1... the character value of % d is % c)
Printf ("2... control the number of decimal in % f with %. 2f", 5.1234, 5.1234 );
Printf ("3... we can also left-pad number with zeros: % 05f", 33.22 );
Printf ("4... or we can left-pad number and special precision: % 5.2f", 33.22 );
Printf ("5... the hexadecimal representation of % d is % x", 92,92 );
Printf ("6... but you can also write it as % X", 92 );
Printf ("7... and if you were wondering, its octal representation is % o", 92 );
Printf ("8... left-pad the string % s with dashes, like so: % '* 6 s", 'foo', 'bar ');
Printf ("9... and now let's right-pad it with dashes: % '*-6 s", 'foo', 'bar ');
?>
........................................ ........................................ ......................................

6 print_r ();
Function: traverses and outputs the value of an array.
$ Arr = array (1, 2, 4 );
Print_r ($ arr );
?>
The output result is
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)
........................................ ........................................ ........................................ ........................
7 var_dump ();
Function: output the content of a variable, type or string, type, and length. You can quickly understand a variable.
   $ A = 100;
Var_dump ($ a); // int (3) 100
?>
Output result: int (3) 100
 

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.