'(花式撇号?)在 PHP 中是什么意思 [英] What does ’ (fancy apostrophe?) mean in PHP

查看:25
本文介绍了'(花式撇号?)在 PHP 中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个示例 PHP 代码:

I got this example PHP code:

if ( $new_value && ’ == $old_value )

  1. &&
  2. 后面的字符叫什么
  3. 它有什么作用
  4. 如何在键盘上打字,以及
  5. 最重要的是,我可以用什么来代替人类可读的地方,并且看起来不像是我试图炫耀我对晦涩的代码速记的了解?

我在这里找不到它:参考 — 这是什么符号在 PHP 中是什么意思? 并且由于 Google 忽略了它,并且大多数其他搜索将 ' 视为与 ' 相同(撇号)我将空出来.

I don't find it here: Reference — What does this symbol mean in PHP? and since Google ignores it, and most other searches treat the same as '(apostrophe) I'm coming up empty.

推荐答案

这个字符在 PHP 中没有特殊含义,可能只是您正在阅读的代码中的一个错误.例如,它可能本来是 ''(一个空字符串),但被一些 Markdown 风格的格式系统搞砸了.

This character has no particular meaning in PHP, and is probably just a mistake in the code you're reading. For instance, it may have been intended as '' (an empty string) but got messed up by some markdown-style formatting system.

直到 PHP 8.0,它实际上不会因为两个稍微奇怪的特性的组合而产生错误:

Until PHP 8.0, it doesn't actually produce an error due to the combination of two slightly odd features:

  • 任何可能是有效常量名称但当前未定义为常量的名称都被视为字符串,就好像您使用与名称相同的值定义了常量.此功能已在 PHP 8.0 中删除.
  • 任何超出 7 位 ASCII(即 128 到 255 的值)的字节都允许出现在变量或常量名称中,即使在开头也是如此.由于这个字符不是 ASCII,它将使用一堆匹配此规则的字节以 UTF-8 呈现 - 请注意,PHP 实际上并不关心它们在 UTF-8 中的含义,它只是看到它们不是-ASCII 并让您可以在任何地方使用它们.

结果是' == $old_value实际上被解释为''' == $old_value,即does $old_value> 匹配包含字符 '?"

The result is that ’ == $old_value is actually interpreted as '’' == $old_value, i.e. "does $old_value match a string containing the character ?"

在 PHP 8.0 中,不再允许这种解释,并且您将在运行时收到未定义常量的错误.但是,您实际上可以定义这个常量,并且该代码将在从 4.3 到 8.0 的所有 PHP 版本上运行 完全没有任何错误或警告:

In PHP 8.0, this interpretation is no longer allowed, and you'll get an error at runtime that the constant isn't defined. However, you can actually define this constant, and the code will run on all PHP versions from 4.3 up to and including 8.0 without any errors or warnings at all:

<?php

// Define our oddly-named constant to have a value of 'hello'
define('’', 'hello');

// Compare it against a normal string variable
$foo = 'hello';

if ( ’ == $foo ) {
    echo 'It works!';
}

需要说明的是,这并不是特别有用,除非您想对某人进行恶作剧,否则它只是解释了一个令人惊讶的事实,即这不是语法错误.

Just to be clear, this isn't particularly useful, unless you're trying to play a prank on someone, it just explains the surprising fact that this isn't a syntax error.

这篇关于'(花式撇号?)在 PHP 中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆