官术网_书友最值得收藏!

  • Learning PHP 7
  • Antonio Lopez
  • 629字
  • 2021-07-09 20:17:40

PHP files

From now on, we will work on your index.php file, so you can just start the web server, and go to http://localhost:8080 to see the results.

You might have already noticed that in order to write PHP code, you have to start the file with <?php. There are other options, and you can also finish the file with ?>, but none of them are needed. What is important to know is that you can mix PHP code with other content, like HTML, CSS, or JavaScript, in your PHP file as soon as you enclose the PHP bits with the <?php ?> tags.

<?php
  echo 'hello world';
?>
bye world

If you check the result of the preceding code snippet in your browser, you will see that it prints both messages, hello world and bye world. The reason why this happens is simple: you already know that the PHP code there prints the hello world message. What happens next is that anything outside the PHP tags will be interpreted as is. If there is an HTML code for instance, it would not be printed as is, but will be interpreted by the browser.

You will learn in Chapter 6, Adapting to MVC, why it is usually a bad idea to mix PHP and HTML. For now, assuming that it is bad, let's try to avoid it. For that, you can include one file from another PHP file using any one of these four functions:

  • include: This will try to find and include the specified file each time it is invoked. If the file is not found, PHP will throw a warning, but will continue with the execution.
  • require: This will do the same as include, but PHP will throw an error instead of a warning if the file is not found.
  • include_once: This function will do what include does, but it will include the file only the first time that it is invoked. Subsequent calls will be ignored.
  • require_once: This works the same as require, but it will include the file only the first time that it is invoked. Subsequent calls will be ignored.

Each function has its own usage, so it is not right to say that one is better than the other. Just think carefully what your scenario is, and then decide. For example, let's try to include our index.html file from our index.php file such that we do not mix PHP with HTML, but have the best of both worlds:

<?php
echo 'hello world';
require 'index.html';

We chose require as we know the file is there—and if it is not, we are not interested in continuing the execution. Moreover, as it is some HTML code, we might want to include it multiple times, so we did not choose the require_once option. You can try to require a file that does not exist, and see what the browser says.

PHP does not consider empty lines; you can add as many as you want to make your code easier to read, and it will not have any repercussion on your application. Another element that helps in writing understandable code, and which is ignored by PHP, is comments. Let's see both in action:

<?php

/*
 * This is the first file loaded by the web server.
 * It prints some messages and html from other files.
 */

// let's print a message from php
echo 'hello world';

// and then include the rest of html
require 'index.html';

The code does the same job as the previous one, but now everyone will easily understand what we are trying to do. We can see two types of comments: single-line comments and multiple-line comments. The first type consists of a single line starting with //, and the second type encloses multiple lines within /* and */. We start each commented line with an asterisk, but that is completely optional.

主站蜘蛛池模板: 碌曲县| 定陶县| 鄂尔多斯市| 汾西县| 兰坪| 革吉县| 册亨县| 浏阳市| 东光县| 长沙县| 株洲市| 三都| 德江县| 乳源| 崇明县| 甘洛县| 大洼县| 洛南县| 阿鲁科尔沁旗| 黑山县| 桂阳县| 安远县| 平潭县| 宜章县| 台州市| 凤城市| 陕西省| 江油市| 平邑县| 绩溪县| 杨浦区| 长白| 嘉善县| 淅川县| 旌德县| 手机| 渝中区| 茶陵县| 昌黎县| 玛纳斯县| 郴州市|