Home > Backend Development > PHP Tutorial > How Does PHP Handle Periods in Variable Names, and How Can I Work Around It?

How Does PHP Handle Periods in Variable Names, and How Can I Work Around It?

Patricia Arquette
Release: 2024-12-10 18:58:11
Original
304 people have browsed it

How Does PHP Handle Periods in Variable Names, and How Can I Work Around It?

PHP's Dot Conversion: Understanding and Workarounds

When passing request fields or cookies with periods in their names, PHP automatically replaces them with underscores. This behavior can be a hindrance, and you may want to prevent it.

PHP's Explanation

According to PHP.net, the dot is not a valid character in PHP variable names. To prevent unintended concatenation issues, PHP automatically converts dots in variable names to underscores.

Additional Characters Affected

In addition to dots, PHP also converts the following characters to underscores:

  • Space (chr(32))
  • Open square bracket (chr(91))
  • Various non-ascii characters (chr(128) - chr(159))

Workarounds

Since you cannot prevent PHP from performing this conversion, you can use a workaround to convert the underscores back to dots. One possible method is using the str_replace function:

$request_uri = str_replace('_', '.', $_SERVER['REQUEST_URI']);
Copy after login

This will replace all occurrences of underscores with dots in the request URI. You can apply the same approach to other name-value pairs (e.g., $_GET, $_POST, $_COOKIE).

The above is the detailed content of How Does PHP Handle Periods in Variable Names, and How Can I Work Around It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template