Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of basic output statement in PHP

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the usage of basic output sentences in PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of basic output statements in PHP.

Echo

The most basic output statement, not a function, is a language structure and does not require parentheses. You can use a list of parameters, separated by commas. But if you add parentheses, you can't separate the output with a comma. There is no return value.

Echo 'fullstackpm'; / / normal output: fullstackpm

Echo 'fullstackpm',' is', 'Goodboys'; / / normal output: fullstackpm is Good!

Echo ('fullstackpm'); / / normal output: fullstackpm

Echo ('fullstackpm',' is', 'goodie'); / / wrong report

Print

Basically the same as echo, but does not support parameter lists, there are return values. The return value is always 1.

Because there is a return value, it is relatively less efficient than echo.

Print 'fullstackpm'; / / normal output: fullstackpm

Print 'fullstackpm',' is', 'Goodluck'; / / error

$r = print ('fullstackpm'); / / normal output: fullstackpm

Print $r; / / output 1

Printf and sprintf

Two very high-end functions that can format the output string. The placeholder is marked with%, and the placeholder is replaced by the following parameters. The difference between printf and sprintf is that the former outputs directly, while the latter returns the string as a function. Please look at the example.

$str ='My name is% s, I\'m% d years old.'

Printf ($str, 'fullstackpm', 1002); / / Direct output: My name is fullstackpm, Isimm 1002 years old.

$s = sprintf ($str, 'WoW', 12); / / No output here

Print $s; / / output: My name is WoW, Ichimm 12 years old.

The least you need to remember is that% s represents a string,% d represents a number,% f is a floating point number,%% is the output% itself, and there are many other types that can view related documents. There are several similar ones:

Vprintf, whose second argument is an array, not a variable length argument.

Sscanf, some special characters are handled differently.

Fscanf, read from the document and formatted.

Print_r

A very common function that can be formatted as an output array or object. Notice that the second parameter is set to true, so you can return the function instead of directly outputting it.

$str = [

"a"

1 = > "b"

"3" = > "c"

"show" = >'d'

]

Print_r ($str)

/ / output

/ * *

Array

(

[0] = > a

[1] = > b

[3] = > c

[show] = > d

)

, /

$s = print_r ($str, true); / / No output here

Echo $s

/ / output

/ / Note: the output stream is not in ob_start (). Please test this paragraph without any other output.

/ * *

Array

(

[0] = > a

[1] = > b

[3] = > c

[show] = > d

)

, /

Var_dump and var_exports

Var_dump is also a very common function, which is used to display structural information, including types and values. Array objects are expanded and indented to represent the hierarchy. Var_exports is different in that the content returned by var_exports is normal PHP code that can be used directly, and has a second return parameter similar to print_r, which has a similar effect.

$str = [

"a"

1 = > "b"

"3" = > "c"

"show" = >'d'

]

Var_dump ($str)

/ / output

/ * *

Array (4) {

[0] = >

String (1) "a"

[1] = >

String (1) "b"

[3] = >

String (1) "c"

'show' = >

String (1) "d"

}

, /

Var_export ($str)

/ / output

/ * *

Array (

0 = >'a'

1 = >'b'

3 = >'c'

'show' = >' d'

)

* / at this point, I believe you have a better understanding of "the usage of basic output statements in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report