php单引号与双引号$\u文件
我是php新手,现在正在学习我的第一门课程。就我个人而言,我无法理解为什么只有$screenshot=$\u文件[“screenshot”][“name”]需要双引号。我最初有单引号,但它不起作用。我随机决定尝试双引号,它开始工作。。。谁能告诉我为什么php单引号与双引号$\u文件,php,forms,file-upload,double-quotes,single-quotes,Php,Forms,File Upload,Double Quotes,Single Quotes,我是php新手,现在正在学习我的第一门课程。就我个人而言,我无法理解为什么只有$screenshot=$\u文件[“screenshot”][“name”]需要双引号。我最初有单引号,但它不起作用。我随机决定尝试双引号,它开始工作。。。谁能告诉我为什么 <?php // Define the upload path and maximum file size constants define('GW_UPLOADPATH', 'images/'); if (isset($_P
<?php
// Define the upload path and maximum file size constants
define('GW_UPLOADPATH', 'images/');
if (isset($_POST['submit'])) {
// Grab the score data from the POST
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot = $_FILES["screenshot"]["name"];
if (!empty($name) && !empty($score)) {
// Move the file to the targe upload folder
$target = GW_UPLOADPATH . $screenshot;
// Connect to the database
$dbc = mysqli_connect('localhost', 'root', 'Bandito8', 'gwdb')
or die('Unable to connect to databse');
// Write the data to the database
$query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
mysqli_query($dbc, $query)
or die('Unable to complete query');
// Confirm success with the user
echo '<p>Thanks for adding your new high score!</p>';
echo '<p><strong>Name:</strong> ' . $name . '<br>';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image"></p>';
echo '<p><a href="index.php"><< Back to high scores</a></p>';
// Clear the score data to clear the form
$name = "";
$score = "";
$screenshot = "";
mysqli_close($dbc);
}
else {
echo '<p class="error">Please enter all of the information to add your high score.</p>';
}
}
?>
php解析器尝试将双引号中的字符串解释为变量,而将单引号中的字符串解释为字面上的字符串
php解释程序正在搜索变量,由于找不到任何变量,因此需要花费更多的php时间
echo$_文件[“上载”][“大小”]代码>
php解释器正在搜索字符串,因为它没有找到任何变量,所以花费的php时间更少
echo$_文件['upload']['size']代码>
另一个例子:
此示例将$hidden视为变量,效果良好
echo“红狐对猎人来说是$hidden”代码>
此示例将$hidden视为字符串,无法按预期工作
echo“红狐对猎人来说是$hidden”代码>
希望这有帮助 $screenshot=$\u文件[“screenshot”][“name”]
和$screenshot=$\u文件[“screenshot”][“name”]
的可能重复应该以完全相同的方式工作;你一定还改变了一些其他的东西它应该可以使用单引号,我相信一定有一些错误,试试var\u dump($\u FILES['screenshot']['error'])
,然后检查错误详细信息,在使用var\u dump进行了大量的尝试和错误更改之后,我以某种方式使用单引号获得了它-谢谢Mahesh!