Php, Escaping From Html

*

*
*

16.2. Embedding PHP in HTML

You embed PHP code into a standard HTMLpage. For example, here"s how you can dynamicallygenerate the title of an HTML document:

...The portion of the documentis replaced by the contents of the $title PHPvariable. Echo is a basic language statement thatyou can use to output data.

Bạn đang xem: Php, escaping from html

This style is always available và is recommended when your PHP codeneeds khổng lồ be portable lớn many different systems. Embedding PHP withinOne final style, in which the code isbetween and %> tags,is disabled by default:

You can turn on this style with the asp_tagsdirective in your php.ini file. The style ismost useful when you are using Microsoft FrontPage or another HTMLauthoring tool that prefers that tag style for HTML-embeddedscripts.

You canembed multiple statements by separating them with semicolons:

It"s legal khổng lồ switch back và forth between HTML andPHP at any time. For example, if you want to output đầu ra 100 /> tags for somereason, you can bởi vì it:

Of course, using the str_repeat( ) function herewould make more sense.

When you embed PHP code in an HTML file, you need to use the.php tệp tin extension for that file, so that yourweb hệ thống knows lớn send the file to PHP for processing. Or, if youhave configured your web server to use a different extension for PHPfiles, use that extension instead.

Xem thêm: Có Nên Cho Bà Bầu Ăn Ốc Được Không ? Có Bầu 3 Tháng Nên Ăn Ốc Không

When you have PHP code embedded in an HTML page, you can think ofthat page as a PHP program. The bits và pieces of HTML and PHPcombine lớn provide the functionality of the program. A collection ofpages that contain programs can be thought of as a web application.


16.2.1. Including Files

An important feature of PHP isits ability to include files. These files may contain additional PHPtags. When you are designing a web application, you can break outcommon components and place them in a single file. This step makes itmuch easier to change certain aspects in one place later, và havethe change take effect across the entire application. Lớn include afile, use the includekeyword:

The header.inc tệp tin might look as follows:

This example illustrates two important concepts of included files inPHP. First, variables phối in the including tệp tin are automaticallyavailable in the included file. Second, each included tệp tin starts outin HTML mode. In other words, if you want khổng lồ include a file that hasPHP code in it, you have khổng lồ embed that code just as you would anyother PHP code.

Note also that I used the .inc extension here.This is not a special file type, just an arbitrary extension name Ichose. Since your Apache server is not phối up lớn treat.inc files as PHP files, if you put this filesomewhere under your document_root, people canbrowse to lớn it and see the PHP source in that tệp tin directly. This isusually not a good idea, so I địa chỉ cửa hàng these lines khổng lồ myhttpd.conf file:

Order allow,deny Deny from allThis blocks any direct access to .inc files. Theother option is khổng lồ not put the files underdocument_root, or perhaps lớn name them.php instead. But be careful with that lastapproach. Keep in mind that people will then be able khổng lồ execute thesescripts, when they were probably not designed to lớn be executed in astandalone fashion.

Other ways khổng lồ include files are throughinclude_once, require, andrequire_once. The difference betweeninclude and require is simplythat with include, if the file to be included doesnot exist, you get a warning, whereas with requireyou get a fatal error & script execution stops. Theinclude_once & require_oncevariations ensure that the file being included has not been includedalready. This helps avoid things like function redefinitionerrors.