반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
05-19 00:03
관리 메뉴

ImJay

PHP 프로그래밍 입문 제 6장 예제 풀이 본문

대학생활/웹프로그래밍

PHP 프로그래밍 입문 제 6장 예제 풀이

ImJay 2022. 4. 25. 19:31
반응형

PHP 프로그래밍 입문 제 6장 예제 풀이


예제 6-1. 함수를 이용하여 문자열 출력하기

<?php
    function hello() 
    {
		echo ("안녕하세요!");
    }

    hello();
?>

예제 6-1 결과화면

예제 6-2. 함수의 매개변수를 이용하여 두 수의 합 구하기

<?php
    function plus($a, $b)
    {
        $c = $a + $b;
        echo $c;
    }

    plus(10, 20);
    echo "<br>";
    plus(300, 500);
?>

예제 6-2 결과화면

예제 6-3. 함수의 반환 값을 이용하여 두 수의 합 구하기

<?php
    function plus($a, $b)
    {
        $c = $a + $b;
        return $c;
    }

    $result1 = plus(10, 20);
    echo $result1."<br>";

    $result2 = plus(300, 500);
    echo $result2."<br>";
?>

예제 6-3 결과화면

예제 6-4. 함수의 반환 값을 이용하여 정수의 합 구하기

<?php
    // sum($a, $b) 함수는 $a에서 $b까지의 합을 구한다.
    function sum($a, $b)
    {
        $sum=0;
        while($a <= $b)
        {
            $sum=$sum+$a;
            $a++;
        }
        return $sum;
    }

    $from = 1;
    $to   = 100;
	
    $total = sum($from, $to); 
    echo("$from 에서 $to 까지의 합 : $total");
?>

 

예제 6-4 결과화면

예제 6-5. 함수의 반환 값을 이용하여 만 나이 계산하기

<?php
   	/* 오늘 날짜와 생년월일에 따라
           만 나이 계산하기  */
	
	function man_age($year, $month, $day,
                         $b_year, $b_month, $b_day)
 	{
	    if($b_month < $month)  
      	   	$age= $year - $b_year;
   	  else if($b_month == $month)
   	  { 
       	if($b_day <= $day)
      	   $age= $year - $b_year;
       	else
      	   $age= $year - $b_year - 1;
   	  } 
   	  else
	    {
		      $age= $year - $b_year - 1;
	    }
	    return $age;
	}

   	$now_year=2019;
   	$now_month=9;
   	$now_day=10;

   	$birth_year=1999;
   	$birth_month=2;
   	$birth_day=9;

    $your_age = man_age($now_year, $now_month, $now_day,
			    $birth_year, $birth_month,
			    $birth_day);

   	echo "오늘 날짜 : $now_year 년 $now_month 월 $now_day 일<br>";
   	echo "생년월일 : $birth_year 년 $birth_month 월 $birth_day 일생<br>";
   	echo "만 나이 : $your_age 세";
?>

예제 6-5 결과화면

예제 6-6. 함수를 이용하여 입장료 계산하기

<?php
   	function cal_fee1($day, $age)       // 일반 입장권 요금 구하기
   	{
        if ( $day == "주간" )
      	{
            if ($age> 12 && $age < 65)
                $money = 26000;
            else
             	  $money = 19000;
       	}
      	else
        {
         	  if ($age> 12 && $age < 65)
             	  $money = 21000;
         	  else
             	  $money = 16000;
      	}
      
      	return $money;
   	}

   	function cal_fee2($day, $age)       // 자유이용권 요금 구하기
   	{
      	if ( $day == "주간" )
      	{
           	if ($age> 12 && $age < 65)
           	    $money = 33000;
           	else
           	    $money = 24000;
      	}
      	else
      	{
           	if ($age> 12 && $age < 65)
           	    $money = 28000;
         	  else
                $money = 21000;
      	}
      
      	return $money;
   	}

   	function cal_fee3($age)       // 2일 자유이용권 요금 구하기
   	{
       	if ($age> 12 && $age < 65)
          	$money = 55000;
       	else
          	$money = 40000;

      	return $money;
   	}

   	function cal_fee4($age)       // 콤비권 요금 구하기
   	{
   	    if ($age> 12 && $age < 65)
          	$money = 54000;
       	else
          	$money = 40000;

      	return $money;
   	}

   	// $category가 1=> 입장권, 2=> 자유이용권, 3=> 2일 자유이용권, 4=> 콤비권 의미

   	$category = 1;       // 입장권 요금을 구하고자 함
   	$age = 13;
   	$day = "야간";

   	if( $category == 1 )
       	$fee = cal_fee1($day, $age);
   	elseif ( $category == 2 )
        $fee = cal_fee2($day, $age);
   	elseif ( $category == 3 )
       	$fee = cal_fee3($age);
   	else
       	$fee = cal_fee4($age);
      
   	if( $category == 1 )
       	$cat = "일반 입장권";
   	elseif ( $category == 2 )
   	    $cat = "자유이용권";
   	elseif ( $category == 3 )
   	    $cat = "2일 자유이용권";
    else
   	    $cat = "콤비권";

   	echo "- 구분 : $cat<br>";

    if ($category == 1 || $category==2)
        echo "- 주야간 : $day<br>";
   	
    echo "- 나이 : $age 세<br>";
   	echo "- 입장료 : $fee 원";
?>

예제 6-6 결과화면

예제 6-7. 문자열 관련 내장 함수 사용하기

<?php   
	$tel = "010-2777-3333";
	echo "\$tel : $tel<br>";
	$num_tel = strlen($tel);                      // 문자열의 길이 계산
	
	echo "\$tel의 길이 : $num_tel<br>";
	
	$tel1 = substr($tel, 0, 3);                    // 앞에서 세 문자를 가져옴
	echo "$tel1<br>";
	$tel2 = substr($tel, 4, 4);		       // 네 번째 문자에서 네 개를 가져옴
	echo "$tel2<br>";
	$tel3 = substr($tel, 9, 4);		       // 아홉 번째 문자에서 네 개를 가져옴

	echo "$tel3<br>";

    $phone = explode("-", $tel);            	// 하이픈(-)을 기준으로 문자열 분리

	echo "전화번호 : $phone[0] $phone[1] $phone[2]<br>";
?>

예제 6-7 결과화면

궁금한 점은 댓글 부탁드립니다.

반응형
Comments