PHP Bible Chapter 4 Adding PHP to HTML
PHP Bible Chapter 4 : Adding PHP to HTML ________________________________________________________ PHP Bible, 2 nd Edition 1 Wiley and the book authors, 2002
Summary n n Escaping into PHP mode Choosing PHP tag styles Writing a “Hello World” program in PHP Including external files ________________________________________________________ PHP Bible, 2 nd Edition 2 Wiley and the book authors, 2002
Escaping from HTML n PHP is embedded within HTML Ø Ø n n Anything that is compatible with HTML on the client side is also compatible with PHP You can use any method of developing Web pages (with the caveats mentioned earlier), and simply add PHP to that In order to tell the PHP parser how to recognize where your PHP code begins and ends you need to “escape” from HTML (like the <SCRIPT> tags for Java. Script) 4 styles of PHP tags Ø Ø Canonical PHP tags (most universal): <? php … ? > Short-open (SGML-style): <? … ? > ASP-style: <% … %> HTML script tags: <SCRIPT LANGUAGE=“PHP”> … </SCRIPT> ________________________________________________________ PHP Bible, 2 nd Edition 3 Wiley and the book authors, 2002
Hello world Open a new file in your PHP editor (e. g. notepad) <HTML> <HEAD> <TITLE>My first PHP program</TITLE> </HEAD> n <BODY> <? php print(“Hello, cruel world<BR>n”); phpinfo(); ? > </BODY> </HTML> ________________________________________________________ PHP Bible, 2 nd Edition 4 Wiley and the book authors, 2002
Hello World - output ________________________________________________________ PHP Bible, 2 nd Edition 5 Wiley and the book authors, 2002
Jumping in and out of PHP mode n At any given moment in a PHP script, you are either in PHP mode or you’re out of it and in HTML. Ø Ø Anything within the PHP tags is PHP Everything outside the PHP tags is HTML ________________________________________________________ PHP Bible, 2 nd Edition 6 Wiley and the book authors, 2002
Including files n Another way you can add PHP to your HTML is by putting it in a separate file and calling it by using PHP’s include functions Ø Ø include(‘/filepath/filename’); require(‘/filepath/filename’); include_once(‘/filepath/filename’); require_once(‘/filepath/filename’); Differ only in the kind of error they throw on failure. include and include_once will merely generate a warning if the file doesn’t exist, require and require_once will cause a fatal error and terminate execution of the script n The _once functions differ from the base functions in that they will only allow a file to be included once per PHP script n require_once is the recommended method for including ________________________________________________________ files in your PHP script in most instances n PHP Bible, 2 nd Edition 7 Wiley and the book authors, 2002
- Slides: 7