#PHP0016. 分段函数(PHP101)

分段函数(PHP101)

题目描述

数学中的有一种函数叫做分段函数。

现有:

$$y= \begin{cases} \text{1,} \quad x>0\\ \text{0,} \quad x=0\\ \text{-x,} \quad x<0 \end{cases} $$

输入要求

输入一个整数xx

输出要求

输出一个函数映射的整数yy


样例

5
1

代码提示

if语句

在PHP中,if语句是用来基于条件执行不同代码块的一种控制结构。其基本语法如下:

if (condition) {
    // code to be executed if condition is true
}
  • condition​:一个布尔表达式,其结果要么是true要么是false
  • 如果condition的结果为true,则执行大括号{}内的代码。
  • 可以通过添加else子句来指定当条件为false时执行的代码块:
if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}
  • 还可以使用else if(或简写为elseif)来添加多个条件判断:
if (condition1) {
    // code to be executed if condition1 is true
} elseif (condition2) {
    // code to be executed if the condition1 is false and condition2 is true
} else {
    // code to be executed if both condition1 and condition2 are false
}

PHP运算符

PHP提供了多种运算符来对变量和值进行操作,包括:

  • 算术运算符:如加(+)、减(-)、乘(*)、除(/)等。
  • 赋值运算符:如等号(= )用于给变量赋值。
  • 比较运算符:如等于(== )、全等于(=== ),不等于(!= <> ),大于(> )、小于(< )等,用于比较两个值。
  • 逻辑运算符:如与(AND 或 && )、或(OR 或 || )、非(NOT 或 ! ),用于组合条件判断。