Common type conversion characters of php output functions-php Tutorial

Source: Internet
Author: User
Php output function Common type conversion character PHP

The output functions include:

Echo ()

Functions and

Print ()

Functions and formatted output functions

Printf ()

Functions and

Sprintf ()

Function.

I,

Echo ()

Function

Echo ()

The function outputs one or more strings, which can be enclosed in parentheses or not. In practice,

We generally do not need parentheses;

Echo

It is more like a statement with no return value.

Echo

(

"

The weather is good today. let's go out and play!

"

).

"
"

;

Echo

"

The weather is good today. let's go out and play!

"

;

?>

The output in the browser is:

The weather is good today. let's go out and play!

The weather is good today. let's go out and play!

II,

Print ()

Function

Print ()

The function outputs one or more strings,

Parentheses can be used,

Or not,

In practical applications,

We generally do not need parentheses;

Print ()

A function has a return value. its return value is

1

, When the execution fails (compared

Returns if disconnected.

Flase

.

3

Print

(

"

The weather is good today. let's go out and play!

"

).

"
"

;

Print

"

The weather is good today. let's go out and play!

"

.

"
"

;

Echo print

"

The weather is good today. let's go out and play!

"

;

?>

The output in the browser is:

The weather is good today. let's go out and play!

The weather is good today. let's go out and play!

The weather is good today. let's go out and play!

1

First,

The second sentence is output.

"The weather is good today,

Let's go out and play!

"

,

Third, besides the output

"Today

The weather is good. let's go out and play !" The returned value"

1

.

Echo ()

Functions and

Print ()

Functions have almost identical functions. The difference is that

Echo ()

Letter

Quantity

No return value,

Print ()

A function returns a value. And

Echo ()

Functions are faster

Print ()

Function.

III,

Printf ()

Function

Printf ()

Function output formatted string.

The formatted string contains two parts:

Part

Yes

Normal characters, which will be output as is; the other part is to format the specified character,

%

"Start,

It is followed by one or several specified characters to determine the output content format.

A parameter table is a series of parameters to be output,

The number must be the same as the output parameter described in the formatted string.

There are as many numbers as possible. each parameter is separated by a comma (,) and the order is one to one.

.

Common type conversion characters

% B

Convert integer to binary

% C

Integer conversion

ASCII

Code

% D

Convert integer to signed decimal

% F

Convert precision to floating point

% O

Integer to octal

% S

Integer to string

% U

Convert integer to unsigned decimal

% X

Convert integer to hexadecimal

(

Lowercase

)

4

% X

Convert integer to hexadecimal

(

Uppercase

)

$

=

"

Today

"

;

$ B

=

10

;

Printf (

"% S

I bought

% U

This book

"

,

$

,

$ B

);

?>

The content output by the browser is:

I bought it today

10

This book

Printf ()

A function has a return value. The return value is the length of the string.

$

=

"

Today

"

;

$ B

=

10

;

Echo

Printf (

"% S

I bought

% U

This book

"

,

$

,

$ B

);

?>

The browser outputs the following content:

I bought it today

10

This book

16

Where

"

I bought it today

10

This book

"

Is

Printf ()

The formatted string of the function,

"

16

"

Is

Printf ()

Letter

Return value of number ?? String length

16

, You need to pass

Echo

Can be output.

IV,

Sprintf ()

Function

Sprintf ()

Functions and

Printf ()

Functions are similar,

Printf ()

The return value of the function is the length of the string,

While

Sprintf ()

The return value side of the function is the string itself.

Therefore,

Sprintf ()

The function must pass

Echo

Can be output.

$

=

"

Today

"

;

$ B

=

10

;

Echo

Sprintf (

"% S

I bought

% U

This book

"

,

$

,

$ B

);

?>

We can see in the browser that the output is

I bought it today

10

This book, if omitted

Echo

, Then browse

Is empty.

Sprintf ()

And

Printf ()

Usage and

C

Language

Printf ()

Very similar. We often use

Sprintf ()

Set

5

Convert decimal to other Hexadecimal formats. For example:

$

=

12

;

Echo

Sprintf (

"% B"

,

$

);

?>

Output in the browser:

1100

Coming Soon

12

Convert to binary

1100

.

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.