Home  >  Article  >  Backend Development  >  Summary of commonly used output functions in PHP, php output function_PHP tutorial

Summary of commonly used output functions in PHP, php output function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:18:262965browse

Summary of commonly used output functions in PHP, php output functions

echo(); "Output content";

Can output multiple strings at the same time, can have multiple parameters, does not require parentheses, and has no return value.

print(); has a return value of 1, and 0 can only contain one parameter

Only one string and one parameter can be output at the same time. Parentheses are required and there is a return value. When the execution fails, it returns false.
The usage of print is very similar to that of C language, so % in the output content will be specially interpreted.

Copy code The code is as follows:

$a=print(‘hi’);
echo $a;
//——————————-

hi 1 //1 is the value of $a.

//——————————–


die(); Generally used to judge database connection. Once die() is executed, the content after die() will not be executed

die(); // The difference between and exit().

Has two functions: first output the content, and then exit the program. (commonly used in linked servers and databases)

Copy code The code is as follows:

mysql_connect("locahost","root","root") or die("Link to server failed!");

printf(); The same usage as C language, you can format the output

printf(); //f refers to format formatting

printf(“Parameter 1″, Parameter 2): Parameter 1= what format to output; Parameter 2= output variable.

(% s: according to string; %d: according to integer; %b: according to binary; % x: according to hexadecimal; %X: according to hexadecimal uppercase output; %o: according to octal; % f: by floating point type)
Function, returns the number of output characters, formats the text and then outputs it, such as:

Copy code The code is as follows:

printf (“$%01.2f” , 43.2); //$43.20

$ represents the padding character
0 means there are not enough digits to fill in without affecting the original value
1 represents the total width of the output
2 represents the number of decimal places, with rounding
%f means displaying it as a floating point number

Formatting commands and instructions:

%% prints the percent symbol without conversion.
%b Convert integer to binary.
%c Convert integer to corresponding ASCII character.
%d Convert integer to decimal.
%f times precision number converted to floating point number.
%o Convert integer to octal.
%s Convert integer to string.
%x integer is converted to lower case hexadecimal.
%X Convert integer to uppercase hexadecimal.

Copy code The code is as follows:

$num=100.001;
printf(“%d”,$num); //100
printf(“%s”,$num); //100.001
printf(“%s—%d—%b—%x—%o—%f”,$num,$num,$num,$num,$num,$num)
//100.001—100—1100100—64—144—1001.00100
printf(“%.2f”,$num); //100.00 (keep 2 decimal places)
printf(“%.1f”,$num); //100.0 (retain 1 decimal place)
printf(“%`#10s”,$num); //#10s
printf(“%#10s”,$num); //10s
?>

sprintf; Store the output content in a variable

This cannot be output directly. First assign it to a variable and then output the variable.

Copy code The code is as follows:

$num=100.001;
$a=sprintf(“%d”,$num);
echo $a; //100
?>

print_r(); is used to output array

Function: Only used to output arrays.

Copy code The code is as follows:

$a = array (1, 2, array (“a”, “b”, “c”));
print_r ($a);

Return:
Copy code The code is as follows:

Array ( [0] => 1 [1] => 2 [2] => Array ( [0] => a [1] => b [2] => c ) )

var_dump(); can output any content

Output the content, type, or string content, type, and length of the variable. Often used for debugging.

Copy code The code is as follows:

$a=100;
var_dump($a); //int(100)

$a=100.356;
var_dump($a); //float(100.356)

?>


var_export ();
Returns structural information about the variables passed to this function. It is similar to var_dump(), except that the returned representation is legal PHP code.

You can return the value of a variable by setting the second parameter of the function to TRUE.

Copy code The code is as follows:

$a = array (1, 2, array (“a”, “b”, “c”));
var_export ($a);
/*
Output:

array (
0 => 1,
1 => 2,
2 =>
array (
0 => ‘a’,
1 => ‘b’,
2 => ‘c’,
),
)
*/
$b = 3.1;
$v = var_export($b, TRUE);
echo $v;
/*
Output:

3.1
*/
?>


Simplified usage:

Copy code The code is as follows:

$color = "red";
?>

Roses are


php common functions

It’s not difficult to master some frequently used functions. Use it a few times and remember it.
Then check the manual when you encounter something you don’t know
In fact, it is impossible for everyone who does PHP to know all the functions. Remember, but you must remember the frequently used ones, otherwise it will be too shameful.
The manual is very important, check it out if you don’t know how to do it.

Okay, answer your questions one by one.
You must use functions frequently in your work.
Functions are definitely very important
Frequent use of functions will speed up the efficiency of function learning. For example, if you make a case and use all the available functions, you will remember many functions
Commonly used functions The functions are string processing and array processing

php common functions

In fact, there is no need to search everywhere for so-called "commonly used functions". The so-called "commonly used functions" vary from person to person. Someone often uses a certain function, but it does not mean that you will also use it frequently. There are many PHP functions. It is key to always have a PHP help manual. If you encounter something you are not familiar with, check it out. The more times you check it, the more often it will become "commonly used".

Generally speaking, string, array, and database class functions are relatively commonly used categories.

The functions are listed below for reference only.

==========================================

//================================Time and Date========= ======================
//y returns the last two digits of the year, the four-digit number of year Y, the number of month m, and the English month of M. d number of the month, D day of the week in English
$date=date("Y-m-d");

//include,include_once.require,require_once
//require("file.php" ) Before the PHP program is executed, the files specified by require will be read in. If an error occurs, it will be fatal.
//include("file.php") can be placed anywhere in the PHP program. The file specified by include will not be read until the PHP program is executed. If an error occurs,

//= ==============================Output printing================== =============
//sprintf("%d","3.2") ;//Only formatting, return the formatted string, no output.
//printf("%d","3.2") ;//Formatting and output
//print("3.2") ;//Only output
//echo "nihao" ,"aa";//Can output multiple strings
//print_r(array("a","b","c"));//Display the key values ​​and elements of the array in sequence

//============================== Commonly used string functions========== =====================

//Get the length of the string, how many characters there are, spaces are also counted
$str=" sdaf sd ";
$len=strlen($str);

//Use the string in the first parameter to connect each element in the subsequent array and return a string.
$str=implode("-",array("a","b","c"));

//String splitting method, returns an array, using the first parameter The characters in will split the following string, and intercept the characters before, after, and between the specified characters. If the specified character is at the beginning or end, the element at the beginning or end of the returned array will be an empty string
//If it is not divided into a string, Returns a null value to the corresponding element of the array. The last limit returns the length of the array. If there is no limit, it will continue to be divided.
$array=explode("a","asdd...the rest of the text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/882915.htmlTechArticleSummary of commonly used output functions in PHP, php output function echo(); "output content"; can output multiple items at the same time A string, can have multiple parameters, does not require parentheses, and has no return value. prin...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn