PHPでのエスケープシーケンスと数値表記

こんにちは、Habr。オンラインコース「PHP-developer」の開始前夜に、私たちは資料の伝統的な翻訳準備しました。また、過去のデモレッスン「PHPエコシステム」の録画もご覧いただけます。






, , , , , .





PHP . , $string = "php.watch"



- PHP, $num = 42



- . ( ), , : $emoji = "?"



.





PHP, , escape- , , (, ) . escape-, PHP.





, PHP , , , , (scientific) . .





Heredoc

PHP ("string"



) Heredoc ( ) escape- .





PHP , Heredoc.





$name = 'John';
echo "Hi $name"; // "Hi John"
      
      



$name = 'John';
echo <<<HEREDOC
Hi $name
HEREDOC;
// "Hi John"
      
      



( ) , :





$name = 'John';
echo "Hi {$name}"; // "Hi John"
      
      



('string'



) Nowdoc :





$name = 'John';
echo 'Hi $name'; // "Hi $name"
      
      



$name = 'John';
echo <<<'NOWDOC'
      
      



Hi $name
NOWDOC;
// "Hi $name"
      
      



Heredoc escape-.





PHP heredoc, (\



) «escape-».





, \$name



$name



PHP $name



.





$name = 'John';
echo "Hi \$name"; // "Hi $name"
      
      



escape-.





$name = 'John';
echo "Hi \\$name"; // "Hi \John"
      
      



PHP escape- . \$



escape-, PHP, PHP $



.





: \t \v

, - . ( tab) , \t



. \t



IDE.





echo "Foo\tBar";
Foo Bar
      
      



\v - . :





echo "Foo\vBar\vBaz";
      
      



Foo
   Bar
      Baz
      
      



: \r \n

\r



(« ») \n



(« ») .





, \r



, \n



, Windows \r\n



. , Linux MacOS « » (\n



) , Windows \r\n



( , ). MacOS \r



.





PHP PHP_EOL



, .





echo "Left\nLeft\nRight\nRight";
      
      



Left
Left
Right
Right
      
      



Escape-: \e

Escape- ANSI . , \e



, [32m



, , [33m



- .





echo "\e[32mGreen text\e[0m \e[33mYellow text\e[0";
      
      



, ANSI, :





: \f

- ASCII . . \f , , .






escape- ASCII 

PHP ASCII .





, ASCII P



80



( ). 80 - 120



.





P



escape-:





echo "\120";

P
      
      



ASCII :





echo "\120\110\120\56\127\141\164\143\150";

PHP.Watch
      
      



\0



\377



escape- ASCII .





, ASCII ( 128 255) UTF-8. PHP 128



(: 200



; : 80



) , UTF-8.





PHP , UTF-8.





escape- ASCII

escape- , PHP escape- \x .





, x0



xFF



. UTF-8 - , x80



.





, (.. AF



af



aF



).





ASCII P - 80, x50



:





echo "\x50";

P
      
      



"PHP.Watch" escape-:





echo "\x50\x48\x50\x2E\x57\x61\x74\x63\x68";

PHP.Watch
      
      



Escape- Unicode

PHP Unicode \u



code point .





echo "\u{1F418} - \u{50}\u{48}\u{50}\u{2E}\u{57}\u{61}\u{74}\u{63}\u{68}";

? - PHP.Watch
      
      



PHP , Unicode 10FFFF



:





echo "\u{10FFFF1}"

Invalid UTF-8 codepoint escape sequence: Codepoint too large on line ...
      
      



 10FFFF



, UTF-8 U+0000



U+10FFFF



.





FFFFF



, 10FFFF



. , .






Unicode \u{}



escape- . :









Code point (Dec)





Code point (Hex)





Escape- Unicode





A





65





41





"\u{41}"





B





66





42





"\u{42}"





$





36





24





"\u{24}"









8364





20AC





"\u{20AC}"





\n ( )





10





A





"\u{A}"





\r ( )





13





D





"\u{D}"





\t ( )





9





9





"\u{9}"





\v ( )





11





B





"\u{B}"





\e ( )





27





1B





"\u{1B}"





\f ( )





12





C





"\u{C}"





?





128024





1F418





"\u{1F418}"









3461





D85





"\u{D85}"






PHP 5.2.1 , « » (binary strings). , PHP 6.





b



/ , PHP .





echo b'Foo';

Foo
      
      



is_binary, is_unicode is_buffer Unicode PHP 6, PHP 7. - PHP 7 PHP 8.





- , , , PHP PHP .





PHP.






PHP- PHP . PHP , , , .





PHP 7.4, PHP .





, 0b



, .





$number_binary  = 0b101010;
      
      



.





$number_binary = 0b10_1010;

0b101010  === 42; // true

0b10_1010 === 42; // true
      
      



PHP 0



. PHP 8.1, PHP 0O 0o.





$number_octal = 052; // ===  42
$number_octal = 0o52; // ===  42
$number_octal = 0O52; // ===  42
      
      



\0X



\0x



.





$number_hex = 0x2A; // ===  42
$number_hex = 0X2A; // ===  42
      
      



(Scientific Notation)

PHP "E-" .





$number_float = 42E1;
      
      



E- 42 * 10^0 (10 0). , ( PHP) ^



XOR, **



.





.





$planck_constant    = 6.62607004E-34;
$avogadros_constant = 6.022140857E+23;
3.844E5 === 3.844 * 10**5 === 384400.0; // true
      
      




PHP escape- . 0O/0o(PHP 8.1) (PHP 7.4).





, escape- . , , , PHP , .





var_dump((int) "2_34_5");
// int(2)

var_dump((int) "0xabcd");
// int(0)
      
      



Escape- , . , "\43"



, , escape- . $str = "\43"



"#"



, PHP .






«PHP-»



- « PHP».








All Articles