简体   繁体   中英

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.

Example of working code:

define('ENTITY_ID', 2547);

If I echo the $product_id, I receive the number I want.

$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. That will fix the issue

$product_id = intval($product_id_string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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