1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-01-17 06:38:14 +01:00

Adding new HTML chapter under web

This commit is contained in:
Andrew Davis 2020-02-02 10:21:14 -06:00
parent b8af664af3
commit ee7c6c4f47
3 changed files with 48 additions and 0 deletions

View File

@ -4,6 +4,7 @@ description = "Handling HTTP Requests in PHP"
tags = ["php", "http", "server"]
slug = "http-server"
previous = "web/http-post.html"
next ="web/php-html.html"
+++
In PHP, you usually use a separate web server program that accepts HTTP requests and passes them to PHP to create a response. Common examples of separate web server programs are Apache and Nginx. However, PHP has a built in web server we can use during development.

View File

@ -0,0 +1,46 @@
+++
title = "PHP HTML"
description = "Serving HTML using PHP"
tags = ["php", "http", "server"]
slug = "php-html"
previous = "web/http-server.html"
+++
The most common content type of HTTP messages is HTML. Hypertext Markup Language is the anchor of all websites. It is the language that defines how content should be displayed in a web page. Just as we saw in the last chapter with HTTP messages, PHP makes it easy to output HTML. In fact, PHP was built around enhancing HTML.
The Mozilla Developer Network has an [excellent tutorial](https://developer.mozilla.org/en-US/docs/Web/HTML) on HTML. I will assume that you can already write some HTML, however, I will use basic tags for this tutorial.
We can start by creating a new `index.php` file and running the localhost server: `php -S localhost:8080`. However, we are not going to start the file with a PHP tag. We are going to write some HTML.
```html
<!DOCTYPE html>
<html>
<head>
<title>PHP Apprentice - HTML</title>
</head>
<body>
<h1>I can write HTML!</h1>
</body>
</html>
```
If you open `http://localhost:8080` in your web browser, you will see `I can write HTML!` in big letters. PHP automatically sees that you have written some HTML and responds to the browser's GET request with the appropriate HTTP response headers and the body set to the HTML text.
How does the PHP runtime know when you are writing HTML or writing PHP code? It all depends on PHP tags. When you write `<?php`, the PHP runtime will interpret anything after that tag as PHP code. This is the reason why PHP files with only PHP code must start with `<?php`. When you want to stop writing PHP code, you can use `?>` as the closing tag. In PHP only files, the closing tag is unnecessary since the entire file is PHP.
Using the PHP open and close tags, we can embed PHP code inside an HTML tag. For example, we can make our HTML message all caps using PHP.
```php
<!DOCTYPE html>
<html>
<head>
<title>PHP Apprentice - HTML</title>
</head>
<body>
<h1><?php echo strtoupper('I can write HTML!'); ?></h1>
</body>
</html>
```
Refresh your browser and you will see all the text in uppercase letters. When I wanted to run some PHP code, I used the PHP open tag: `<?php`. When I wanted to return to writing HTML, I used the closing tag: `?>`. In this way, you can easily output HTML documents to web browsers, while running code. Embedding PHP gives you the flexibility to create dynamic web pages.
I used the PHP keyword `echo` in the last example to output the string result of `strtoupper` inside the `<h1>` tags. PHP supports a shorthand opening tag that combines the open tag with echo: `<?=`. You could write the above example like the following.
```php
<h1><?= strtoupper('I can write HTML!'); ?></h1>
```
This example and the previous one are equivalent to the PHP runtime.

View File

@ -32,6 +32,7 @@
<li><a href="/web/http.html">HTTP</a></li>
<li><a href="/web/http-post.html">HTTP POST</a></li>
<li><a href="/web/http-server.html">PHP HTTP Server</a></li>
<li><a href="/web/php-html.html">PHP HTML</a></li>
</ol>
<a href="/credits.html">Credits</a>