함수내에서 return을 쓸경우...... 채택완료
------------------------------------------- function test() { $a = "free"; } test(); echo $a; -------------------------------------------- ------------------------------------------- function test() { $a = "free"; return $a; } test(); echo $a; -------------------------------------------- 두가지 경우 모두 아무런 값도 출력하지 못하네요... 함수 안에서 선언(?)한 변수를 함수밖에서 참조하려면 어떻게 해야하는 하는지요 ? return 에 대해서도 개념이 혼란스럽네요...
답변 3개
test();
echo $a;
위에껀 아예 리턴을 하지 못하니 값이 나올 수 없죠.
global $a 식으로 정의해준다면 모를까요 ^^
function test() {$a = "free";return $a;}
test();
echo $a;
아래껀 리턴은 해줬지만 그 값을 누군가 받아줘야겠죠?
$var=test();
echo $var;
하시면 답이 나올겁니다.
좀더 쉽게 한다면 echo test(); 해주셔도 되고요..
댓글을 작성하려면 로그인이 필요합니다.
</span></p><p><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">function test() { </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"> $a = "free"; </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"> return $a; </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">} </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px; color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif;">$a = test();</span> </p><p><font color="#484848" face="돋움, Dotum, sans-serif"><span style="font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">echo $a;</span></font></p><p><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">
댓글을 작성하려면 로그인이 필요합니다.
</p><p><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">function test() { </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"> $a = "free"; </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"> return $a; </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">} </span><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><br style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;"><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">echo </span><span style="color: rgb(72, 72, 72); font-family: 돋움, Dotum, sans-serif; font-size: 14.004px; letter-spacing: -0.01px; line-height: 25.2072px;">test();</span> </p><p>
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인