echo 'This \'s a \r\n $str \\'; //This 's a \r\n $str \
$str = 'php中文网';echo "This \"s \r\n a ${str} \\";

举例
$str = 'php中文网';echo <<< "HELLO"This "s \r\n a $str \HELLO;echo PHP_EOL;echo <<< 'ABC'This "s \r\n a $str \ABC;

<?php// 打印输出函数// printf(): 格式化输出// printf('输出的格式', 字符串列表)$site = 'php.cn';// %s: 字符串, %d: 数值printf('Hello %s', $site);echo '<br>';printf('SELECT * FROM `%s` LIMIT %d', 'staff', 25);echo '<hr>';// vprintf(): 与printf()区别就在参数上,多个参数使用数组vprintf('SELECT * FROM `%s` LIMIT %d', ['staff', 5]);echo '<hr>';// sprintf():与printf()功能一样, 但是它是返回, 不是打印echo sprintf('SELECT * FROM `%s` LIMIT %d', 'users', 15);echo '<br>';// vsprintf 与 vprint: 返回字符串版本echo vsprintf('SELECT * FROM `%s` LIMIT %d', ['staff', 25]);echo '<hr>';// fprintf():将格式化的字符串写入到一个文件流中$handle = fopen('test.txt', 'w') or die('open file faill');fprintf($handle,sprintf('SELECT * FROM `%s` LIMIT %d', 'staff', 55));echo file_get_contents('test.txt');echo '<hr>';// sscanf(): 按指定的格式输入数据var_dump(sscanf('SN-5566 123', 'SN-%d %d')); //array(2) {[0]=>int(5566)[1]=>int(123)}list($sn) = sscanf('SN-123333', 'SN-%d');echo $sn;echo '<hr>';// number_format(): 数值格式化echo number_format(12345.67), '<br>';echo number_format(12345.67, 2), '<br>';echo number_format(12345.67, 2, '.', ''), '<br>';echo number_format(12345.67, 2, '*', '-'), '<br>';
字符串拆分和合并
// implode(): 一维数组转字符串// 用指定字符串将数组组装成一个字符串返回echo implode('***', ['html', 'css', 'js', 'php']);// echo join('***', ['html', 'css', 'js', 'php']); //同一个函数// explode(): 使用一个字符串来分隔另一个字符串, 返回数组$paras = 'localhost-root-root-utf8-3306';print_r(explode('-', $paras, 4));list($host,$user, $pass) = explode('-', $paras, 4);echo "$host: $user => $pass";
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号