메뉴 건너뛰기

프로그램언어

2015.04.14 19:20

PHP 삼항연산자 ?

조회 수 22947 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

if 와 else 문의 장황함을 피하는 방법 중 하나는 더욱 간결한 삼항 연산자(conditional operator)인 ? 를 사용하는 것이다.



예제를 통해 살펴보자.

$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');

/* shorthand usage */
$message = 'Hello '.($user->get('first_name') ?: 'Guest');


/* "thankfully-you-don't-need-to-maintain-this" level */
 $days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31)); //returns days in the given month


/* If condition is true then assign a to result otheriwse b */
/* 조건이 참이면 a를 할당하고, 거짓이면 b를 할당
$result = ($a < $b ) ? $a :$b;


function xx($a,$b)
{
  $rs = ($a > $b) ? $a : $b ;
  return $rs;
}


<?php

$j = 11;

while($j-- > -10) {

if($j == 0 ) continue;  // 0 으로 나누는 것을 방지하기 위한 목적

echo "$j". (10/$j) . "<br />";

}

?>


  1. No Image 23Dec
    by
    2016/12/23 Views 17328 

    시간관련함수

  2. No Image 22Dec
    by
    2016/12/22 Views 19972 

    PHP Mcrypt 라이브러리를 활용한 암호화 시스템

  3. No Image 21Sep
    by
    2016/09/21 Views 33237 

    $_SERVER 환경변수

  4. No Image 21Sep
    by
    2016/09/21 Views 25959 

    환경변수 HTTP_USER_AGENT를 이용해서 스마트 기기 분류하기

  5. No Image 22Aug
    by
    2016/08/22 Views 20732 

    템플릿 관련 정보

  6. No Image 22Aug
    by
    2016/08/22 Views 21042 

    RSSReader Class 제작 및 Reader 만들기

  7. No Image 22Apr
    by
    2016/04/22 Views 20637 

    HTTP Protocol의 data method - GET / POST

  8. PHP에서 CSV 파일 export

  9. 디렉토리내 특정 확장자를 가진 파일 전부 삭제

  10. PHP error 메시지 출력

  11. No Image 14Apr
    by
    2015/04/14 Views 20778 

    검색어 처리 루틴

  12. No Image 14Apr
    by
    2015/04/14 Views 25334 

    파일 삭제

  13. PHP 삼항연산자 ?

  14. PHP continue 문

  15. No Image 14Apr
    by
    2015/04/14 Views 24904 

    array (배열)

  16. PHP switch 문

  17. No Image 14Apr
    by
    2015/04/14 Views 26622 

    전화번호에 하이픈(-) 넣기

  18. No Image 14Apr
    by
    2015/04/14 Views 25198 

    한글자르기 substr

  19. No Image 14Apr
    by
    2015/04/14 Views 21246 

    DB 내용을 화면에 출력(이중 for 문)

  20. No Image 14Apr
    by
    2015/04/14 Views 22401 

    PHP 파일 다루기

Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved