使用会话 - PHP

Pet*_*gad 0 php session

使用Sessions时,是否需要声明session_start()或ob_start()?这样做有什么好处呢?

谢谢!

Mik*_*e B 6

session_start() 必须用于存储和读取$ _SESSION全局.

ob_start()与会话完全分开.ob_start()触发输出缓冲,它将所有输出存储在缓冲区中供以后使用.当PHP脚本结束时,缓冲区会自动刷新给用户.或者,您可以在执行中获取缓冲区的内容并调整内容.见下面的例子.

来自php.net的示例:

<?php
function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>
Run Code Online (Sandbox Code Playgroud)

输出:

<html>
<body>
<p>It's like comparing oranges to oranges.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)