PHP에서 echo, print 및 print_r의 차이점은 무엇입니까?
나는 많이 사용 echo
하고 print_r
거의 사용하지 않습니다 print
.
느낌 echo
은 매크로 print_r
이며의 별칭입니다 var_dump
.
그러나 이것이 차이점을 설명하는 표준 방법은 아닙니다.
print
그리고 echo
거의 동일하거나; 둘 다 문자열을 표시하는 언어 구성입니다. 차이는 미묘하다 : print
반면가 식에 사용될 수 있도록 (1)의 반환 값이 echo
가지고 void
리턴 타입; echo
이러한 사용법은 드물지만 여러 매개 변수를 사용할 수 있습니다. echo
보다 약간 빠릅니다print
. (개인적으로는 항상을 사용 echo
하고 결코 사용 하지 않습니다 print
.)
var_dump
변수의 유형 및 하위 항목 유형 (배열 또는 객체 인 경우)을 포함하여 변수의 세부 덤프를 인쇄합니다. print_r
보다 인간이 읽을 수있는 형태로 변수를 인쇄합니다 : 문자열은 인용되지 않으며, 유형 정보는 생략되고, 배열 크기는 제공되지 않습니다.
var_dump
print_r
내 경험에 따르면 디버깅 할 때보 다 일반적으로 더 유용합니다 . 변수에 어떤 값 / 유형이 있는지 정확히 모르는 경우 특히 유용합니다. 이 테스트 프로그램을 고려하십시오.
$values = array(0, 0.0, false, '');
var_dump($values);
print_r ($values);
를 사용 print_r
하면 0
and 및 0.0
또는 false
and 의 차이점을 알 수 없습니다 ''
.
array(4) {
[0]=>
int(0)
[1]=>
float(0)
[2]=>
bool(false)
[3]=>
string(0) ""
}
Array
(
[0] => 0
[1] => 0
[2] =>
[3] =>
)
에코
- 쉼표로 구분 된 하나 이상의 문자열을 출력합니다.
반환 값 없음
예 :
echo "String 1", "String 2"
인쇄
- 단일 문자열 만 출력
를 반환
1
하므로 표현식에 사용할 수 있습니다.예 :
print "Hello"
또는,
if ($expr && print "foo")
print_r ()
- 하나의 값을 사람이 읽을 수있는 표현으로 출력 합니다
- 문자열뿐만 아니라 배열과 객체를 포함한 다른 유형을 받아 들여 읽을 수 있도록 형식화
- 디버깅 할 때 유용
- 두 번째 선택적 인수가 제공되면 출력을 반향 값으로 반환 할 수 있습니다 (에코 대신)
var_dump ()
- 하나 이상의 값을 쉼표로 구분 하여 사람이 읽을 수있는 표현을 출력 합니다.
- 문자열뿐만 아니라 배열과 객체를 포함한 다른 유형을 받아 들여 읽을 수 있도록 형식화
- 다른 출력 형식을 사용하여
print_r()
예를 들어 값 유형 을 인쇄 합니다. - 디버깅 할 때 유용
- 반환 값 없음
var_export ()
- 사람이 읽을 수 있고 PHP가 실행할 수있는 임의의 값을 하나의 값으로 출력합니다.
- 문자열뿐만 아니라 배열과 객체를 포함한 다른 유형을 받아 들여 읽을 수 있도록 형식화
- 모두 다른 출력 형식을 사용
print_r()
하고var_dump()
- 출력을 발생하여 유효 PHP 코드입니다! - 디버깅 할 때 유용
- 두 번째 선택적 인수가 제공되면 출력을 반향 값으로 반환 할 수 있습니다 (에코 대신)
노트:
- Even though
print
can be used in an expression, I recommend people avoid doing so, because it is bad for code readability (and because it's unlikely to ever be useful). The precedence rules when it interacts with other operators can also be confusing. Because of this, I personally don't ever have a reason to use it overecho
. - Whereas
echo
andprint
are language constructs,print_r()
andvar_dump()
/var_export()
are regular functions. You don't need parentheses to enclose the arguments toecho
orprint
(and if you do use them, they'll be treated as they would in an expression). - While
var_export()
returns valid PHP code allowing values to be read back later, relying on this for production code may make it easier to introduce security vulnerabilities due to the need to useeval()
. It would be better to use a format like JSON instead to store and read back values. The speed will be comparable.
Just to add to John's answer, echo
should be the only one you use to print content to the page.
print
is slightly slower. var_dump()
and print_r()
should only be used to debug.
Also worth mentioning is that print_r()
and var_dump()
will echo by default, add a second argument to print_r()
at least that evaluates to true to get it to return instead, e.g. print_r($array, TRUE)
.
The difference between echoing and returning are:
- echo: Will immediately print the value to the output.
- returning: Will return the function's output as a string. Useful for logging, etc.
echo
Not having return type
print
Have return type
print_r()
Outputs as formatted,
The difference between echo, print, print_r and var_dump is very simple.
echo
echo is actually not a function but a language construct which is used to print output. It is marginally faster than the print.
echo "Hello World"; // this will print Hello World
echo "Hello ","World"; // Multiple arguments - this will print Hello World
$var_1=55;
echo "$var_1"; // this will print 55
echo "var_1=".$var_1; // this will print var_1=55
echo 45+$var_1; // this will print 100
$var_2="PHP";
echo "$var_2"; // this will print PHP
$var_3=array(99,98,97) // Arrays are not possible with echo (loop or index value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with echo (loop or index value required)
You can also use echo statement with or without parenthese
echo ("Hello World"); // this will print Hello World
Just like echo construct print is also a language construct and not a real function. The differences between echo and print is that print only accepts a single argument and print always returns 1. Whereas echo has no return value. So print statement can be used in expressions.
print "Hello World"; // this will print Hello World
print "Hello ","World"; // Multiple arguments - NOT POSSIBLE with print
$var_1=55;
print "$var_1"; // this will print 55
print "var_1=".$var_1; // this will print var_1=55
print 45+$var_1; // this will print 100
$var_2="PHP";
print "$var_2"; // this will print PHP
$var_3=array(99,98,97) // Arrays are not possible with print (loop or index value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with print (loop or index value required)
Just like echo, print can be used with or without parentheses.
print ("Hello World"); // this will print Hello World
print_r
The print_r() function is used to print human-readable information about a variable. If the argument is an array, print_r() function prints its keys and elements (same for objects).
print_r ("Hello World"); // this will print Hello World
$var_1=55;
print_r ("$var_1"); // this will print 55
print_r ("var_1=".$var_1); // this will print var_1=55
print_r (45+$var_1); // this will print 100
$var_2="PHP";
print_r ("$var_2"); // this will print PHP
$var_3=array(99,98,97) // this will print Array ( [0] => 1 [1] => 2 [2] => 3 )
$var_4=array("P"=>"3","J"=>"4"); // this will print Array ( [P] => 3 [J] => 4 )
var_dump
var_dump function usually used for debugging and prints the information ( type and value) about a variable/array/object.
var_dump($var_1); // this will print int(5444)
var_dump($var_2); // this will print string(5) "Hello"
var_dump($var_3); // this will print array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
var_dump($var_4); // this will print array(2) { ["P"]=> string(1) "3" ["J"]=> string(1) "4" }
Echo :
It is statement not a function No return value
Not Required the parentheses
Not Print Array
It is real function
Return type 1
Required the Parentheses
Not Print Array
Print_r
Print in human readable format
String not in Quotes
Not Detail Information of Variable like type and all
var_dump
All dump information of variable like type of element and sub element
**Echocan accept multiple expressions while print cannot. The Print_r () PHP function is used to return an array in a human readable form. It is simply written as
![Print_r ($your_array)][1]
echo : echo is a language construct where there is not required to use parentheses with it and it can take any number of parameters and return void.
void echo (param1,param2,param3.....);
Example: echo "test1","test2,test3";
print : it is a language construct where there is not required to use parentheses it just take one parameter and return
1 always.
int print(param1);
print "test1";
print "test1","test2"; // It will give syntax error
prinf : It is a function which takes atleast one string and format style and returns length of output string.
int printf($string,$s);
$s= "Shailesh";
$i= printf("Hello %s how are you?",$s);
echo $i;
Output : Hello Shailesh how are you?
27
echo returns void so its execution is faster than print and printf
print_r()
is used for printing the array in human readable format.
print_r() can print out value but also if second flag parameter is passed and is TRUE - it will return printed result as string and nothing send to standard output. About var_dump. If XDebug debugger is installed so output results will be formatted in much more readable and understandable way.
they both are language constructs. echo returns void and print returns 1. echo is considered slightly faster than print.
'Programming' 카테고리의 다른 글
N-Tier 아키텍처 란 무엇입니까? (0) | 2020.05.13 |
---|---|
Android-패키지 이름 규칙 (0) | 2020.05.13 |
unique_ptr을 벡터로 푸시 백 할 수없는 이유는 무엇입니까? (0) | 2020.05.13 |
JavaScript에 Sleep / Pause / Wait 기능이 있습니까? (0) | 2020.05.13 |
쿼리에서 반환 된 각 행에 대해 저장 프로 시저를 어떻게 한 번 실행합니까? (0) | 2020.05.13 |