我正在嘗試創(chuàng)建一個界面,其中研討會描述可以在用戶創(chuàng)建后在服務(wù)器上更新。有一個文本字段和一個按鈕,可以通過分配的編號調(diào)出研討會。(這也是服務(wù)器上的submissions/workshop# 中的目錄名稱)。當(dāng)我使用此方法設(shè)置變量 $workshopPath 時,我希望在使用字符串填寫文本輸入以更新研討會標(biāo)題時能夠訪問此全局變量。$workshopPath 在“updateTextItems”函數(shù)中注冊為空字符串,因此它將文本文件寫入根目錄而不是正確的創(chuàng)意工坊目錄。我認(rèn)為我正確地引用了函數(shù)中的全局變量,但這不起作用。我還嘗試使用 $GLOBALS['workshopPath'],但這也不起作用。有人可以幫我弄清楚如何將變量傳遞給第二個函數(shù)嗎?謝謝 :-)<?php$workshopPath;if (isset($_POST['gotoWorkshop'])) { if (empty($_POST['numberInput'])) { echo "Please enter a valid workshop number"; } else { //Assign the name variable form the posted field info if one was entered. $workshopNumber = stripslashes($_POST['numberInput']); global $workshopPath; $workshopPath = "submissions/" . $workshopNumber . "/"; }}if (isset($_POST['updateTextItems'])) { if ($_POST['titleInput']) { //Assign the name variable form the posted field info if one was entered. $titleInput = stripslashes($_POST['titleInput']); } if ($titleInput) { global $workshopPath; $titleFile = fopen($workshopPath . "title.txt", "w") or die("There was an error creating the title file."); fwrite($titleFile, $titleInput); fclose($titleFile); }}?>
如何在兩個函數(shù)中使用 PHP 全局變量?
寶慕林4294392
2023-09-22 17:04:21