我希望能夠使用函數向被稱爲播放列表的數組添加新值(是必需的)。但每次輸入另一個值時,它都會替換舊的值,而不是僅添加數組的末尾。哪裏不對?我的數組值保持消失和刷新在PHP?
<?php
$playlist = array("Be more.mp3", "Drift Away.mp3", "Panda Sneeze.mp3");
function add_songs_playlist() {
global $playlist;
?>
<form method = "post">
<input type = "text" name = "name1"/>
<input type = "submit" name = "submit1"/>
</form>
<?php
if (isset($_POST['submit1'])) {
$newsong = $_POST['name1'];
array_push($playlist, $newsong);
}
}
add_songs_playlist();
foreach ($playlist as $value) {
echo $value."<br>";
}
如果'array_push'不起作用,爲什麼會'$ myArray的[]'? – Eevee