Summary of commonly used Output Functions in PHP, php output function _ PHP Tutorial

Source: Internet
Author: User
Summary of commonly used Output Functions in PHP, and php output functions. Summary of commonly used Output Functions in PHP, php output function echo (); output content; multiple strings can be output at the same time, multiple parameters are allowed, no parentheses are required, and no return value is required. Summary of common output functions in prin PHP, and php output functions

Echo ();"Output Content ";

Multiple strings can be output at the same time, and multiple parameters are allowed. parentheses are not required and no return value is required.

Print ();A switch with a return value of 1 to 0 can only contain one parameter.

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.

The code is as follows:


$ A = print ('hi ');
Echo $;
//----------

Hi 1 // 1 is the value of $.

//----------


Die ();It is generally used to determine the database connection. Once die () is executed

Die (); // is different from exit.

There are two functions: output the content first, and then exit the program. (Commonly used in linking servers and databases)

The code is as follows:


Mysql_connect ("locahost", "root", "root") or die ("link server failed! ");


Printf ();Similar to the C language, the output can be formatted.

Printf (); // f indicates format

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; % X: by hexadecimal; % x: by hexadecimal; % o: by octal; % f: by floating point)
Function, returns the number of output characters. after formatting the text, it is output, for example:

The code is as follows:


Printf ("$ % 01.2f", 43.2); // $43.20

$ Indicates the character to be filled
0 indicates that the number of digits is not enough and the original value is not affected.
1 indicates the total output width.
2 indicates the number of decimal places, rounded down
% F indicates a floating point number.

Formatting commands and instructions:

% Indicates the percentage, which is not converted.
% B integer to binary.
The % c integer is converted to the corresponding ASCII character.
% D integer to decimal place.
% F times the precision number to a floating point number.
% O integer to octal.
% S integer to string.
% X integer to lowercase hexadecimal.
% X integer to uppercase hexadecimal.

The code is as follows:


<? Php

$ 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 ("% '# 10s", $ num); // #10 s
Printf ("% # 10s", $ num); // 10 s
?>

Sprintf;Store output content in variables

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

The code is as follows:


<? Php
$ Num = 100.001;
$ A = sprintf ("% d", $ num );
Echo $ a; // The value 100.
?>


Print_r ();Used to output arrays

Function: only outputs an array.

The code is as follows:


$ A = array (1, 2, array ("a", "B", "c "));
Print_r ($ );


Return value:

The code is as follows:


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

Var_dump ();Can output any content

The content, type, and length of the output variable. It is often used for debugging.

The code is as follows:


<? Php
$ A = 100;
Var_dump ($ a); // int (100)

$ A = 100.356;
Var_dump ($ a); // float (100.356)

?>


Var_export ();
Returns the structure information about the variable passed to the function. it is similar to var_dump (). The difference is that the returned table is a valid PHP code.

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

The code is as follows:


<? Php
$ A = array (1, 2, array ("a", "B", "c "));
Var_export ($ );
/*
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:

The code is as follows:


<? Php
$ Color = "red ";
?>

Roses are <? = $ Color?>



Common php functions

It's not hard to master some frequently used functions. it should be remembered several times.
Check the manual if no problem occurs.
In fact, every PHP developer cannot remember all the functions, but the frequently used functions must be remembered. Otherwise, it would be too disappointing.
The manual is very important. if not, check it.

Okay, answer your questions one by one
Functions are often used.
Functions must be very important.
If you use a function frequently, it will accelerate function learning efficiency. for example, if you use a case to use all the functions that can be used, you will remember a lot of functions.
Common functions are string processing, and array processing.

Common php functions

In fact, there is no need to search for the so-called "common functions". The so-called common functions vary from person to person. someone often uses a function, but it does not mean that you will always use it. There are a lot of php functions. The key is to keep the Help Manual for php. if you are not familiar with it, you can check it more often and it will naturally become "common.

In general, functions of strings, arrays, and databases use a large number of classes.

The following lists the functions for your reference only.

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

/// ================================================ ======================================
// Y returns the last two digits of the year. Y indicates the four digits of the year. m indicates the number of the month. M indicates the English number of the month. D: The number of months. D: the number of weeks.
$ Date = date ("Y-m-d ");

// Include, include_once.require, require_once
// Require ("file. php") reads the file specified by require before executing the PHP program. If an error occurs, it is fatal.
// Include ("file. php") can be stored in any position of the PHP program. only when the PHP program is executed can the file specified by include be read. If an error occurs, a prompt is displayed.

// ==================================== Output print ======== ======================================
// Sprintf ("% d", "3.2"); // format only. the formatted string is returned without output.
// Printf ("% d", "3.2"); // format and output
// Print ("3.2"); // output only
// Echo "nihao", "aa"; // multiple strings can be output.
// Print_r (array ("a", "B", "c"); // displays the key values and elements of the array in sequence.

// ================================ Common string functions ====== ======================================

// Obtain the length of a string, including the number of characters and spaces.
$ Str = "sdaf sd ";
$ Len = strlen ($ str );

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

// String segmentation method. an array is returned. the string is separated by the characters in the first parameter, and the front and back of and between the specified characters are intercepted, if the specified character starts or ends, the element at the beginning or end of the returned array is an empty string.
// Returns a null value to the element corresponding to the array if the string is not split. The last limit returns the length of the array. if there is no limit, the array will be split.
$ Array = explode ("a", "asdd ...... remaining full text>

Optional echo (); "output content"; multiple strings can be output at the same time. multiple parameters are not required, and no return value is required. Prin...

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.