Summary of PHP print function types-PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php print
Summary of the types of PHP print functions. Print function 1 echo () in PHP to output multiple strings at the same time. multiple parameters are allowed without parentheses and no return value is required. PHP print function 2 print () can only output one string at a time PHP print function 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.

PHP print function 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.

PHP printing function 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! ");

PHP print function 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)

 
 
  1. <? Php
  2. $ Num = 100.001;
  3. Printf ("% d", $ num); // 100
  4. Printf ("% s", $ num); // 100.001
  5. Printf ("% s --- % d --- % B --- % x --- % o --- % f", $ num)
  6. // 100.001 --- 100---1100100---64---144---1001.00100
  7. Printf ("%. 2f", $ num); // 100.00 (2 decimal places)
  8. Printf ("%. 1f", $ num); // 100.0 (one decimal point)
  9. Printf ("% '#10 s", $ num); // #### 100.001
  10. Printf ("% '@ 10 s", $ num); // @ 100.001
  11. Printf ("% '_ 10 s", $ num); // ___ 100.001
  12. Printf ("% '#-10 s", $ num); // 100.001 ###
  13. Printf ("% '@-10 s", $ num); // 100.001 @@@
  14. Printf ("% '_-10 s", $ num); // 100.001 ___
  15. Printf ("% '#20 s", $ num); // ############# 100.001
  16. ?>

PHP print function 5 sprintf ();

This cannot be output directly. assign a variable first and then output the variable.

 
 
  1. < ?php
  2. $num=100.001;
  3. $a=sprintf("%d",$num);
  4. echo $a; //100
  5. ?>

PHP print function 6 print_r ();

Function: only outputs an array.

PHP print function 7 var_dump ();

Function: output the content of a variable, type or string, type, and length. It is often used for debugging.

 
 
  1. < ?php
  2. $a=100;
  3. var_dump($a); //int(3) 100
  4. ?>


PHP print function 1 echo () can output multiple strings at the same time. it can have multiple parameters without parentheses and no return value. PHP print function 2 print () can only output one string at a time ,...

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.