百科狗-知识改变命运!
--

print() - 输出字符串 - php 字符串函数

梵高1年前 (2023-11-21)阅读数 21#技术干货
文章标签字符串

print()

(PHP 4, PHP 5, PHP 7)

输出字符串

说明

print(string $arg) : int

输出$arg。

print实际上不是函数(而是语言结构),所以可以不用圆括号包围参数列表。

echo最主要的区别:print仅支持一个参数,并总是返回 1。

参数

$arg

输入数据。

返回值

总是返回1

范例

print() - 输出字符串 - php 字符串函数

Example #1print范例

注释

Note:因为是一个语言构造器而不是一个函数,不能被可变函数调用。

参见

  • echo- 输出一个或多个字符串
  • printf()输出格式化字符串
  • flush()刷新输出缓冲
  • Heredoc syntax
Be careful when using print. Since print is a language construct and not a function, the parentheses around the argument is not required.
In fact, using parentheses can cause confusion with the syntax of a function and SHOULD be omited.
Most would expect the following behavior:

But since the parenthesis around the argument are not required, they are interpretet as part of the argument.
This means that the argument of the first print is
  ("foo") && print("bar")
and the argument of the second print is just
  ("bar")
For the expected behavior of the first example, you need to write: 
I wrote a println function that determines whether a \n or a 
should be appended to the line depending on whether it's being executed in a shell or a browser window. People have probably thought of this before but I thought I'd post it anyway - it may help a couple of people. Examples: Running in a browser: Output: Hello, world!
Running in a shell: Output: Hello, world!\n
the FAQTs article can be found archived at http://web.archive.org/web/20060601063513/http
://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40
(url split to get past the line-length limitation)
Don't rely on parenthesis used for `print` construct:
print 1 . print(2) + 3;
print PHP_EOL;
print 1 . (print(2)) + 3;
mvpetrovich of 2007 could just use single quotes as his string delimiters (see the example in the current documentation).
It's not ALWAYS appropriate, but generally it is best (the Zend Framework coding standards have a good take on this). It yields a number of interesting benefits:
1: Nobody will be tempted to write functions to replace backticks or other characters with double quotes. Such functions may cause a (negligible) loss of efficiency, and maybe other undesired effects.
2: You will be able to use double quotes without escaping. This is recommended (although not required) for HTML and XML attributes, as well as quoted text.
3: The script will hit the browser very slightly slightly faster since PHP doesn't have to scan through the string looking for variables, escaped characters, curly braces or other things.
4: Your code gets ten times easier to read. (as mvpetrovich pointed out)
If, in spite of these four excellent benefits, you really MUST still use double quotes to delimit boring old string constants (and seriously, why would you?), you could use the slightly less favourable single quotes as delimiters for most markup languages.
HTML served as HTML will even let you lay out unquoted attributes (yuck).
It should also be noted though that if you are just printing bare strings, you may as well shut off the php parser. The quickest way to send a string is to write it as plain text, OUTSIDE of the php tags. This will also make your code look excellent in a lot of syntax highlighters.
There are few disadvantages to doing this, if any. Output buffering still works. All your classes and objects and includes remain in place. Your script runs faster. World peace is obtained.
I have written a script to benchmark the several methods of outputting data in PHP: via single quotes, double quotes, heredoc, and printf. The script constructs a paragraph of text with each method. It performs this construction 10,000 times, then records how long it took. In total, it prints 160,000 times and records 16 timings. Here are the raw results.
Outputted straight to browser--
Single quotes: 2,813 ms
...with concatenation: 1,179 ms
Double quotes: 5,180 ms
...with concatenation: 3,937 ms
heredoc: 7,300 ms
...with concatenation: 6,288 ms
printf: 9,527 ms
...with concatenation: 8,564 ms
Outputted to the output buffer--
Single quotes: 8 ms
...with concatenation: 38 ms
Double quotes: 8 ms
...with concatenation: 47 ms
heredoc: 17 ms
...with concatenation: 49 ms
printf: 54 ms
...with concatenation: 52 ms
A nice graph of the script's output can be found here:
http://i3x171um.com/output_benchmarks/ob.gif
So what should you choose to print your text? I found several things out writing this.
First, it should be noted that the print and echo keywords are interchangeable, performance-wise. The timings show that one is probably an alias for the other. So use whichever you feel most comfortable with.
Second, if you've ever wondered which was better, the definitive answer is single quotes. Single quotes are at least four times faster in any situation. Double quotes, while more convenient, do pose a debatably significant performance issue when outputting massive amounts of data.
Third, stay away from heredoc, and absolutely stay away from [s]printf. They're slow, and the alternatives are there.
The source of my script can be found here:
http://i3x171um.com/output_benchmarks/ob.txt
DO NOT RUN THE SCRIPT ON THE INTERNET! Run it instead from localhost. The script outputs ~45 megabytes of text in an html comment at the top of the page by default. Expect the benchmark to take ~45 seconds. If this is too long, you can change the amount of iterations to a lower number (the results scale accurately down to about 1,000 iterations).
I have a small utility run from the command line that processes a potentially huge list of files. As it can take hours to complete, I stuck a 
print '.';
statement in the body of the main loop to prove that something was happening.
For reasons unknown to me, the utiliity suddenly started buffering the output such that it printed nothing until completion, defeating the purpose of the running monitor. Adding flush() statements did nothing. The problem was solved by using
fputs(STDOUT, '.');
but I have no idea why.
This is a simple function for printing debug comments that I didn't think of for a long time. Maybe it'll serve you good too.

It's mostly just to make sure everything is running without having to go through everything and put in echo "Step #whatever has executed" whenever something mysterious isn't working.
An update to the println function I wrote below, this is a more efficient, correct and returns a value (1, always; (print)).

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)