在 php 7.3 中,我做了如下操作。
$TitelString = $_GET["TitelString"];
If (!$TitelString) $TitelString="";
$AuteurString = $_GET["AuteurString"];
If (!$AuteurString) $AuteurString="";
$JaarString = $_GET["JaarString"];
If (!$JaarString) $JaarString="";
现在我迁移到了 8.3,但却出现了错误,上面写着 : 警告:未定义变量 $TitelString 在 D:\xampp\htdocs\fb\ltvg-1\ltvg-3.php 第 43 行 Warning:未定义的数组关键字 "AuteurString" 在 D:\xampp\htdocs\fb\ltvg-1\ltvg-3.php 第 44 行 Warning:未定义的数组关键字 "JaarString" 在 D:\xampp\htdocs\fb\ltvg-1\ltvg-3.php 第 46 行
I did try....
const $TitelString;
const $AuteurString = "";
const $JaarString="";
但是没有用
我在互联网上进行了搜索,但我不清楚我现在必须做什么。我认为这很简单......
谢谢、 弗雷德
The errors you're encountering are because you're trying to access potentially undefined array keys in $_GET
.
Here's a breakdown of the problem and how to fix it in PHP 8.3:
The Issue:
- Undefined Array Keys: PHP now throws warnings when you try to access an array key that doesn't exist. This is considered good practice because it helps catch potential errors in your code.
The Solution:
Use the null coalescing operator (??
) introduced in PHP 7.0. It provides a concise way to provide default values when variables or array keys are undefined.
$TitelString = $_GET["TitelString"] ?? "";
$AuteurString = $_GET["AuteurString"] ?? "";
$JaarString = $_GET["JaarString"] ?? "";
Explanation:
$_GET["TitelString"] ?? ""
: This checks if the key"TitelString"
exists in the$_GET
array.- If the key exists and has a value, that value is assigned to
$TitelString
. - If the key doesn't exist or its value is
null
, an empty string (""
) is assigned to$TitelString
.
- If the key exists and has a value, that value is assigned to
The same logic applies to $AuteurString
and $JaarString
.
Important Note about Constants:
Your attempt to use const
wouldn't work because:
- Constants Cannot Be Dynamic: Constants are defined once and their values cannot change during script execution. You can't assign values from
$_GET
(which changes on each request) to constants.
Best Practices:
- Error Reporting: Ensure your PHP error reporting is set to show warnings during development. This will help you catch similar issues early on.
- Input Sanitization: Always sanitize user input (
$_GET
,$_POST
, etc.) to prevent security vulnerabilities like cross-site scripting (XSS).
Let me know if you have any other questions!
标签:variables,php-8.3 From: 78550595