Undefined constant ENT_SUBSTITUTE

<?php
if (isset($_GET['key'])) {
$key=$_GET['key'];
} else {
$key='';
}

$key=str_replace('\\', '', htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') );

$html='<a href="t08.php">initial</a><br>
<form method="get">  
    <input name="key" value="' .$key. '">
    <button type="submit">Search</button> 
</form>';
echo $html;

I have the code above at http://form.kr/test01/t1/t08.php?key="
It works fine…

I add error_reporting at the code above like the code below.at http://form.kr/test01/t1/t09.php?key="

<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

if (isset($_GET['key'])) {
$key=$_GET['key'];
} else {
$key='';
}

$key=str_replace('\\', '', htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') );

$html='<a href="t08.php">initial</a><br>
<form method="get">  
    <input name="key" value="' .$key. '">
    <button type="submit">Search</button> 
</form>';
echo $html;

The code above which has error_reporting says like the quote below.

How can I define the “constant” for removeing the error?

you can define() constants

https://www.php.net/manual/en/function.define.php

What version of PHP are you using?

(I just wondered why the constant would not already be defined as the documentation suggests it was added in 5.4.0, in March 2012.)

1 Like

I am afraid it is under 5.4…

I tried to add “define(“CONSTANT”, “ENT_SUBSTITUTE”);”.

<?php

declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

if (isset($_GET['key'])) {
$key=$_GET['key'];

} else {
$key='';
}

define("CONSTANT", "ENT_SUBSTITUTE");

$key=str_replace('\\', '', htmlspecialchars($key, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') );

$html='<a href="t08.php">initial</a><br>
<form method="get">  
    <input name="key" value="' .$key. '">
    <button type="submit">Search</button> 
</form>';

echo $html;

The code above at http://form.kr/test01/t1/t10.php still produces the quote below.

What other trials should I try?

You could try reading the link that @chorn sent you, that has samples of how to define a constant :slight_smile:

But what value will you give it? I would suggest you set it to zero so it doesn’t upset anything, and make sure your definition is somewhere you can easily find when you upgrade to a newer version. If you’re not intending to upgrade for some reason, then leave it out as it’s not going to do anything.

Or if you’re incredibly lazy…
if(phpversion() < "5.4") {

As long as you keep messing with obsolete PHP versions that are hundreds and hundreds of releases behind you are just going to keep having problems you shouldn’t be having.

if (!defined('ENT_SUBSTITUTE')) {
    define('ENT_SUBSTITUTE',0); // Real value is 8 but use 0 to be safe
}
echo 'ENT_SUBSTITUTE ' . ENT_SUBSTITUTE . "\n";

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.