Suppose we have a web directory like this:
index.html
class/ajax.js
class/a.php
class/b.php
img/img.jpgSo, if a.php needs to call b.php with require_once, how should we write it?
require_once("b.php");Then, if a.php wants to use echo to display img/img.jpg, what path should we use?
The answer is:
echo "<img src="img/img.jpg" />";Here, the positioning of "yourself" must be clear.
When PHP calls something, you are standing at the server's current location. But with something like echo, you are standing from the front-end client's perspective — for example, in this case, with ajax, the result is displayed as part of index.html, so your directory is the root "/".
In short, where you are depends on whether you're on the front end or the back end.
It's not just PHP — you often see this kind of problem when writing JSP and the like too.
At first, all the directories, positions, and perspectives can get confusing.
But once you classify things by front end vs. back end, the logic becomes very clear.
Comments · 1