Table of Contents
php中heredoc与nowdoc介绍,phpheredocnowdoc
Home php教程 php手册 php中heredoc与nowdoc介绍,phpheredocnowdoc

php中heredoc与nowdoc介绍,phpheredocnowdoc

Jun 13, 2016 am 09:18 AM
php

php中heredoc与nowdoc介绍,phpheredocnowdoc

Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术。但是现在的一些论坛程序,和部分文章系统,都巧妙的使用heredoc技术,来部分的实现了界面与代码的准分离,phpwind模板就是一个典型的例子。  

1.以

2.位于开始标记和结束标记之间的变量可以被正常解析,但是函数则不可以。在heredoc中,变量不需要用连接符.或,来拼接,如下: 

复制代码 代码如下:


$v=2; 
$a= "abc"$v 
"123" 
EOF; 
echo $a; //结果连同双引号一起输出:"abc"2 "123" 


3.heredoc常用在输出包含大量HTML语法d文档的时候。比如:函数outputhtml()要输出HTML的主页。可以有两种写法。很明显第二种写法比较简单和易于阅读。 

复制代码 代码如下:


function outputhtml(){ 
echo ""; 
echo "

主页";  
echo "主页内容"; 
echo "; 

function outputhtml() 

echo     
   主页 
   主页内容 
    
EOT; 

outputhtml(); 

在heredoc中会自动替换其中的$变量,将命令和输入放在一块,方便 

附:heredoc和nowdoc的区别

heredoc使用 heredoc中的PHP变量建议使用{$name->change()} 大括号括起来,这样可以避免一定的歧义,如果要原样输出可以使用传说中的转义字符 \ ,转义字符本身可以使用转义字符进行输出,即 \ 这种表示方法,大括号之类的这些都需要转义输出。
为了保证的确可用,建议使用 heredoc 的语法,本身也带有转义,因为PHP5.3才引进的nowdoc语法,很多云托管坏境很有可能不支持导致歇菜。
最后强调下,heredoc是从PHP4.0开始引进的,而nowdoc语法则需要5.3版本,因为heredoc包含了nowdoc的功能,所以个人建议还是使用heredoc比较好些。

简单来说:

1、heredoc是动态的 nowdoc是静态的
2、heredoc类似多行的双引号 newdoc类似多行的单引号
3、heredoc是一种专门处理大段字符串的通用处理方案,而nowdoc是php为了弥补动态实现“heredoc”的效率问题而实现的“高效率”的静态版本

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles