Recap - Creating Reusable Code
Coddy PHP 여정의 기초 섹션에 포함된 레슨 — 71개 중 68번째.
챌린지
쉬움세 줄의 입력을 읽으세요:
- 아이템 이름 (예:
Coffee) - 소수점 숫자로 된 기본 가격 (예:
4.50) - 정수로 된 수량 (예:
3)
함수에 대해 배운 모든 것을 결합하는 calculateTotal이라는 함수를 만드세요:
- 매개변수:
$item(string),$price(float),$quantity(int with default value of1) - 반환 타입:
string
함수 내부에서:
$price * $quantity의 결과를 저장하기 위해 로컬 변수$subtotal을 만드세요[quantity]x [item] = $[subtotal]형식의 문자열을 반환하세요
입력 값으로 함수를 호출하고 반환된 결과를 출력하세요.
Example 1:
입력값이 Coffee, 4.50, 3인 경우, 출력은 다음과 같아야 합니다:
3x Coffee = $13.5Example 2:
입력값이 Sandwich, 8.99, 2인 경우, 출력은 다음과 같아야 합니다:
2x Sandwich = $17.98Example 3:
입력값이 Muffin, 3.25, 5인 경우, 출력은 다음과 같아야 합니다:
5x Muffin = $16.25직접 해보기
<?php
// 입력 읽기
$item = trim(fgets(STDIN));
$price = floatval(fgets(STDIN));
$quantity = intval(fgets(STDIN));
// TODO: 아래에 calculateTotal 함수 생성
// - 매개변수: $item (string), $price (float), $quantity (int with default value of 1)
// - 반환 타입: string
// - 내부: $subtotal 변수 생성하고 형식화된 문자열 반환
// 함수 호출하고 결과 출력
?>기초의 모든 레슨
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic7Arrays Part 2 - Associative
Intro to Associative ArraysAccessing Values by KeyModifying Values by KeyAdding New Key-Value PairsCheck if Key ExistsRecap - Key-Value Data Store10Functions
Declaring & Calling FunctionsFunction ParametersReturning ValuesDefault Parameter ValuesType Declarations for ParamsReturn Type DeclarationsVariable ScopeRecap - Creating Reusable Code2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators