downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

explode> <crypt
Last updated: Fri, 24 Jul 2009

view this page in

echo

(PHP 4, PHP 5)

echo하나 이상의 문자열을 출력

설명

void echo ( string $arg1 [, string $... ] )

모든 인수를 출력합니다.

echo()은 실제 함수가 아니기에 (언어 구조입니다) 괄호를 사용할 필요가 없습니다. echo()는 (다른 언어 구조와 달리) 함수처럼 작동하지 않으므로, 함수 문맥으로 사용할 수 없습니다. 추가로, echo()에 둘 이상의 인수를 사용할 때 괄호를 사용해서는 안됩니다.

echo()는 열기 태그에 이어지는 등호를 사용한 짧은 구문을 가지고 있습니다. 이 짧은 구문은 short_open_tag 설정을 활성화 했을 때만 작동합니다.

I have <?=$foo?> foo.

인수

arg1

출력할 인수.

...

반환값

값을 반환하지 않습니다.

예제

Example #1 echo() 예제

<?php
echo "Hello World";

echo 
"이것은 여러
줄을 표현합니다. 물론 줄바꿈도 
출력합니다."
;

echo 
"이것은 여러\n줄을 표현합니다. 물론 줄바꿈도\n출력합니다.";

echo 
"문자 이스케이프는 \"이렇게\" 합니다.";

// echo 구문 안에 변수를 사용할 수 있습니다.
$foo "foobar";
$bar "barbaz";

echo 
"foo는 $foo"// foo는 foobar

// 배열을 사용할 수도 있습니다.
$baz = array("value" => "foo");

echo 
"이것은 {$baz['value']} !"// 이것은 foo !

// 작은 따옴표는 변수값이 아닌, 변수명을 출력합니다.
echo 'foo는 $foo'// foo는 $foo

// 다른 문자를 사용하지 않는다면, 바로 변수를 echo할 수 있습니다.
echo $foo;          // foobar
echo $foo,$bar;     // foobarbarbaz

// 몇몇 사람들은 결합 echo보다 복수 인수 사용을 선호합니다.
echo 'This ''string ''was ''made ''with multiple parameters.'chr(10);
echo 
'This ' 'string ' 'was ' 'made ' 'with concatenation.' "\n";

echo <<<END
이는 $variable 삽입을 가지는 여러 줄을
출력하는 "here document" 구문을 사용합니다. here
document 종료어는 줄에 세미콜론만을 가지고 있어야
하며, 어떠한 공백도 없어야하는 점에 주의하십시오!
END;

// echo는 함수처럼 작동하지 않기에, 다음 코드는 유효하지 않습니다.
($some_var) ? echo 'true' : echo 'false';

// 그러나, 다음 예제는 작동합니다.
($some_var) ? print 'true' : print 'false'// print도 구조이지만, 함수처럼
                                            // 작동합니다. 그러므로
                                            // 이 문맥에서 사용할 수 있습니다.
echo $some_var 'true''false'// 구문을 변경하여 처리
?>

주의

print()echo()의 차이에 대해서는, FAQT의 Knowledge Base Article을 읽어보십시오: » http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

Note: 이것은 함수가 아닌 언어 구조이기 때문에, 가변 함수 방식으로 호출할 수 없습니다.

참고

  • print() - 문자열을 출력
  • printf() - 형식화한 문자열을 출력
  • flush() - 출력 버퍼를 비웁니다



explode> <crypt
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
echo
Jakob Thomsen
23-Nov-2008 02:23
A way to color your echo output is to use shell_exec and the echo command (this only works on Linux/bash) in the following way:

<?php
echo shell_exec('echo "\e[0;31m Red color \e[0;32mGreen color \e[0m No color "');
?>

See http://wiki.archlinux.org/index.php/Color_Bash_Prompt for more colors and other options.
sandaimespaceman at gmail dot com
01-Sep-2008 06:25
Outputting \n won't generate a line break in the browser, <br /> is required for line break. Also,
<?php
echo "first line";
echo
"second line";
?>
will like
first linesecond line
because you didn't insert spaces/line breaks.
the echo function can also be written like
<?php
echo ('text here')
?>
nikolaas dot mennega at links dot com dot au
01-Nov-2007 07:04
hemanman at gmail dot com, the problem is that func() doesn't actually return a value (string or otherwise), so the result of echoing func() is null.

With the comma version, each argument is evaluated and echoed in turn: first the literal string (simple), then func(). Evaluating a function call obviously calls the function (and in this case executes its own internal echo), and the result (null) is then echoed accordingly. So we end up with "outside func() within func()" as we would expect.

Thus:

<?
echo "outside func ()\n", func ();
?>

effectively becomes:

<?
echo "outside func ()\n";
//func ()
{
echo "within func ()\n";
}
echo '';
?>

The dot version is different: there's only one argument here, and it has to be fully evaluated before it can be echoed as requested. So we start at the beginning again: a literal string, no problem, then a concatenator, then a function call. Obviously the function call has to be evaluated before the result can be concatenated with the literal string, and THAT has to happen BEFORE we can complete the echo command. But evaluating func() produces its own call to echo, which promptly gets executed.

Thus:

<?
echo "outside func ()\n" . func ();
?>

effectively becomes:

<?
//func ()
{
echo "within func ()\n";
}
echo "outside func ()\n" . '';
?>
Jason Carlson - SiteSanity
16-May-2005 05:28
In response to Ryan's post with his echobig() function, using str_split wastes memory resources for what you are doing.

If all you want to do is echo smaller chunks of a large string, I found the following code to perform better and it will work in PHP versions 3+

<?php
function echobig($string, $bufferSize = 8192)
{
 
// suggest doing a test for Integer & positive bufferSize
 
for ($chars=strlen($string)-1,$start=0;$start <= $chars;$start += $bufferSize) {
    echo
substr($string,$start,$buffer_size);
  }
}
?>
ryan at wonko dot com
27-Feb-2005 08:56
Due to the way TCP/IP packets are buffered, using echo to send large strings to the client may cause a severe performance hit. Sometimes it can add as much as an entire second to the processing time of the script. This even happens when output buffering is used.

If you need to echo a large string, break it into smaller chunks first and then echo each chunk. The following function will do the trick in PHP5:

<?php
function echobig($string, $bufferSize = 8192)
{
   
$splitString = str_split($string, $bufferSize);

    foreach(
$splitString as $chunk)
        echo
$chunk;
}
?>
zombie)at(localm)dot(org)
25-Jan-2003 07:26
[Ed. Note: During normal execution, the buffer (where echo's arguments go) is not flushed (sent) after each write to the buffer. To do that you'd need to use the flush() function, and even that may not cause the data to be sent, depending on your web server.]

Echo is an i/o process and i/o processes are typically time consuming. For the longest time i have been outputting content by echoing as i get the data to output. Therefore i might have hundreds of echoes in my document. Recently, i have switched to concatenating all my string output together and then just doing one echo at the end. This organizes the code more, and i do believe cuts down on a bit of time. Likewise, i benchmark all my pages and echo seems to influence this as well. At the top of the page i get the micro time, and at the end i figure out how long the page took to process. With the old method of "echo as you go" the processing time seemed to be dependent on the user's net connection as well as the servers processing speed. This was probably due to how echo works and the sending of packets of info back and forth to the user. One an one script i was getting .0004 secs on a cable modem, and a friend of mine in on dialup was getting .2 secs. Finally, to test that echo is slow; I built strings of XML and XSLT and used the PHP sablotron functions to do a transformation and return a new string. I then echoed the string. Before the echo, the process time was around .025 seconds and .4 after the echo. So if you are big into getting the actual processing time of your scripts, don't include echoes since they seem to be user dependent. Note that this is just my experience and it could be a fluke.

explode> <crypt
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites