diff --git a/content/web/03-http-server.md b/content/web/03-http-server.md index 0c77d83..f512e30 100644 --- a/content/web/03-http-server.md +++ b/content/web/03-http-server.md @@ -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. diff --git a/content/web/04-php-html.md b/content/web/04-php-html.md new file mode 100644 index 0000000..63d1049 --- /dev/null +++ b/content/web/04-php-html.md @@ -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 + + + + PHP Apprentice - HTML + + +

I can write 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 `` 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 + + + + PHP Apprentice - 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: ``. 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 `

` tags. PHP supports a shorthand opening tag that combines the open tag with echo: `

+``` +This example and the previous one are equivalent to the PHP runtime. \ No newline at end of file diff --git a/layouts/partials/table_of_contents.html b/layouts/partials/table_of_contents.html index 55b5a62..4bdccab 100644 --- a/layouts/partials/table_of_contents.html +++ b/layouts/partials/table_of_contents.html @@ -32,6 +32,7 @@
  • HTTP
  • HTTP POST
  • PHP HTTP Server
  • +
  • PHP HTML
  • Credits