0
$\begingroup$

While having some older PHP code checked/updated with an online system, the output indicated that I should have (string) in several POST variables, such as:

  • $info = pathinfo((string) $_FILES['receipt']['name']);
    

    instead of

    $info = pathinfo($_FILES['receipt']['name']);
    
  • $name = strtoupper((string) $_POST['name']);
    

    instead of

    $name = strtoupper($_POST['name']);
    
  • $arrdate = date("j M Y", strtotime((string) $trdate));
    

    instead of

    $arrdate = date("j M Y", strtotime($trdate));
    

I am not familiar with this aspect and am wondering what it does and why it is needed? The code worked just fine without the (string).

$\endgroup$
3
  • 2
    $\begingroup$ Type casting converts between value types. For example, if $_FILES['receipt']['name'] was NULL, casting it to string would make it "". $\endgroup$ Commented Feb 5, 2025 at 1:38
  • $\begingroup$ "why it is needed?" In your case; it's not. $\endgroup$ Commented Feb 5, 2025 at 8:01
  • 1
    $\begingroup$ I suppose that "online system" means some code quality tool or linter. Such tools are sometimes overzealous. Most of these changes make no sense. For example, $_FILES['receipt']['name'] may or may not exist. If it exists, it'll be a string no matter what because PHP itself populates it; if it doesn't, a cast to string will still throw a warning. Same problem with $_POST['name'] but, also, if it's an array you'll get an Array to string conversion warning anyway. $\endgroup$ Commented Feb 5, 2025 at 8:13

1 Answer 1

0
$\begingroup$

The super global array $_POST is populated depends on the form value setnt through the client's POST request. If a field to key hello exists, the value will be populated to the $_POST["hello"] field accordingly.

However, if there is not value to the key, $_POST["hello"] does not exist. And accessing an non-exists key in an array would result in 2 issues:

  • warning message

    such as:

    PHP Warning:  Undefined array key "hello"
    
  • unexpected NULL value

    The value of the expression $_POST["hello"] here will be NULL.

Using a NULL in functions may results in unexpected behaviour. As @amadan mentioned in his comment, some programmers uses type casting to cast undefined values from NULL into string to prevent such issue.

However, the warning is not suppressed.

Better solution

Started from PHP 7, now you may use the null coalesce operator (the ?? sign) to both suppress the warning and to force a value if a key is not defined:

$info = pathinfo($_FILES['receipt']['name'] ?? '');
$name = strtoupper($_POST['name'] ?? '');
$arrdate = date("j M Y", strtotime($trdate ?? ''));

Null coalesce operator detects if the former operator is NULL or undefined, and then fallbacks to later value. For example:

$value = $_POST["hello"] ?? "my default";

If $_POST["hello"] is defined, then $value will be assigned to $_POST["hello"]. If not, then $value will be assigned the fallback value "my default" here.

$\endgroup$
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that explanation. Is there any performance difference between using (string) vs using the null coalesce operator?
@MrCycling I don't know which one is faster, but if you have enough POST fields to make the difference matter (like, millions of them) you'll already have much bigger performance issues in network transfer and parameter parsing.
@MrCycling: I believe the difference would depend on the PHP version and is probably very little. I would prefer coalesce operator because type casting of undefined array key value would generate unwanted warning message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.