首页 > 编程语言 >如何在 PHP 8.3 中声明变量

如何在 PHP 8.3 中声明变量

时间:2024-05-29 23:33:59浏览次数:19  
标签:variables php-8.3

在 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.

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

相关文章

  • CMake Professtional-2 Variables
    set(varNamevalue...[PARENT_SCOPE])cmake中所有的值都是string,如果同时添加多个值,会自动添加;set(myVarabc)#myVar="a;b;c"set(myVara;b;c)#myVar="a;b;c"set(myVar"abc")#myVar="abc"set(myVarab;c)#myVar=&q......
  • 加载权重时候出现:not created its variables yet
    ValueError:UnabletoloadweightssavedinHDF5formatintoasubclassedModelwhichhasnotcreateditsvariablesyet.CalltheModelfirst,thenloadtheweights.这个错误是由于尝试将保存在HDF5格式中的权重加载到一个还未创建其变量的子类模型中所导致的......
  • MindSponge分子动力学模拟——定义Collective Variables(2024.02)
    技术背景在前面的几篇博客中,我们介绍了MindSponge分子动力学模拟框架的基本安装和使用和MindSponge执行分子动力学模拟任务的方法。这里我们介绍一个在增强采样领域非常常用的工具:CollectiveVariable(CV),或者我们也可以直接称呼其为一个物理量。因为像化学反应或者是蛋白质折叠等......
  • [引]Power Automate Use variables and the % notation
    Variablemanipulationandthe%notation-PowerAutomate|MicrosoftLearn变量操作和%表示法-PowerAutomate|MicrosoftLearn InthisarticleHardcodedvaluesVariablenamesBasicarithmeticComparisonsShow2moreVariablesareusedwithinflowstostored......
  • 变量与函数Variables and Functions
    Task04:变量与函数VariablesandFunctions变量Variables变量是一段数据,用"="对某个变量名赋值新的值会覆盖掉旧的值新值的数据类型不必与旧值相同x=5print(x)x="data"print(x)data变量命名规则:必须以字母或下划线(_)开头命名可由字母、数字和下划线组成大小写敏感......
  • python-task4:Variables and Functions
    变量Variables以字母或下划线(_)开头(不可以以数字开头)以字母、数字、下划线组成大小写敏感(A与a不一样)需要避免使用保留字命名,以下代码可查询保留字importkeywordkeyword.kwlist对于变量,旧的值会覆盖新的值,而且python支持多变量赋值a=b=c=2print(f"a={a},b={b},c={c}"......
  • [Flink] Flink(CDC/SQL)Job在启动时,报“ConnectException: Error reading MySQL varia
    1问题描述1.1基本信息所属环境:CN-PT问题时间:2023-11-21所属程序:FlinkJob(XXXPT_dimDeviceLogEventRi)作业类型:FlinkSQLJob数据流:业务MySQL==>FlinkJob(FlinkCdcConnector(mysql)+FlinkSQL)==>BigdataKafka==>BigdataOLAP==>业务系统作业......
  • WEBSITE_LOCAL_CACHE_OPTION Environment variables and app settings in Azure App S
    EnvironmentvariablesandappsettingsinAzureAppService SettingnameDescriptionWEBSITE_LOCAL_CACHE_OPTIONWhetherlocalcacheisenabled.Availableoptionsare:-Default:Inheritthestamp-levelglobalsetting.-Always:Enablefortheapp.......
  • 无涯教程-批处理 - Local Variables in Functions函数
    函数中的局部变量可用于避免名称冲突,并将变量更改保持在函数本地,首先使用SETLOCAL命令来确保命令处理器备份所有环境变量,可以通过调用ENDLOCAL命令来恢复变量,当到达批处理文件的末尾时,即通过调用GOTO:EOF,将自动调用ENDLOCAL。使用SETLOCAL对变量进行本地化允许在函数内自由使用......
  • Proj. Unknown: Deciding Differential Privacy of Online Algorithms with Multiple
    Paperhttps://arxiv.org/abs/2309.06615Abstract背景:自动机A被称作查分隐私自动机:当对某些D,对任何隐私预算ε>0,该自动机是Dε-differentiallyprivate(ADiPautomatonisaparametricautomatonwhosebehaviordependsontheprivacybudget......