PHP: echo'ing UTF-8 characters into Windows command line

php echo variable in html
php echo not working
php echo html code as text
php echo array
php echo variable in string
php echo new line
php echo color variable
print page in php

I've ran into a problem, but can't tell whether it is PHP or Windows' command line. When a PHP script tries to echo UTF-8 characters into cmd with UTF-8 codepage loaded, the process stops unexpectedly. Here is a case:

test1.php:

<?php
error_reporting( -1 );
echo 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ', "\n";
echo "OK";
?>

test2.php:

<?php
error_reporting( -1 );    
echo 'ASCII: ABCDEFGHIJKLMNOPQRSTUVWXYZ', "\n";
echo 'UTF-8: АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ', "\n";
echo 'UTF-8: АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ', "\n";
echo "OK";
?>

(Both test1.php and test2.php are saved in UTF-8 without BOM.)

The command prompt log:

e:\tests>chcp 1252
Active code page: 1252

e:\tests>php -f test1.php
АБВР"Р"ЕЁЖЗРЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ
OK
e:\tests>php -f test2.php
ASCII: ABCDEFGHIJKLMNOPQRSTUVWXYZ
UTF-8: АБВР"Р"ЕЁЖЗРЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ
UTF-8: АБВР"Р"ЕЁЖЗРЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ
OK
e:\tests>chcp 65001
Active code page: 65001

e:\tests>php -f test1.php

e:\tests>php -f test2.php
ASCII: ABCDEFGHIJKLMNOPQRSTUVWXYZ
UTF-8: АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ
e:\tests>

While in 1252 mode, all characters get echoed (although not correctly, of course). But in 65001 (UTF-8) mode test1 stalls on the very first character while test2 stalls on the first one of the second UTF-8 row.

PHP version is:

PHP 5.4.13 (cli) (built: Mar 15 2013 02:07:14)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Windows is XP SP3.

Update:

1) If I change echo to:

$f = fopen( 'php://stdout', 'w' );
fwrite( $f, ... );
...
close( $f );

it works.

2) If I redirect the output:

e:\tests>php -f test1.php > out.log

it also works (with echo).

But what is wrong with the first case?

You need to change the font as well.

Right click the title bar of "Command Prompt", click "Properties" and select a TTF font rather than "Raster Fonts".

PHP Echo and Print Statements, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

For windows cyrillic this helps me (windows 7 git bash console):

$text = "Юра";
echo iconv("utf-8", "cp866", $text)

PHP Echo Vs Print Statement, In this tutorial you will understand the basic difference between PHP echo and print statements as well as how to use them to display output in the browser. In PHP the two basic constructs to get outputs are echo and print. Actually, echo() is not a function, it is a language construct, therefore, you can use it without parentheses.

Try this. I hope it will work:

shell_exec('chcp 866');
echo iconv('utf-8', 'cp866', $message);

Or use this improved helper:

class ConsoleHelper
{
    /**
     * @var boolean
     */
    private static $isEncodingSet = false;

    /**
     * @param string $message
     * @return string
     */
    public static function encodeMessage($message)
    {
        $isWindows = (DIRECTORY_SEPARATOR == '\\');
        if ($isWindows) {
            if ( ! self::$isEncodingSet) {
                shell_exec('chcp 866');
                self::$isEncodingSet = true;
            }
            $message = iconv('utf-8', 'cp866', $message);
        }
        return $message;
    }
}

PHP echo Function, You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output. PHP Code: <?php $myString = "​  PHP echo statement In PHP ‘echo’ statement is a language construct and never behaves like a function, hence no parenthesis required. The end of echo statement is identified by the semi-colon (‘;’). We can use ‘echo’ to output strings or variables.

What does php's echo do?, PHP echo and HTML list element. Display string, variable with echo. Syntax echo (arg1, arg2 ) Example : Simple string display PHP - Echo. As you saw in the previous lesson, the PHP command echo is a means of outputting text to the web browser. Throughout your PHP career you will be using the echo command more than any other. So let's give it a solid perusal!

Difference between echo and print_r in php?, In PHP 'echo' statement is a language construct and never behaves like a function, hence no parenthesis required. The end of echo statement is identified by  Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>).

Echo, Some of methods are described here: Using echo or print: PHP echo or print can be used to display HTML markup, javascript, text or variables. Example 1: This  You can display the value in a PHP variable on a web page by using any of the following statements: echo, print, print_r, or var_dump. Variables in echo and print statements You can display the value in a variable on a web page with an echo or print statement.

Comments
  • I thought, maybe the problem was the cmd itself, but if I call type test2.php (or any other UTF-8 file), it works.
  • I have Lucida Console set by default.
  • I used that for some time, and then wrote a batch which does roughly php foo.php > tmp && type tmp so I can see the output.