PHP output function Common type conversion character

Source: Internet
Author: User
Tags sprintf
Php

The output function has

Echo ()

function and

Print ()

Functions and formatted output functions

printf ()

Functions and

sprintf ()

Function.

One

Echo ()

Function

Echo ()

The function outputs one or more strings, which can be in parentheses or not, and in practical applications,

We generally do not use parentheses;

Echo

More like a statement, no return value.

Echo

(

"

The weather is very good today, let's go out to play!

"

).

"
"

;

Echo

"

The weather is very good today, let's go out to play!

"

;

?>

The output in the browser is:

The weather is very good today, let's go out to play!

The weather is very good today, let's go out to play!

Two

Print ()

Function

Print ()

function to output one or more strings,

You can use parentheses,

You don't have to.

In practical applications,

We generally do not use parentheses;

Print ()

The function has a return value whose return value is

1

, when its execution fails (than

such as disconnection) is returned

Flase

3

Print

(

"

The weather is very good today, let's go out to play!

"

).

"
"

;

Print

"

The weather is very good today, let's go out to play!

"

.

"
"

;

echo Print

"

The weather is very good today, let's go out to play!

"

;

?>

The output in the browser is:

The weather is very good today, let's go out to play!

The weather is very good today, let's go out to play!

The weather is very good today, let's go out to play!

1

The first sentence,

The second sentence is output

"The weather is fine today,

Let's go out and play!

Third, in addition to the output

"This

The weather is very good, we go out to play! "The return value is also output"

1

Echo ()

function and

Print ()

Functions are almost identical in function, a little different is

Echo ()

Letter

Number

No return value,

Print ()

function has a return value. And

Echo ()

function slightly faster than

Print ()

Function.

Three

printf ()

Function

printf ()

The function outputs a formatted string.

Where the formatted string consists of two parts:

Part

Is

Normal characters, these characters will be output as-is, and the other part is the formatting of the specified character to "

%

Start

followed by one or more specified characters, used to determine the format of the output content.

The parameter table is a series of parameters that need to be output

The number must be the output parameter as indicated by the formatted string

The number is as many, each parameter with "," separate, and the order one by one corresponds, otherwise will appear the intent does not

to the error.

Common type conversion characters

%b

Integer to Binary

%c

Integer turn

Ascii

Code

%d

Integer to signed decimal

%f

Double precision to floating point

%o

Integer to Octal

%s

Integer to String

%u

Integer to unsigned decimal

%x

Integer to hexadecimal

(

Lowercase

)

4

%x

Integer to hexadecimal

(

Capital

)

$a

=

"

Today

"

;

$b

=

10

;

printf

"%s

I bought it.

%u

The book

"

,

$a

,

$b

);

?>

The content of the browser output is:

I bought it today.

10

The book

printf ()

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

$a

=

"

Today

"

;

$b

=

10

;

Echo

printf

"%s

I bought it.

%u

The book

"

,

$a

,

$b

);

?>

The contents of the browser output are:

I bought it today.

10

The book

16

which

I bought it today.

10

The book

For

printf ()

The string after the function is formatted,

16

For

printf ()

Letter

The return value of the number?? The length of the string

16

, you need to pass

Echo

to output.

Four

sprintf ()

Function

sprintf ()

function and

printf ()

function is similar,

printf ()

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

and

sprintf ()

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

So

sprintf ()

function must Pass

Echo

to output.

$a

=

"

Today

"

;

$b

=

10

;

Echo

sprintf

"%s

I bought it.

%u

The book

"

,

$a

,

$b

);

?>

We can see in the browser that the output is

I bought it today.

10

This book, if omitted.

Echo

, then browse

Null in the output of the converter.

sprintf ()

And

printf ()

The usage and

C

In the language

printf ()

Very similar. We often use

sprintf ()

Will

5

Decimal is converted to another binary. Such as:

$a

=

12

;

Echo

sprintf (

"%b"

,

$a

);

?>

The output in the browser is:

1100

Upcoming

12

Switch to binary as

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.