PHP:是来自另一个数组内的数组的回声吗?

时间:2016-06-02 12:28:36

标签: php arrays

以下是在index.php中工作,但它是否正确?

在html标签之前:

$la= array();
$la['index.php'] = 'Start page';

(实际上这是包含的另一种语言库)

然后在标题内:

<title><?php echo $la[$_SERVER['PHP_SELF']];?></title>

对我而言,"$la[$_SERVER['PHP_SELF']]"部分接缝很奇怪,但它的工作原理。标题在我的浏览器中。这是好的做法吗?

2 个答案:

答案 0 :(得分:1)

是的,目前的代码有效。如果它的良好做法有争议。

PHP(与许多其他语言一样)将按顺序评估语句。 每次使用括号时,您实际上都使用数组索引运算符,其中索引充当参数。

您的代码将首先评估$ _SERVER [&#39; PHP_SELF&#39;]语句,该语句可能会返回&#39; index.php&#39;。下一个电话将是$ la [&#39; index.php&#39;](因为这是你的内部声明返回的内容。这将依次返回值&#39;开始页面&#39;这是发送的内容回声。

答案 1 :(得分:0)

您的代码没有任何问题。超全局#include <vector> binaryTree() { //Creation and naming scheme TString fileName = "binaryTree2.root";//name of file wishing to create. TString treeName = "Binary Tree 2";//name of tree wishing to create. TFile *file = new TFile(fileName, "RECREATE"); TTree *bTree = new TTree(treeName,"Binary Tree"); //Connection scheme TString fileNameCopy = "hodoscopehittree7.root";//name of file you will be accessing. TString treeNameCopy = "tree";//Name of tree within file you are accessing. TFile *filePtr = new TFile(fileNameCopy);//points to file with previously created tree TTree *treePtr = (TTree *) filePtr->Get(treeNameCopy);//Ptr to tree within accessed file. TObjArray *obj = treePtr->GetListOfBranches();//Ptr to branches. int branchNum = obj->GetEntries();//Number of branches in accessed tree. //Vector to hold all of the information from the tree. vector<vector<int>> dataHolder; int* inHist;//Ptr to become the entry. int inVal; vector <int> entryVec;//Vector of ints that the branches will rely on. entryVec.resize(branchNum); TString branchName; const int entryNum = treePtr->GetEntries();//Number of entries in each branch. //This loop creates a branch in the binary tree with the same name as the //branch in the tree being accessed and fills the dataHolder vector with //vectors. for (int i = 0; i < branchNum; i++) { TString temp; temp = "entryVec["; temp += (i); temp += "]/I"; branchName = obj -> At(i)-> GetName(); bTree -> Branch(branchName, &entryVec[i],temp); vector <int> tempVec; dataHolder.push_back(tempVec); } //This loop reads the entries of each branch within the accessed tree. If the //value is less than or equal to zero, 0 is added to the dataHolder and if //not 1 is added to the dataHolder. for (int i = 0; i < branchNum; i++) { branchName = obj-> At(i)-> GetName(); //Gets name of branch at index i treePtr -> SetBranchAddress(branchName, &inHist); for (int j = 0; j < entryNum; j++) { treePtr -> GetEntry(j); inVal = inHist; if (inVal <= 0) { dataHolder[i].push_back(0); } else { dataHolder[i].push_back(1); } } } //This loop fills the tree. Each inner loop reads the jth element of the //datHolder and inputs that information int the entryVec. The outer loop fills //the tree and then loops over all of the entries. for (int i = 0; i < entryNum; i++) { for (int j = 0; j < branchNum; j++) { entryVec[j] = dataHolder[j][i]; } bTree -> Fill(); } file -> Write(); cout << "Your program has finished; " << treeName << " has been created."; cout << endl; filePtr-> Close(); new TBrowser(); } 包含当前文件的名称。它不是很安全,因为它可以被操作来执行任意代码,如果你注入它而不正确地清理它。