简体   繁体   中英

Need Assistance Interpreting PHP File

I have a new project that has a bit of code at the beginning of every page. I am in need of some clarification on what this series of statements do. Here is the opening call:

<?php
session_start();
$levels = 1;
include("../Connections/main.php");
include("../queries.php");

I understand all of it except how $levels = 1; relates to include("../queries.php");

When I look at include("../queries.php"); I see that it begins with the following statement:

<?php
switch($levels) {
case 1:
$dir = "../";
break;

case 2:
$dir = "../../";
break;


case 3:
$dir = "../../../";
break;

case 4:
$dir = "../../../../";
break;

case 5:
$dir = "../../../../../";
break;
}
function db_info($table,$where,$value,$info,$dir) {
//the functions just continue from there

This is the portion that I don't follow. I understand that there is a switch statement that offers several cases for $dir based upon the value of $levels which was defined in the first bit of code. But how do these different outputs for the $dir value translate? Is this something you've seen or used before? What does the ../ stand for? Thanks.

../ refers to the parent directory of the current directory.

../../ refers to the parent of the parent. etc.

This is a ../ always refers to parent directory , the more ../ the more parents you get. With enough you can get to the root of the file system (though you're better off just using / ). This isn't exactly the best way to accomplish this.

$dir = str_repeat( '../', $level );

Would be more obvious and it would be more extensible. A better option still would be to have some config file which simply did something like:

// obviously this would be a better constant name.
define( 'PATH_TO_STUFF', dirname( __FILE__ ) ); // use __DIR__ on PHP 5.>=3

Bonus info:

  • ./ = current directory
  • ../ = parent directory
  • ./../../ = grandparent of current directory
  • ./../../foo/../ The parent directory of the folder foo in the grandparent directory (ie. the grandparent directory). (I am your father's, brother's, nephew's, cousin's former roommate)
  • / = file system root directory.

The ../ means previous/parent directory. So if $level == 1 , then $dir will equal ../ .

Something else must use dir to construct a path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM