简体   繁体   English

在PHP中定义时使用变量值

[英]Using a variable value when defining in PHP

$product_id_string = $_GET['product_id'];
$product_id = (int)$product_id_string;


//this should be derived from GET or the method you use to distinguish entities
define('ENTITY_ID', $product_id);

The above code is part of a plugin that I am attempting to modify - If I change $product_id to a number, the code works - however when I try to GET the product_id, a 0 ends up being saved to the database instead of the GET variable. 上面的代码是我尝试修改的插件的一部分-如果将$ product_id更改为数字,则该代码有效-但是,当我尝试获取product_id时,最终将0保存到数据库而不是GET变量。

Example of working code: 工作代码示例:

define('ENTITY_ID', 2547);

If I echo the $product_id, I receive the number I want. 如果回显$ product_id,则会收到我想要的号码。

$product_id_string = $_GET['product_id'];
$product_id = (int)$product_id_string;
echo $product_id; //Returns 2547
define('ENTITY_ID', $product_id); //Ends up saving as 0 in database

Use the below statement to convert to int. 使用以下语句转换为int。 That will fix the issue 这将解决问题

$product_id = intval($product_id_string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM