메뉴 건너뛰기

프로그램언어

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 23083 

    addslashes — 문자열을 슬래시로 인용

  2. PHP 삼항연산자 ?

  3. phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성

  4. No Image 12Apr
    by
    2014/04/12 Views 22662 

    PHP 로그인후 현재 페이지로 돌아오기 처리

  5. No Image 12Apr
    by
    2014/04/12 Views 22458 

    php한글체크를 위한 정규표현식

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

    PHP 파일 다루기

  7. PHP에서 CSV 파일 export

  8. No Image 23Dec
    by
    2016/12/23 Views 22206 

    array_key_exists 배열에서 key가 존재하는지 확인

  9. No Image 14Apr
    by
    2015/04/14 Views 22038 

    PHP에서 조건문 처리

  10. No Image 23Dec
    by
    2016/12/23 Views 21970 

    파일 확장자 비교

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

    접속 IP 검사

  12. No Image 12Apr
    by
    2014/04/12 Views 21862 

    iframe 사용시 iframe의 높이가 내용의 높이만큼 자동으로 조절

  13. No Image 12Apr
    by
    2014/04/12 Views 21699 

    php 파일 다운로드 구현

  14. No Image 06Mar
    by
    2017/03/06 Views 21695 

    PHPExcel 클래스를 이용해 Excel 2007~2010 의 xlsx 파일 읽기 (100만 행 까지)

  15. No Image 27Mar
    by
    2017/03/27 Views 21681 

    파일시스템, 폼 파일업로드 관련 함수

  16. No Image 27Feb
    by
    2014/02/27 Views 21627 

    날짜 계산하기 (하루전 날짜 구하기)

  17. No Image 26Mar
    by
    2014/03/26 Views 21623 

    특수문자 없애는 정규표현식

  18. No Image 23Dec
    by
    2016/12/23 Views 21604 

    array_push 배열 끝에 하나 이상의 요소를 추가

  19. No Image 27Feb
    by
    2014/02/27 Views 21422 

    유용한 함수 모음

  20. No Image 23Dec
    by
    2016/12/23 Views 21302 

    call_user_func 사용자가 정의한 함수를 호출하여 실행고자 할 때 사용

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved